我已经安装了打印机和内部网服务器上的工作,我想编程方式发送的“Hello World”到默认打印机。 这似乎是最简单的事情,但我一直在google搜索,但没有成功了几个小时。 (注:我正在开发运行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();
}