I have 3 printing methods: printa, printb , and printc (each printing a form) . All the 3 forms need to be printed across multiple pages. How can I print across 3 pages?
I know that we need to use the the event handler and use currentpa开发者_如何学JAVAge and e.hasmorepages but I'm not sure how to use it.
Simply keep track of the page number. You set it to 0 in the BeginPrint event and increment it every time that PrintPage is called. Like this:
int pageNumber;
private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
pageNumber = 0;
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
++pageNumber;
e.HasMorePages = true;
switch (pageNumber) {
case 1: printa(e); break;
case 2: printb(e); break;
case 3: printc(e); e.HasMorePages = false; break;
}
}
Yeah, it can be pretty obscure. Here's how I did it for printing Reporting Services reports (the MetaFile image array in the code below).
public void Print()
{
if (emfImage == null || emfImage.Count <= 0)
{
throw new ArgumentException("An image is required to print.");
}
printer = printer.Trim();
if (string.IsNullOrEmpty(printer))
{
throw new ArgumentException("A printer is required.");
}
printingPage = 0;
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = printer;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
Metafile page = emfImage[printingPage];
e.Graphics.DrawImage(page, 0, 0, page.Width, page.Height);
e.HasMorePages = ++printingPage < emfImage.Count;
}
精彩评论