Visual Studio 2012 C# Basic Printing

Posted on Saturday, May 4, 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#.





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 BasicPrintingTestApp
  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 PrintDialog and PrintDocument.








Drag and drop one PrintDialog 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.



Here is the code I wrote for the document creation


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

            g.DrawString("Print Me", font,
                     System.Drawing.Brushes.Black, 100, 50);
            g.DrawString("Smaller Red Print", font2,
                     System.Drawing.Brushes.Red, 400, 200);
        }



This document creates 2 fonts, the first one is bold with a size of 24 and the second font is regular with a size of 10.

Then the program draws a Black text string that says "Print Me" at location 100,50
Then the program draws a Red text string that says "Smaller Red Print" at location 400, 200


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) {
            DialogResult dr = printDialog1.ShowDialog();

            if (dr == DialogResult.OK) {
                printDocument1.Print();
                MessageBox.Show("Printing Complete");
            }
        }


This code will open the printDialog1 and when it Print button on the dialog has been pressed will return a DialogResult.OK and then it prints the document by running printDocument1.Print();




Test it



Click Start and run the program






Click print






This is the PrintDialog.   Here I selected the virtual CutePDF printer and click Print.  To print it.



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.




Printing Complete.





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.




A few more tests


I wanted to test drawing a few more things on the screen here are a few examples of what I created.


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

            g.DrawString("Print Me", font,
                     System.Drawing.Brushes.Black, 100, 50);
            g.DrawString("Smaller Red Print", font2,
                     System.Drawing.Brushes.Red, 400, 200);

            g.DrawLine(Pens.Blue, 0, 0, 80, 80);
        }


Draws a blue line from (0,0) to (80,0)








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

            g.DrawString("Print Me", font,
                     System.Drawing.Brushes.Black, 100, 50);
            g.DrawString("Smaller Red Print", font2,
                     System.Drawing.Brushes.Red, 400, 200);

             Pen bluePen = new Pen(Color.Blue, 10);
             g.DrawLine(bluePen, 0, 0, 80, 80);
        }


Thick Blue Pen





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

            g.DrawString("Print Me", font,
                     System.Drawing.Brushes.Black, 100, 50);
            g.DrawString("Smaller Red Print", font2,
                     System.Drawing.Brushes.Red, 400, 200);

            Pen orangePen = new Pen(Color.Orange, 10);
            orangePen.StartCap = LineCap.Round;
            orangePen.EndCap = LineCap.Round;
            g.DrawLine(orangePen, 40, 40, 80, 80);
        }


Thick Orange Pen, from (40,40) to (80,80) with rounded ends





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

            g.DrawString("Print Me", font,
                     System.Drawing.Brushes.Black, 100, 50);
            g.DrawString("Smaller Red Print", font2,
                     System.Drawing.Brushes.Red, 400, 200);

            g.DrawRectangle(Pens.Black, 50, 50, 200, 200);                 
        }



Black Rectangle at (50,50) to (200,200)




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

            g.DrawString("Print Me", font,
                     System.Drawing.Brushes.Black, 100, 50);
            g.DrawString("Smaller Red Print", font2,
                     System.Drawing.Brushes.Red, 400, 200);

            Pen redPen = new Pen(Color.Red, 10);
            g.DrawRectangle(redPen, 50, 50, 200, 200);
        }


Red Rectangle at (50,50) to (200,200) with thick line





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

            g.DrawString("Print Me", font,
                     System.Drawing.Brushes.Black, 100, 50);
            g.DrawString("Smaller Red Print", font2,
                     System.Drawing.Brushes.Red, 400, 200);

            SolidBrush redBrush = new SolidBrush(Color.Red);
            g.FillRectangle(redBrush, 50, 50, 200, 200);
        }


Solid Red Rectangle at (50,50) to (200,200)




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

            g.DrawString("Print Me", font,
                     System.Drawing.Brushes.Black, 100, 50);
            g.DrawString("Smaller Red Print", font2,
                     System.Drawing.Brushes.Red, 400, 200);

            Pen purplePen = new Pen(Color.Purple, 3);
            g.DrawEllipse(purplePen, 50, 50, 200, 200);
        }


Purple Circle (ellipse) at (50,50) to (200,200)






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

            g.DrawString("Print Me", font,
                     System.Drawing.Brushes.Black, 100, 50);
            g.DrawString("Smaller Red Print", font2,
                     System.Drawing.Brushes.Red, 400, 200);

            Image drawImage = Image.FromFile("C:/Users/patman/Desktop/photo.png");
            g.DrawImage(drawImage, 300, 300);
        }


Draw an image from a file at (300, 300)




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

No comments:

Post a Comment