3

我正在使用 PrintDocument 类打印到我的 Brother 标签打印机。当我执行 Print() 方法时,打印机开始闪烁红色错误灯,但其他一切都返回成功。

我可以在我的激光打印机上运行相同的代码,一切正常。

如何查看导致标签打印机错误的原因?

代码:

public class Test
{
    private Font printFont;
    private List<string> _documentLinesToPrint = new List<string>();

    public void Run()
    {
        _documentLinesToPrint.Add("Test1");
        _documentLinesToPrint.Add("Test2");
        printFont = new Font("Arial", 10);
        var pd = new PrintDocument();
        pd.DefaultPageSettings.Margins = new Margins(25, 25, 25, 25);
        pd.DefaultPageSettings.PaperSize = new PaperSize("Label", 400, 237);

        var printerSettings = new System.Drawing.Printing.PrinterSettings();
        printerSettings.PrinterName ="Brother QL-570 LE";
        pd.PrinterSettings = printerSettings;
        pd.PrinterSettings.Copies = 1;
        pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
        pd.Print();
    }

    // The PrintPage event is raised for each page to be printed. 
    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;

        // Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height /
           printFont.GetHeight(ev.Graphics);

        // Print each line of the file. 
        while ((count < linesPerPage) && (count < _documentLinesToPrint.Count))
        {
            line = _documentLinesToPrint[count];

            yPos = topMargin + (count *
               printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, Brushes.Black,
               leftMargin, yPos, new StringFormat());

            line = null;
            count++;
        }

        // If more lines exist, print another page. 
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
    }
}
4

3 回答 3

3

PrintDocument 是一个非常基本的 API。您可以获得简单的通用打印,但其代价是减少了并非特定于打印驱动程序的功能。我的 HP 打印机通常给我一个打印错误而不是异常。看到类似的事情发生在你身上并不奇怪。

闪烁可能是您可以查找的代码。如果失败,您可以尝试保存为图像格式、PDF 或 XPS。或使用第 3 方库或编写您自己的PCL 文件。有很多选择。创建一个您可以查看的输出,而不是内存中的输出,应该调试诸如边距之类的计算。您可以查看 PDF,看看它是否看起来很古怪。请记住,它在 PC 上的外观可能与输出略有不同,尤其是在边缘附近打印时。

于 2013-11-08T03:34:34.837 回答
3

我可能完全错了,但我的理解是,当您使用此代码进行打印时,它与打印机本身无关,而与操作系统有关。Windows 设置打印队列,将输出放入其中,然后您的代码返回。

然后 Windows 将项目从队列中取出并通过打印机驱动程序发送到您的打印机。如果打印出现错误,它应该在打印队列中显示为失败的文档。我认为在这个阶段将错误作为异常捕获为时已晚。

如果我错了,请纠正我。

于 2013-11-08T03:38:40.157 回答
0

我将使用Try/Catch 块包围您的方法主体,然后处理catch每个方法中的异常。举个例子:

public class Test
{
    private Font printFont;
    private List<string> _documentLinesToPrint = new List<string>();

    public void Run()
    {
        try
        {
            _documentLinesToPrint.Add("Test1");
            _documentLinesToPrint.Add("Test2");
            printFont = new Font("Arial", 10);
            var pd = new PrintDocument();
            pd.DefaultPageSettings.Margins = new Margins(25, 25, 25, 25);
            pd.DefaultPageSettings.PaperSize = new PaperSize("Label", 400, 237);

            var printerSettings = new System.Drawing.Printing.PrinterSettings();
            printerSettings.PrinterName = "Brother QL-570 LE";
            pd.PrinterSettings = printerSettings;
            pd.PrinterSettings.Copies = 1;
            pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
            pd.Print();
        }
        catch (InvalidPrinterException exc)
        {
            // handle your errors here.
        }

        catch (Exception ex)
        {
            // handle your errors here.
        }
    }

    // The PrintPage event is raised for each page to be printed. 
    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        try
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
               printFont.GetHeight(ev.Graphics);

            // Print each line of the file. 
            while ((count < linesPerPage) && (count < _documentLinesToPrint.Count))
            {
                line = _documentLinesToPrint[count];

                yPos = topMargin + (count *
                   printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                   leftMargin, yPos, new StringFormat());

                line = null;
                count++;
            }

            // If more lines exist, print another page. 
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
        }
        catch (InvalidPrinterException exc)
        {
            // handle your errors here.
        }

        catch (Exception ex)
        {
            // handle your errors here.
        }
    }
}
于 2013-11-07T22:00:40.763 回答