从Windows窗体打印的工具
问题描述:
因此,我需要从Windows窗体应用程序(c#,.net 4)打印到激光打印机,并且我发现了超级“有趣”的PrintDocument类,现在有可用的代码,但它看起来像这样:从Windows窗体打印的工具
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=10 & prod = thermallabel)来打印Zebra打印机,并希望有类似的API用于创建激光打印机的打印文档。我还需要在此打印文档中添加条形码打印,因此,包含条形码功能的任何工具都会更有帮助。
有什么建议吗?
答
如果你需要你的窗口打印在屏幕上的exacly打印,我会建议使用DrawToBitmap方法。更多细节:How to get a screen capture of a .Net control programmatically。这种方法也适用于复合控件(例如整个表单)。
如果您需要更高级的打印功能,我建议您寻找.NET的一些“报告库”(可能是其中之一:Report Viewer/Crystal Reports/SQL Server Reporting Services)。虽然我不能给出任何详细的指导,因为我还没有使用过它们中的任何一个。
我想我正在寻找的将类似于报告库,但可访问/绑定使用C#。我非常确定MS Report Viewer和Crystal依赖于使用关系数据,但我的数据来自Web服务,并且包含动态附加位图图像... – 2011-04-27 03:08:07
您当然可以填写报告,其中包含来自网络服务。看看[本演练](http://msdn.microsoft.com/en-us/library/ms251784.aspx#Y114)。基本上,您可以根据任何业务对象创建内存数据源。我认为附加位图图像也是可能的。实际上,从代码中快速创建报告是相当普遍的情况,所以我几乎可以肯定,市场上的任何报告库都可行。我使用Jasperreports在java中完成了类似的事情。 – surfen 2011-04-29 22:13:50