【问题标题】:Tools For Printing From a Windows Forms从 Windows 窗体打印的工具
【发布时间】: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


    【解决方案1】:

    如果您需要完全按照屏幕显示的方式打印 Windows 表单,我建议您使用 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 依赖于使用关系数据,但我的数据来自网络服务,包括动态附加位图图像...
    • 您当然可以使用来自网络服务的数据填写报告。看看this walkthrough。基本上,您可以基于任何业务对象创建内存数据源。我认为附加位图图像也是可能的。事实上,从代码中动态创建报告是一种很常见的场景,所以我几乎可以肯定,这对于市场上的任何报告库都是可行的。我使用 Jasperreports 在 java 中做过类似的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    相关资源
    最近更新 更多