문제

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;
    }
}
도움이 되었습니까?

해결책

인쇄 문서는 매우 기본적인 API입니다. 간단한 일반 인쇄를 얻지 만 인쇄 드라이버에만 해당되지 않는 기능 감소 비용이 발생합니다. 내 HP 프린터는 일반적으로 예외가 아닌 인쇄 오류를 제공합니다. 당신과 비슷한 일이 일어나는 것을 보는 것은 놀라운 일이 아닙니다.

깜박임은 조회 할 수있는 코드 일 것입니다. 실패하면 이미지 형식, PDF 또는 XPS로 저장을 시도 할 수 있습니다. 또는 제 3 자 도서관을 사용하거나 직접 작성하십시오. PCL 파일. 많은 옵션이 있습니다. 메모리와 달리 출력을 생성하면 여백과 같은 계산을 디버깅해야합니다. PDF를보고 엉뚱한 것처럼 보일 수 있습니다. PC에서 보이는 방식은 특히 가장자리 근처에서 인쇄 할 때 출력과 약간 다를 수 있습니다.

다른 팁

나는 이것에 대해 완전히 잘못 될 수 있지만, 내 이해는이 코드로 인쇄 할 때 프린터 자체와 관련이 없지만 운영 체제와 관련이 없다는 것입니다. Windows는 인쇄 큐를 설정하고 출력을 배치하고 코드가 반환됩니다.

그런 다음 Windows는 대기열에서 항목을 제거하여 프린터 드라이버와 프린터로 보냅니다. 인쇄에 오류가 있으면 인쇄 대기열에 실패한 문서로 표시됩니다. 이 단계에서 예외로 오류를 가두기에는 너무 늦었다 고 생각합니다.

내가 착각했다면 저를 바로 잡으십시오.

나는 당신의 방법 본체를 사용하여 a를 둘러 쌀 것입니다 시도/캐치 블록 그런 다음 내에서의 예외를 처리하십시오 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