Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a String

How do I print out a String that I generated in Winforms? The String I'd like to print out is located in a UserControl.

This is what I already have. When I press the Printing Button, nothing is printed.

private void print_Click(object sender, EventArgs e)
{
    PrintDialog printDialog = new PrintDialog();
    PrintDocument printDocument = new PrintDocument();
    printDialog.Document = printDocument;

    printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
    DialogResult result = printDialog.ShowDialog();

    if (result == DialogResult.OK)
    {
        printDocument.Print();
    }

    PrintDocument recordDoc;

     // Create the document and name it
     recordDoc= new PrintDocument();
     recordDoc.DocumentName = "Customer Receipt";
     recordDoc.PrintPage += new PrintPageEventHandler(this.PrintReceiptPage);

     // Preview document
     dlgPreview.Document = recordDoc;
     dlgPreview.ShowDialog();

     // Dispose of document when done printing
     recordDoc.Dispose();    
}
like image 758
phill Avatar asked Dec 08 '25 07:12

phill


1 Answers

In the PrintPage event try this

 e1.Graphics.DrawString(s, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));
 try
 {
     p.Print();
 }
 catch (Exception ex)
 {
     throw new Exception("Exception Occured While Printing", ex);
 }

taken from https://social.msdn.microsoft.com/Forums/en-US/93e54c4f-fd07-4b60-9922-102439292f52/c-printing-a-string-to-printer?forum=csharplanguage

like image 67
Dimitris Batsougiannis Avatar answered Dec 10 '25 20:12

Dimitris Batsougiannis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!