Printing and Previewing multiple pages in C#






4.62/5 (19 votes)
In this article I will explain how to print documents in multiple pages in windows application using C#.
Introduction
In this article I will explain how to print documents in multiple pages in windows application using C#.
Background
To specify the output to print, use the Graphics
property of the PrintPageEventArgs
and PrintPage event handler of PrintDocument
. The Graphics
class provides methods
for drawing objects (graphics or text) to a device, such as a screen or
printer. This Graphics
Property calculates length, lines per page of a document.
For example, to specify a line of text that should be printed, draw the text
using the Graphics.DrawString
method.
Using the code
To print multiple pages , I have taken one button, oneprintDocument
, one printPreviewDialog
and one printDialog
by drag and drop from toolbox.
Here 50 numbers will be printed in three pages. Among them first 20 number will be printed in first page, next 20 in second one and rest of numbers will be printed in last page.
The numbers will be printed one by one with some line
spacing in between them. After each page is drawn, check whether the count is 20
or not, if the count is more than 20 then set the HasMorePages
property of the PrintPageEventArgs
to true.
//Declaration the global variables
PaperSize paperSize = new PaperSize("papersize", 150, 500);//set the paper size
int totalnumber = 0;//this is for total number of items of the list or array
int itemperpage= 0;//this is for no of item per page
//PrintPreview button_click definition
private void btnprintpreview_Click(object sender, EventArgs e)
{
//here we are printing 50 numbers sequentially by using loop.
//For each button click event we have to reset below two variables to 0
// because every time PrintPage event fires automatically.
itemperpage = totalnumber = 0;
printPreviewDialog1.Document = printDocument1;
((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Enabled
= false;//disable the direct print from printpreview.as when we click that Print button PrintPage event fires again.
printDocument1.DefaultPageSettings.PaperSize = paperSize;
printPreviewDialog1.ShowDialog();
}
//Print button_click definition
private void bnprint_Click(object sender, EventArgs e)
{
itemperpage = totalnumber = 0;
printDialog1.Document =printDocument1;
printDocument1.DefaultPageSettings.PaperSize = paperSize;
printDialog1.ShowDialog();
}
//Define the Printpage event of the printdocument
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float currentY = 10;// declare one variable for height measurement
e.Graphics.DrawString("Print in Multiple Pages", DefaultFont, Brushes.Black, 10, currentY);//this will print one heading/title in every page of the document
currentY += 15;
while(totalnumber <= 50) // check the number of items
{
e.Graphics.DrawString(totalnumber.ToString(),DefaultFont, Brushes.Black, 50,currentY);//print each item
currentY += 20; // set a gap between every item
totalnumber += 1; //increment count by 1
if(itemperpage < 20) // check whether the number of item(per page) is more than 20 or not
{
itemperpage += 1; // increment itemperpage by 1
e.HasMorePages = false; // set the HasMorePages property to false , so that no other page will not be added
}
else // if the number of item(per page) is more than 20 then add one page
{
itemperpage = 0; //initiate itemperpage to 0 .
e.HasMorePages = true; //e.HasMorePages raised the PrintPage event once per page .
return;//It will call PrintPage event again
}
}
}
The out put will be like this
Points of Interest
The main logic behind the trick is that you need to work out how much space you have on the page and ensure that you must track item collection and page height.