我正在使用PrintDocument类打印给我的兄弟标签打印机。当我执行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;
    }
}
有帮助吗?

解决方案

PrintDocument是一个非常基本的API。您会得到简单的通用打印,但它是以降低功能的成本而不是特定于打印驱动程序的。我的HP打印机通常会给我打印错误,而不是例外。看到您发生的类似事物也就不足为奇了。

眨眼可能是您可以查找的代码。如果失败,您可以尝试保存到图像格式,PDF或XPS。或使用第三方库或自己写自己的 PCL文件. 。有很多选择。创建一个输出,您可以在内存中查看,而不是在内存中进行调试计算。您可以查看PDF,看看它看起来是否古怪。只要记住它在PC上的外观可能与输出略有不同,尤其是在边缘附近打印时。

其他提示

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

然后,Windows将项目从队列中取出,并通过打印机驱动程序和打印机发送它们。如果打印错误,则应在打印队列中显示为失败的文档。我认为在此阶段将错误作为一个例外为时已晚。

如果我误会,请纠正我。

我会用一个 尝试/捕获块 然后处理在 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.
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top