True PDF Software
for you!

  • Deutsch
  • English

Setting PDF Page Size Programmatically

A great deal of users asks us how to set the page size of the PDF from your program. To answer this question we have made this small example that will show you how to do it from a C# program.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Printing;
  10.  
  11. namespace PrintForms
  12. {
  13.    public partial class Form1 : Form
  14.    {
  15.        public Form1()
  16.        {
  17.            InitializeComponent();
  18.        }
  19.        
  20.         private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  21.        {
  22.            // Output something in the print
  23.            SolidBrush myBrush = new SolidBrush(Color.Blue);
  24.            e.Graphics.DrawString("Page size:\n" + e.PageSettings.PaperSize.ToString(), Font , myBrush, e.MarginBounds.Left, e.MarginBounds.Top);
  25.            myBrush.Dispose();
  26.        }
  27.  
  28.        private void button1_Click(object sender, EventArgs e)
  29.        {
  30.            pd.PrinterSettings.PrinterName = "7-PDF Printer";
  31.            pd.DefaultPageSettings.Landscape = true;
  32.            pd.DefaultPageSettings.PaperSize = new PaperSize("My Paper", 500, 700);
  33.            pd.Print();
  34.        }
  35.    }
  36. }

Download Example Files

Example source files are included in the zip file.

AttachmentSize
Package icon Example files10.21 KB