Visual Studio 2012 C# PrintDocument Dimensions etc.

Posted on Saturday, May 18, 2013

I have a need to print out some documents programmatically using .Net and C#.    I just want to see how I can get the dimensions of the page I will be printing on.

To start off with I am using a program that can print multiple pages and select printers.  The notes on how to make this program can be found here

Get the dimensions

From the PrintPage method you can get the dimensions using e.PageBounds.Width and e.PageBounds.Width

Updating my Print Document Print Page Event to

        private void printDocument1_PrintPage(object sender,
                 System.Drawing.Printing.PrintPageEventArgs e) {
            Font font = new Font("Microsoft Sans Serif", 48F,
            Graphics g = e.Graphics;

            if (this.currentPage < 11) {
                g.DrawString("Page " + this.currentPage, font,
                        System.Drawing.Brushes.Black, 100, 50);
                Pen redPen = new Pen(Color.Red, 10);
                g.DrawRectangle(redPen, 5, 5,
                         e.PageBounds.Width - 10,
                         e.PageBounds.Height - 10);
                e.HasMorePages = (this.currentPage < 11);
            if (!e.HasMorePages) {
                currentPage = 1;

You can see that the Red Rectangle is drawn around the border.

If I go to landscape you can see the rectangle is drawn correctly.


No comments:

Post a Comment