我已安装打印机并在 Intranet 服务器上工作,我想以编程方式将“hello world”发送到该默认打印机。这似乎是最简单的事情,但我已经搜索了几个小时没有成功。(注意:我正在运行 Windows 7 的部署机器上开发 asp.net mvc)
我试图将此处的 VB 示例翻译成 C#,但它说“没有安装打印机”。
public void TestPrint()
{
var x = new PrintDocument();
x.PrintPage += new PrintPageEventHandler(PrintPage);
x.Print();
}
private void PrintPage(Object sender, PrintPageEventArgs e)
{
var textToPrint = "Hello world";
var printFont = new Font("Courier New", 12);
var leftMargin = e.MarginBounds.Left;
var topMargin = e.MarginBounds.Top;
e.Graphics.DrawString(textToPrint, printFont, Brushes.Black, leftMargin, topMargin);
}
我还在这里尝试了 MSDN 的一个片段,但它说它无法识别打印机名称。
public void TestPrint(string msg)
{
var server = new LocalPrintServer();
var queue = LocalPrintServer.GetDefaultPrintQueue();
// Call AddJob
var job = queue.AddJob();
// Write a Byte buffer to the JobStream and close the stream
var stream = job.JobStream;
var buffer = UnicodeEncoding.Unicode.GetBytes(msg);
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}