如何在另一页打印

时间:2014-08-09 16:48:17

标签: c# .net printing

这是我打印数据的代码:

private void document_PrintPage(object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
    {

            System.Drawing.Font valueFont = new System.Drawing.Font("Arial", 15, System.Drawing.FontStyle.Regular);
            string dataToPrint = "";
            string valuesToPrint = "";

        dataToPrint += "رقم بطاقة التسويق" +  Environment.NewLine+"-----------------------------------------------"+Environment.NewLine;
        valuesToPrint += farmID.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("إسم صاحب المزرعة") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += farmOwner.Text + Environment.NewLine + Environment.NewLine;

            dataToPrint += ("رقم المزرعة") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
            valuesToPrint += farmNumber.Text + Environment.NewLine + Environment.NewLine;

            dataToPrint += ("الموبايل") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
            valuesToPrint += farmMobile.Text + Environment.NewLine + Environment.NewLine;

            dataToPrint += ("الإمارة") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
            valuesToPrint += emaraNameArabic.Text + Environment.NewLine + Environment.NewLine;

            dataToPrint += ("المنطقة") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
            valuesToPrint += areaNameArabic.Text + Environment.NewLine + Environment.NewLine;

            dataToPrint += ("الإقليم") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
            valuesToPrint += regionNameArabic.Text + Environment.NewLine + Environment.NewLine;

            dataToPrint += ("مركز الإستلام") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
            valuesToPrint += receivingCenterName.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("خلاص") + khalas.Text + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += khalas.Text + Environment.NewLine + Environment.NewLine;

       dataToPrint += ("فرض") + fardh.Text + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
       valuesToPrint += fardh.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("لولو") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += otherLulu.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("خنيزي") + khedrawi.Text + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += khedrawi.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("بومعان") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += boumaan.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("برحي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += barhi.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("جش خزمة") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += jeshKharma.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("رزيز") + reziz.Text + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += reziz.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("جبري") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += jabri.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("عنبرة المدينة") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += anbaretAlMadina.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("شيشي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += shishi.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("دباس") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += dabbas.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("نبتة سيف") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += nabtetSaif.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("خضراوي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += khedrawi.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("هلالي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += hilali.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("مكتومي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += maktoumy.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("نميشي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += namishi.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("سلطانة") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += sultanah.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("بقلة الطوعة") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += baqlatAlTawaa.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("بقلة الدحالة") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += baqlatAlDahla.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("بقلة الرارنجا") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += baqlatAlRarenja.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("سكري") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += sukary.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("صقعي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += saqei.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("بوزيد") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += abuZebed.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("مجدول") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += majdoul.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("شبيبي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += shabibi.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("يواني") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += youwani.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("يردي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += yardi.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("خدي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += khadi.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("حاتمي") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += hatimi.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("نغال") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += neghal.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("مجموعة مساير") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += otherSayer.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("إجمالي مثمر") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += totalFructiferous.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("إجمالي غير مثمر") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += totalUnFructiferous.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("إجمالي الأفحل") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += totalAfhal.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("الإجمالي العام") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += generalTotal.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("كبير المعرفين") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += seniorSupervisors.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("مساعد") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += assistant.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("مدخل البيانات") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += dataEntry.Text + Environment.NewLine + Environment.NewLine;

        dataToPrint += ("صاحب المزرعة أو من ينوب عنه") + Environment.NewLine + "-----------------------------------------------" + Environment.NewLine;
        valuesToPrint += farmOnwerOrBehalf.Text + Environment.NewLine + Environment.NewLine;

        StringFormat format = new StringFormat(StringFormatFlags.DirectionRightToLeft);
            e.Graphics.DrawString(dataToPrint, valueFont, System.Drawing.Brushes.Black, 600, 20, format);
            e.Graphics.DrawString(valuesToPrint, valueFont, System.Drawing.Brushes.Red, 400, 20, format);
    }


    // Declare the PrintDocument object.
        private System.Drawing.Printing.PrintDocument docToPrint; //=
        //new System.Drawing.Printing.PrintDocument();

    private void printButton_Click(object sender, EventArgs e)
    {
        if (validate())
        {
            PrintDialog PrintDialog1 = new PrintDialog();
            // Allow the user to choose the page range he or she would
            // like to print.
            PrintDialog1.AllowSomePages = true;

            // Show the help button.
            PrintDialog1.ShowHelp = true;

            // Set the Document property to the PrintDocument for 
            // which the PrintPage Event has been handled. To display the
            // dialog, either this property or the PrinterSettings property 
            // must be set 
            PrintDialog1.Document = docToPrint;

            DialogResult result = PrintDialog1.ShowDialog();

            // If the result is OK then print the document.
            if (result == DialogResult.OK)
            {
                docToPrint.Print();
            }
        }
    }
}

有很多数据和数据无法提供。填写一个页面,但当我解雇该代码时,我得到一个页面,其中缺少大量信息,换句话说,数据打印到一个页面,当该页面填充时,代码不写入另一个页面

1 个答案:

答案 0 :(得分:1)

关于e.HasMorePages的使用有多个答案和文章,但它们都将破坏您当前的布局方法。这无济于事。

食谱:

  1. 打印前整理数据。创建一个List<DataClass> Data,其中DataClass具有Label和Value属性。该列表必须是可索引的。

  2. 在表单上引入lastLine项目计数器作为字段(如docToPrint)

  3. 在BeginPrint事件中将lastLine设置为0

  4. 将您的Printpage事件更改为:

    var box = e.MarginBounds;
    
    //todo: calculate X, Y and H from box.
    
    while (lastLine < Data.Count)
    {
        e.Graphics.DrawStrin(Data[lastLine].Label, this.Font, Brushes.Black, X1, Y);
        e.Graphics.DrawStrin(Data[lastLine].Value, this.Font, Brushes.Navy, X2, Y);
    
        lastLine += 1;
        Y += H;
        if (Y >= box.Bottom)  // page full
            break;
    }
    e.HasMorePages = lastLine < Data.Count;
    
  5. 每页有1次打印Printpage事件。由您来跟踪下一步的状态(线路)。

    另外,请务必使用PrintPreviewDialog来节省一些时间和纸张。