Click here to Skip to main content
Click here to Skip to main content

Tagged as

Printing and Previewing multiple pages in C#

, 26 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
In this article I will explain how to print documents in multiple pages in windows application using C#.


In this article I will explain how to print documents in multiple pages in windows application using C#.


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, one printDocument, 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;

           = false;//disable the direct print from when we click that Print button PrintPage event fires again.

           printDocument1.DefaultPageSettings.PaperSize = paperSize;

    //Print button_click definition 

     private void bnprint_Click(object sender, EventArgs e)
          itemperpage = totalnumber = 0;
          printDialog1.Document =printDocument1; 
          printDocument1.DefaultPageSettings.PaperSize = paperSize;
  //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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Animesh Datta
Software Developer
India India
Software Developer
I love to learn, discover and master all aspects of .NET and SQL Server. I have developed some web based applications using C# in addition to the traditional ASP.NET and some Windows Forms applications and WPF .
Follow on   LinkedIn

Comments and Discussions

BugAbove code does not works fine. Prints and extra page Pinmembersajiddesigner13hrs 3mins ago 
GeneralRe: Above code does not works fine. Prints and extra page PinmemberAnimesh Datta3hrs 16mins ago 
QuestionNot Printing all pages in my case Pinmembersajiddesigner18-Oct-14 20:24 
GeneralMy vote of 4 PinmemberSusheel Tandon10-Oct-14 23:16 
GeneralRe: My vote of 4 PinmemberAnimesh Datta12-Oct-14 18:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 26 Feb 2014
Article Copyright 2014 by Animesh Datta
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid