用于从Windows窗体打印的工具
本文关键字:工具 打印 窗体 Windows 用于 | 更新日期: 2023-09-27 17:52:11
所以,我需要从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∏thermallabel)打印到Zebra打印机,并希望有一个类似的API用于为激光打印机创建打印文档。我还需要将条形码打印添加到这个打印文档中,因此任何包含条形码功能的工具都会更有帮助。
有什么建议吗?
如果你需要你的窗体打印完全显示在屏幕上,我建议使用DrawToBitmap方法。更多细节:如何以编程方式获取。net控件的屏幕截图。这种方法也适用于复合控件(例如。整个形式)。
如果你需要更高级的打印功能,我建议寻找一些。net的"报表库"(可能是其中之一:报表查看器/Crystal报表/SQL Server报表服务)。但是我不能给出任何详细的指导,因为我自己还没有用过。