PrintDocumentでプリンターからエラーをキャッチするにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/19847420

質問

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