如何打印字符串

时间:2015-08-11 11:16:39

标签: c# string winforms printing

如何打印出我在Winforms中生成的String?我要打印的字符串位于UserControl中。

这就是我已经拥有的。当我按下“打印”按钮时,不会打印任何内容。

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();    
}

1 个答案:

答案 0 :(得分:1)

在PrintPage事件中试试这个

 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);
 }

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