我有两个程序,一个是Windows服务,另一个是Windows窗体应用程序。它们具有完全相同的打印一页A4页面的代码。他们都打印到相同的网络打印机,他们开始绘制位置0,0。不同的打印结果来自完全相同的代码
private void pd_PrintCustomsDocument(object sender, PrintPageEventArgs ev)
{
Graphics g = ev.Graphics;
g.PageUnit = GraphicsUnit.Millimeter;
using (Font courierBig = new Font("Courier", 15))
{
g.DrawString("Shipping Invoice", courierBig, Brushes.Black, new Point(0, 0));
// etc
}
}
windows窗体应用程序正确打印文档,页边距被使用。但服务开始正好在纸张的边缘打印。 打印与服务和Windows窗体应用程序的gdi +有区别吗?
用于实际打印的代码被划分为基和亚类为覆盖默认打印机设置,如从不同的托盘选择页:
public class PrintBehaviour : IDisposable
{
private string mPrinterName;
private PrintPageEventHandler mHandler;
private PrintDocument mDocument = new PrintDocument();
public PrintBehaviour(string name, PrintPageEventHandler handler)
{
mPrinterName = name;
mHandler = handler;
mDocument.PrintController = new StandardPrintController();
}
public virtual void SettingsOverride(PrintDocument doc) {}
public void Print()
{
SettingsOverride(mDocument);
mDocument.PrinterSettings.PrinterName = mPrinterName;
mDocument.PrintPage += new PrintPageEventHandler(mHandler);
mDocument.Print();
}
public void Dispose()
{
mDocument.Dispose();
}
}
public sealed class CustomsPrintBehaviour : PrintBehaviour
{
private string mPaperTray;
public CustomsPrintBehaviour(string name, PrintPageEventHandler handler, string paperTray)
: base(name, handler)
{
mPaperTray = paperTray;
}
public override void SettingsOverride(PrintDocument doc)
{
base.SettingsOverride(doc);
doc.DefaultPageSettings.Landscape = true;
foreach (PaperSource source in doc.PrinterSettings.PaperSources)
{
if (source.SourceName.Trim().ToUpper() == mPaperTray)
{
doc.DefaultPageSettings.PaperSource = source;
PaperSize size = new PaperSize { RawKind = (int)PaperKind.A4 };
doc.DefaultPageSettings.PaperSize = size;
break;
}
}
}
}
和称为像这样:
using (var pb = new CustomsPrintBehaviour(_customsPrinter, pd_PrintCustomsDocument, kv["PaperTray"].ToUpper()))
{
pb.Print();
}
你能告诉我们实际的打印代码吗? –
编辑OP与打印机类 – Laurijssen