我的打印机安装和工作在内部网服务器上,我想以编程方式发送的Hello World到默认打印机。这似乎是最简单的事情,但我一直在Google上搜寻了几个小时,但没有成功。 (注:我开发asp.net mvc的是运行Windows 7的部署机器本身上)
我试着翻译从VB 一个例子这里成C#,但它说:没有安装打印机。
公共无效TestPrint()
{
VAR X =新的PrintDocument();
x.PrintPage + =新PrintPageEventHandler(的PrintPage);
x.Print();
}
私人无效的PrintPage(对象发件人,PrintPageEventArgs E)
{
VAR textToPrint =世界,你好;
VAR printFont =新的字体(宋体,12);
VAR LEFTMARGIN = e.MarginBounds.Left;
VAR TOPMARGIN = e.MarginBounds.Top;
e.Graphics.DrawString(textToPrint,printFont,Brushes.Black,LEFTMARGIN,TOPMARGIN);
}
我还曾试图从MSDN 一个片段在这里,但它说,它不承认的打印机名称。
公共无效TestPrint(string信息)
{
VAR服务器=新LocalPrintServer();
变种队列= LocalPrintServer.GetDefaultPrintQueue();
//调用AddJob
VAR工作= queue.AddJob();
//写一个字节的缓冲区的作业流和关闭流
VAR流= job.JobStream;
VAR缓冲=统一codeEncoding.Uni code.GetBytes(MSG);
stream.Write(缓冲液,0,buffer.Length);
stream.Close();
}
解决方案
打印Hello World的服务器端的.NET
共享打印机 创建一个的PrintDocument
对象
通过名称来引用打印机
添加一个方法来提供内容
打印
code
使用System.Drawing中;
使用System.Drawing.Printing;
公共无效打印()
{
VAR DOC =新的PrintDocument();
doc.PrinterSettings.PrinterName =部署,计算机名共享名;
doc.PrintPage + =新PrintPageEventHandler(ProvideContent);
doc.Print();
}
公共无效ProvideContent(对象发件人,PrintPageEventArgs E)
{
e.Graphics.DrawString(
你好,世界,
新的字体(宋体,12),
Brushes.Black,
e.MarginBounds.Left,
e.MarginBounds.Top);
}
I have the printer installed and working on an intranet server and I want to programmatically send "hello world" to that default printer. This seems like the simplest thing but I've been googling for a couple hours with no success. (note: I am developing asp.net mvc on the deployment machine itself which is running Windows 7)
I tried to translate an example from VB here into C# but it said "no printers are installed".
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);
}
I had also tried a snippet from MSDN here but it said it did not recognize the printer name.
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();
}
解决方案
Print "hello world" server-side in .NET
Share the printer Create aPrintDocument
object
Reference the printer by name
Add a method to provide content
Print
Code
using System.Drawing;
using System.Drawing.Printing;
public void Print()
{
var doc = new PrintDocument();
doc.PrinterSettings.PrinterName = "deployment-machine-nameshare-name";
doc.PrintPage += new PrintPageEventHandler(ProvideContent);
doc.Print();
}
public void ProvideContent(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(
"Hello world",
new Font("Arial", 12),
Brushes.Black,
e.MarginBounds.Left,
e.MarginBounds.Top);
}
相关推荐
最新文章