كيف يمكنني التقاط الخطأ من الطابعة الخاصة بي مع PrintDocument؟

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

سؤال

أنا أستخدم فئة PrintDocument للطباعة على طابعة Love Label. عندما أقوم بتنفيذ طريقة 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 هو واجهة برمجة تطبيقات أساسية للغاية. تحصل على طباعة عامة بسيطة ، ولكنها تأتي على حساب الوظائف المخفضة غير خاصة بسائق الطباعة. عادة ما تعطيني طابعة HP خطأ مطبوع بدلاً من استثناء. ليس من المستغرب رؤية شيء مشابه يحدث لك.

من المحتمل أن يكون الوامض رمزًا يمكنك البحث عنه. إذا فشل ذلك ، فيمكنك محاولة حفظ تنسيق صورة أو PDF أو XPS. أو استخدم مكتبة الطرف الثالث أو اكتب خاصتك ملف PCL. هناك الكثير من الخيارات. إن إنشاء مخرج يمكنك عرضه بدلاً من واحد في الذاكرة يجب أن يصحح الحسابات مثل الهوامش. يمكنك إلقاء نظرة على ملف PDF ومعرفة ما إذا كان يبدو أحمق. فقط ضع في اعتبارك الطريقة التي ينظر بها على الكمبيوتر قد تكون مختلفة قليلاً عن الإخراج وخاصة عند الطباعة بالقرب من الحواف.

نصائح أخرى

قد أكون مخطئًا تمامًا في هذا الأمر ، لكن ما أفهمه هو أنه عندما تقوم بالطباعة باستخدام هذا الرمز ، لا علاقة له بالطابعة نفسها ، ولكن مع نظام التشغيل. يقوم 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