直接发送字符串到打印机字符串、打印机、直接发送

2023-09-07 04:57:42 作者:你是梦我一想就痛

可能重复:   发送文件到打印机用C#

我要直接发送一个字符串值打印机。当然是非常好,我可以发送一个数据表打印机。但首先我想知道我怎么可以把我的字符串值,没有任何提示最终用户的打印机。 我搜索了3小时上网,但发现没有任何反应。 请帮助我。 THX:)

I want to to Send a String Value directly to printer. of course it is very better that I can send a datatable to printer. but first of all I want to know how I can send my String Value without any prompting for end user to printer. I have searched for 3 hours in internet but found no response. please help me. Thx :)

推荐答案

您可以使用的PrintDocument System.Drawing.Printing 命名空间。 打印方法将使用默认的打印机打印字符串

you can use PrintDocument under System.Drawing.Printing namespace. Print method will print the string using your default printer

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

从这里