我有兩個程序,一個是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