如何在另一页打印
本文关键字:一页 打印 | 更新日期: 2023-09-27 18:17:11
这是我打印数据的代码:
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();
}
}
}
}
有很多数据和数据不能在一页填写,但是当我触发该代码时,我得到了一个有很多信息缺失的单页,换句话说,数据被打印到一页,当该页填充时,代码不写入另一页
关于e.HasMorePages
的使用有很多答案和文章,但它们都会破坏你当前的布局方法。那是没办法的。
配方:
-
打印前整理数据。制作一个
List<DataClass> Data
,其中数据类具有标签和值属性。这个列表必须是可索引的。 -
引入
lastLine
项目计数器作为表单上的字段(如doctopprint) -
在BeginPrint事件中设置
lastLine
为0 -
将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;
每个页面有一个对Printpage事件的调用。接下来的状态(线)由您来跟踪。
另外,确保使用PrintPreviewDialog来节省一些时间和纸张。