簡體   English   中英

如何使用PrintDocument從打印機中捕獲錯誤?

[英]How do I catch the error from my printer with PrintDocument?

我正在使用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;
    }
}

我可能完全錯了,但是我的理解是,當您使用此代碼進行打印時,它與打印機本身無關,而與操作系統無關。 Windows設置一個打印隊列,將輸出放入其中,然后代碼返回。

然后,Windows將項目從隊列中取出,並通過打印機驅動程序將其發送到打印機。 如果打印有錯誤,它將在打印隊列中顯示為失敗的文檔。 我認為在此階段將錯誤作為例外捕獲為時已晚。

如果我弄錯了,請糾正我。

PrintDocument是一個非常基本的API。 您可以進行簡單的常規打印,但是這樣做的代價是功能的減少並非特定於打印驅動程序。 我的HP打印機通常會給我打印錯誤而不是異常。 看到類似的事情發生在您身上也就不足為奇了。

閃爍可能是您可以查詢的代碼。 如果失敗,則可以嘗試保存為圖像格式(PDF或XPS)。 或使用第三方庫或編寫自己的PCL文件 有很多選擇。 如果調試諸如邊距之類的計算,則創建一個可以查看的輸出而不是內存中的輸出。 您可以查看PDF,看它是否古怪。 請記住,在PC上的外觀可能與輸出略有不同,尤其是在邊緣附近打印時。

我會使用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.
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM