【发布时间】:2011-08-10 00:22:36
【问题描述】:
所以,我需要从 Windows 窗体应用程序(c#、.net 4)打印到激光打印机,我发现了超级“有趣”的 PrintDocument 类,现在代码可以工作了,但它看起来像这样:
private void PrintCollate(vws.custom.production.IndiPackLabel ipl)
{
var pd = new PrintDocument();
pd.DocumentName = "Collate";
var printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = "HP LaserJet 4100";
pd.PrinterSettings = printerSettings;
pd.PrinterSettings.Copies = 1;
_currCollate = ipl;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
_currCollate = null;
}
private static vws.custom.production.IndiPackLabel _currCollate;
public void pd_PrintPage(Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float linesPerPage = 0;
//int count = 0;
float leftMargin = 20;
float topMargin = 20;
float yPos = topMargin;
var printFontNormal = new System.Drawing.Font("consolas", 10);
var printFontNormalBold = new System.Drawing.Font("consolas", 10, FontStyle.Bold);
float stdFontHeight = printFontNormal.GetHeight(e.Graphics);
var printFontSmall = new System.Drawing.Font("consolas", 8);
float smallFontHeight = printFontSmall.GetHeight(e.Graphics);
linesPerPage = e.MarginBounds.Height / stdFontHeight;
e.Graphics.DrawString(("Order: " + _currCollate.OrderDescription).PadRight(91) + "ORD #:" + _currCollate.OrderNumber, printFontNormal, Brushes.Black, leftMargin, yPos);
yPos += stdFontHeight;
string customerInfo = (_currCollate.Customer.FirstName + " " + _currCollate.Customer.LastName).PadRight(90) + " BAG #" + _currCollate.BagNumber;
e.Graphics.DrawString(customerInfo, printFontNormal, Brushes.Black, leftMargin, yPos);
yPos += stdFontHeight;
yPos += stdFontHeight;
string header = "ITEMNO".PadRight(20) + "ITEM NAME".PadRight(70) + "QTY".PadRight(3);
e.Graphics.DrawString(header, printFontNormalBold, Brushes.Black, leftMargin, yPos);
int itemTotal = 0;
foreach (var item in _currCollate.OrderItems)
{
yPos += stdFontHeight;
string itemLine = item.ItemNo.PadRight(20) + (item.ItemName + " " + item.OptionNames).PadRight(70) + item.Qty.ToString().PadRight(3);
e.Graphics.DrawString(itemLine, printFontNormal, Brushes.Black, leftMargin, yPos);
if (item.Notes.Length > 0)
{
yPos += stdFontHeight;
string notesLine = string.Empty.PadRight(20) + item.Notes;
e.Graphics.DrawString(notesLine, printFontNormal, Brushes.Black, leftMargin, yPos);
}
itemTotal += item.Qty;
}
yPos += stdFontHeight;
string footer = "TOTAL ITEMS: ".PadRight(90) + itemTotal;
e.Graphics.DrawString(footer, printFontNormal, Brushes.Black, leftMargin, yPos);
e.Graphics.DrawRectangle(new Pen(Color.Black, 2), new Rectangle(20,600,700,500));
}
必须有更好的方法。尤其是当我必须在 6 个月内更改它并且必须重新解决这个问题时。
我正在使用 Neodynamic 的热敏标签 SDK (http://www.neodynamic.com/ND/ProductsPage.aspx?tabid=107&prod=thermallabel) 打印到 Zebra 打印机,并希望使用类似的 API 来创建打印文档用于激光打印机。我还需要在此打印文档中添加条形码打印,因此任何包含条形码功能的工具都会更有帮助。
有什么建议吗?
【问题讨论】:
标签: c# windows winforms printing