3
我在嘗試弄清c#打印多個頁面時遇到困難。我的應用程序創建一個包含1到10個元素的對象類型列表。每個對象包含2個字符串屬性:docTypeNumber和docTypeDescription。一個名爲flightnumber的變量也被傳入類構造函數。每個實例都是一種文檔類型,必須打印爲單獨的條形碼錶,其中包含文檔類型編號,描述和航班號。大多數多頁打印示例都是將文檔「溢出」到多個頁面上,而不是由多個單獨頁面組成的頁面。我的問題是如何實現這一點。需要幫助從列表中打印多個頁面
我是否需要創建一個大文檔,並將其泄漏到多個頁面上? 我是否必須創建PrintDocument類的多個實例?
任何幫助將不勝感激。
這裏是我的代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace BarcodeTest
{
class BarcodePrinter
{
public BarcodePrinter(List<DocumentType> type, string flightnumber)
{
docType = type;
flightNumber = flightnumber;
}
//Attributes
private List<DocumentType> docType = new List<DocumentType>();
private string flightNumber;
//helper variables
string docTypeNumber;
string docTypeDescription;
int pageNumber = 1;
int numberOfPages;
private static Font barcodeFont = new Font("3 of 9 Barcode", 24);
private static Font printFont = new Font("Arial", 24);
public void Print()
{
numberOfPages = docType.Count;
PrintDocument pd = new PrintDocument();
foreach (DocumentType type in docType)
{
docTypeNumber = type.DocumentTypeNumber;
docTypeDescription = type.DocumentDescription;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
}//end foreach
#if DEBUG
PrintPreviewDialog printPreview = new PrintPreviewDialog();
printPreview.Document = pd;
printPreview.Show();
#else
pd.Print();
#endif
}// end Print() method
public void pd_PrintPage(Object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
//e.Graphics.PageUnit = GraphicsUnit.Point;
e.Graphics.PageUnit = GraphicsUnit.Inch;
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
Brush br = new SolidBrush(Color.Black);
RectangleF rec1 = new RectangleF(1.9375f, 0f, 4, 1);
RectangleF rec2 = new RectangleF(1.9375f, .5f, 4, 1);
RectangleF rec3 = new RectangleF(1.9375f, 1f, 4, 1);
RectangleF rec4 = new RectangleF(1.9375f, 2, 4, 1);
RectangleF rec5 = new RectangleF(1.9375f, 2.5f, 4, 1);
g.DrawString("Air - " + docTypeDescription, printFont, br, rec1, stringFormat);
g.DrawString("*" + docTypeNumber + "*", barcodeFont, br, rec2, stringFormat);
g.DrawString(docTypeNumber, printFont, br, rec3, stringFormat);
g.DrawString("*" + flightNumber + "*", barcodeFont, br, rec4, stringFormat);
g.DrawString(flightNumber, printFont, br, rec5, stringFormat);
if (pageNumber < numberOfPages)
{
e.HasMorePages = true;
}
else
e.HasMorePages = false;
pageNumber++;
}//end pd_PrintPage Method
}//end BarcodePrinter Class
}//end namespace