Click here to Skip to main content
15,886,565 members
Articles / Programming Languages / C#
Tip/Trick

Printing and Previewing multiple pages in C#

Rate me:
Please Sign up or sign in to vote.
4.62/5 (19 votes)
26 Feb 2014CPOL1 min read 98.4K   29   18
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, one printDocument, one printPreviewDialog and one printDialog by drag and drop from toolbox.

Image 1

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.

C#
//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 
C#
    //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();

       } 
 
C#
//Print button_click definition

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

Image 2

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.

License

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


Written By
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 .

Comments and Discussions

 
GeneralMy vote of 5 Pin
Yusuf Aksoy4-Apr-21 10:50
Yusuf Aksoy4-Apr-21 10:50 
Questionthxs Pin
shostakovich29-Apr-20 15:00
shostakovich29-Apr-20 15:00 
QuestionHas more page event Pin
Hailu Worku Obsse9-Dec-19 20:23
professionalHailu Worku Obsse9-Dec-19 20:23 
Questionİs working Pin
Member 985220029-Oct-18 22:38
Member 985220029-Oct-18 22:38 
QuestionOverprinting of ellipse on ellipse in print preview c# Pin
Member 872711826-Dec-17 22:16
Member 872711826-Dec-17 22:16 
AnswerRe: Overprinting of ellipse on ellipse in print preview c# Pin
Animesh Datta27-Dec-17 0:48
Animesh Datta27-Dec-17 0:48 
BugSmall error in code Pin
Perić Željko25-Sep-17 18:55
professionalPerić Željko25-Sep-17 18:55 
GeneralRe: Small error in code Pin
Animesh Datta8-Oct-17 20:41
Animesh Datta8-Oct-17 20:41 
QuestionWorking in the preview section but not in the print section. Pin
Member 129378086-Jan-17 5:29
Member 129378086-Jan-17 5:29 
AnswerRe: Working in the preview section but not in the print section. Pin
Steve Neugebauer28-Sep-17 6:53
Steve Neugebauer28-Sep-17 6:53 
GeneralRe: Working in the preview section but not in the print section. Pin
Animesh Datta8-Oct-17 21:10
Animesh Datta8-Oct-17 21:10 
GeneralMy vote of 4 Pin
DaveyM6928-Apr-16 2:20
professionalDaveyM6928-Apr-16 2:20 
GeneralRe: My vote of 4 Pin
Animesh Datta28-Apr-16 19:30
Animesh Datta28-Apr-16 19:30 
BugAbove code does not works fine. Prints and extra page Pin
sajiddesigner21-Oct-14 9:27
sajiddesigner21-Oct-14 9:27 
GeneralRe: Above code does not works fine. Prints and extra page Pin
Animesh Datta21-Oct-14 19:14
Animesh Datta21-Oct-14 19:14 
QuestionNot Printing all pages in my case Pin
sajiddesigner18-Oct-14 20:24
sajiddesigner18-Oct-14 20:24 
GeneralMy vote of 4 Pin
Susheel Tandon10-Oct-14 23:16
Susheel Tandon10-Oct-14 23:16 
GeneralRe: My vote of 4 Pin
Animesh Datta12-Oct-14 18:50
Animesh Datta12-Oct-14 18:50 

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

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