如何打印字符串 [英] How to print a String

查看:166
本文介绍了如何打印字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何打印出我的WinForms生成的字符串?我想打印出字符串坐落在一个用户控件。



这是我已经有了。当我按下打印按钮,没有任何被打印出来。

 私人无效print_Click(对象发件人,EventArgs五)
{
PrintDialog类PrintDialog类=新PrintDialog类();
的PrintDocument PrintDocument的=新的PrintDocument();
printDialog.Document =的PrintDocument;

printDocument.PrintPage + =新PrintPageEventHandler(printDocument_PrintPage);
DialogResult的结果= printDialog.ShowDialog();

如果(结果== DialogResult.OK)
{
printDocument.Print();
}

的PrintDocument recordDoc;

//创建该文件并将其命名为
recordDoc =新的PrintDocument();
recordDoc.DocumentName =客户回执;
recordDoc.PrintPage + =新PrintPageEventHandler(this.PrintReceiptPage);

//预览文件
dlgPreview.Document = recordDoc;
dlgPreview.ShowDialog();完成打印
recordDoc.Dispose当

//处置文件();
}


解决方案

在PrintPage事件试试这个

  e1.Graphics.DrawString(S,新的字体(宋体,12),新SolidBrush(Color.Black ),新的RectangleF(0,0,p.DefaultPageSettings.PrintableArea.Width,p.DefaultPageSettings.PrintableArea.Height)); 

{
p.Print();
}
赶上(异常前)
{
抛出新的异常(异常发生时打印,前);
}



从的 https://social.msdn.microsoft .COM /论坛/ EN-US / 93e54c4f-fd07-4b60-9922-102439292f52 / C-打印一个字符串到打印机吗?论坛= csharplanguage


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

解决方案

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

这篇关于如何打印字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆