直接向打印机发送字符串

时间:2011-09-15 17:21:25

标签: c# visual-studio-2008 .net-3.5 printing

  

可能重复:
  Send document to printer with C#

我想直接向打印机发送字符串值。当然,我可以将数据表发送到打印机。但首先我想知道如何在没有任何提示最终用户打印机的情况下发送我的字符串值。 我在互联网上搜索了3个小时但没有找到回应。 请帮我。谢谢:)

2 个答案:

答案 0 :(得分:15)

您可以在PrintDocument命名空间下使用System.Drawing.PrintingPrint方法将使用您的默认打印机

打印字符串
string s = "string to print";

PrintDocument p = new PrintDocument();
p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
{
    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);
}

here

找到示例

答案 1 :(得分:2)

不确定您要搜索的是什么,但这里有两篇我在MSDN上发现的关于打印的文章。简而言之,PrintDocument类包含了为每个正在打印的页面引发PrintPage事件的功能。

http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.printdialog.aspx