首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于从Windows窗体打印的工具

用于从Windows窗体打印的工具
EN

Stack Overflow用户
提问于 2011-04-21 12:24:18
回答 1查看 3.5K关注 0票数 1

因此,我需要从windows表单应用程序(c#、.net 4)打印到激光打印机,我发现了超级“有趣”的PrintDocument类,现在有了可以工作的代码,但它看起来如下所示:

代码语言:javascript
复制
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的热敏标签开发工具包(http://www.neodynamic.com/ND/ProductsPage.aspx?tabid=107&prod=thermallabel)来打印到Zebra打印机,并希望有一个类似的应用程序接口来为激光打印机创建打印文档。我还需要将条形码打印添加到此打印文档中,因此任何包含条形码功能的工具都会更有帮助。

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-26 19:24:13

如果你想让你的windows窗体完全按照屏幕上的显示方式打印出来,我建议你使用DrawToBitmap方法。更多细节:How to get a screen capture of a .Net control programmatically。这种方法也适用于复合控件(例如,整个表单)。

如果您需要更高级的打印功能,我建议您寻找一些用于.NET的“报告库”(可能是其中之一:Report Viewer / Crystal Reports / SQL Server Reporting Services)。不过,我不能给出任何详细的指导,因为我自己还没有用过任何一个。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5739480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档