Visual Studio 2012 C# Print more than one page

Posted on Friday, May 10, 2013



I have a need to print out some documents programmatically using .Net and C#.   Here are my notes on how to get basic printing working in C#.

This document just goes over printing on more than one page




Set up a Virtual Printer


Before I starting writing this program I downloaded and installed a virtual printer.  In the past I have used a CUPS printer on my Mac but for this time around I looked around for something to run directly on my Windows 7 machine.  I found CutePDF at http://www.cutepdf.com/Products/CutePDF/writer.asp [1]
I installed this virtual server so I could test my printing without using up so much paper.


Before I go into my instructions there is a good YouTube video that goes over how to do basic printing in visual studio, using Visual basic.
http://youtu.be/m8Hz69jnae8 [2] It's not to hard to use this video to figure out how to do it in C#

Create a basic Windows Form Application





From Visual Studio 2012 Professional click on New project (or from the menu select File -->  New --> Project





  1. Select Windows Forms Application
  2. I named mine PrintDocumentWithMultiplePagesTestApp
  3. Click OK





Add a button to the program, I updated the button's text to "Print"






From the ToolBox search for print.    The two print tools that will be used for this simple tutorial are PrintDocument and PrintPreviewDialog.








Drag and drop one PrintPrevieDialog and One PrintDocument tool to the Form.   They will show up on the bottom of the design window.








Double click on PrintDocument1






This will open the code were you can define how the document will be printed.




Microsoft has a good page explaining how the PrintPageEvent works to print more than one page at http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage.aspx [3]

Basically you need to use the PrintPageEventArgs HasMorePages value.  If you set this to true the print event will be called again until it is set to false


Here is the code I wrote for the document creation

    public partial class Form1 : Form {
        private int currentPage = 1;

        public Form1() {
            InitializeComponent();
        }

        private void printDocument1_PrintPage(object sender,
                 System.Drawing.Printing.PrintPageEventArgs e) {
            Graphics g = e.Graphics;
            Font font = new Font("Microsoft Sans Serif", 24F,
                             System.Drawing.FontStyle.Bold,
                             System.Drawing.GraphicsUnit.Point,
                             ((byte)(0)));

            if (this.currentPage == 1) {
                g.DrawString("First Page", font,
                        System.Drawing.Brushes.Black, 100, 50);
                this.currentPage++;
                e.HasMorePages = true;
            }
            else if (this.currentPage == 2) {
                g.DrawString("Second Page", font,
                        System.Drawing.Brushes.Red, 100, 50);
                this.currentPage = 1;
                e.HasMorePages = false;
            }
        }





This will Document will draw the string "First Page" on the first page then call the print page event again and print the second page with "Second Page" on it.


Now this document is ready to print but it needs to actually be printed.





Double Click on the Button to create the Button click event for this button


        private void button1_Click(object sender, EventArgs e) {
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
            this.currentPage = 1;
        }


This code will assign the printDocument1 to the printPreviewDialog1 and open it in a dialog.




Test it





Click Start and run the program




Click print








This is the PrintPreviewDialog.    It’s a basic print preview tool.  You can feed it a PrintDocument and it will display it and give you some basic tools to zoom, go through pages, and print it.





Click on the "Two Pages" button and you can see both pages side by side.






Click the Print button

It will immediately try to print on your default printer (this basic print preview does not have an option for selecting printers)

For some reason you have to click the screen again to open the CudePDF dialog





This is the CutePDF dialog.   Give the document a name and location and click Save.






Here is a screen capture of the PDF created when I printed to the CutePDF.

As another test I actually printed it out and I obtained the same results on a piece of paper.





References
[1]        Cute PDF    
                Accessed 05/2013
[2]        Cute PDF
            http://youtu.be/m8Hz69jnae8
                Accessed 05/2013
[3]        PrintDocument.PrintPage Event
                Accessed 05/2013 

No comments:

Post a Comment