PageSetupDialog in C#

This article has been excerpted from book "Graphics Programming with GDI+".

The PageSetupDialog class represents the PageSetupDialog control in the .NET Framework library. This class represents a standard Windows page setup dialog that allows users to manipulate page settings, including margins and paper orientation. Users can also set a PageSettings object through PageSetupDialog's PageSettings property. Table 11.8 describes the properties of the PageSetupDialog class. All of these properties have both get the set options.

As with PrintDialog, the PageSetupdialog class has a Reset method that resets all the default values for the dialog.

Listing 11.29 creates a PageSetupDialog object, sets its properties, calls ShowDialog, and prints the document.

TABLE 11.8: PageSetupDialog properties




Indicates whether the margins section of the dialog box is enabled. By default, true when a PageSetupDialog object is created.


Indicates whether the orientation section of the dialog box (landscape versus portrait) is enabled. By default, true when a PageSetupDialog object is created.


Indicates whether the paper section of the dialog box (paper size and paper source) is enabled. By default, true when a PageSetupDialog object is created.


Indicates whether the Printer button is enabled. By default true when a PageSetupDialog object is created.


Identifies the PrintDocument object from which to get page settings. By default, null when a PageSetupDialog object is created.


Indicates the minimum margins the user is allowed to select, in hundredths of an inch. By default, null when a PageSetupDialog object is created.


Identifies the page settings to modify. By default, null when a PageSetupDialog object is created.


Identifies the printer settings that the dialog box will modify when the user clicks the Printer button. By default null when a PageSetupDialog object is created.


Indicates whether the Help button is visible. By default, false when a PageSetupDialog object is created.


Indicates whether the Network button is visible. By default, true when a PageSetupDialog object is created.

LISTING 11.29: Creating and using the PageSetupDialog control

            setupDlg = new PageSetupDialog();
            printDlg = new PrintDialog();
            printDoc = new PrintDocument();
            printDoc.DocumentName = "Print Document";

            //PageSetupDialog settings
            setupDlg.Document = printDoc;
            setupDlg.AllowMargins = false;
            setupDlg.AllowOrientation = false;
            setupDlg.AllowPaper = false;
            setupDlg.AllowPrinter = false;

            if (setupDlg.ShowDialog() == DialogResult.OK)
                printDoc.DefaultPageSettings =
                printDoc.PrinterSettings =

The PrintPreviewDialog Control

The PrintePreviewDialog class represents the PrintPreviewDialog control in the .NET Framework library. This class represents a standard Windows print preview dialog, which allows users to preview capabilities before printing. The PrintPreviewDialog class is inherited from the Form class, which means that this dialog contains all the functionality defined in Form, Control, and other base classes.

In addition to the properties provided by the base classes, this class has its own properties. Many of these properties are very common and are provided by many controls. Table 11.9 describers a few important PrintPreviewDialog class properties. All of these properties have both get and set options.

Listing 11.30 creates a PrintPreviewDialog object, sets its properties calls ShowDialog, and prints the document.

LISTING 11.30: Create and using the PrintPreviewDialog control

            //Create a PrintPreviewDialog object
            PrintPreviewDialog previewDlg =
            new PrintPrevioeDialog();

            //Create a PrintDocument object
            PrintDocument printDoc =
            new PrintDocument();

            //Set Document property
            previewDlg.Document = printDoc;
            previewDlg.WindowsState =
            //show Dialog

TABLE 11.9: Some PrintPreviewDialog properties




Identifies the document shown in preview.


Indicates whether a help button should be displayed in the caption box of the form. The default value is false.


Indicates whether the form will receive key events before the event is passed to the control that has focus. The default value is false.


Indicates whether the form is displayed in the Windows taskbar. The default value is true.


Identifies the color that will represent transparent areas of the form.


Indicates whether printing uses the anti-aliasing features of the operating system.


Identifies the form's window state.

Print Dialogs in Action

Now let's create a Windows application. In this application you will see how to use the print dialogs in your Windows applications.

We create a Windows application and add a MainMenu control to the form. We also add four menu items and a separator to the MainMenu control. The final form looks like Figure 11.17.

Figure 11.17.jpg

FIGURE 11.17: The print dialog application

As usual, our first step is to add some private variable to the project, as follows:


Image curImage = null;
string curFileName = null;
PrintPreviewDialog previewDlg = null;
PageSetupDialog setupDlg = null;
PrintDocument printDoc = null;
PrintDialog printDlg = null;

//We also add the following namespace to the project:


On our forms' load event, we initialize these dialogs. We also create a PrintPage event handler and add it to the PrintDocument object, as shown in Listing 11.31.

LISTING 11.31: Initializing print dialogs

        private void Form1_Load(object sender, System.EventArgs e)
            //Create print preview dialog and other dialogs
            previewDlg = new PrintPreviewDialog();
            setupDlg = new PageSetupDialog();
            printDoc = new PrintDocument();

            //Set document name
            printDoc.DocumentName = "Print Document";

            //PrintPreviewDialog settings
            previewDlg.Document = printDoc;

            //PageSetupDialog settings
            setupDlg.Dcoument = printDoc;

            //PrintDialog settings
            printDlg.Document = printDoc;
            printDlg.AllowSelection = true;
            printDlg.AllowSomePages = true;

            //Create a PrintPage event handler
            printDoc.PrintPage +=
            new PrintPageEventHandler(this.pd_Print);

Now we add the PrintPage event handler, which calls DrawGraphicsItems as shown in Listing 11.32. We pass PrintPageEventArgs.Graphics as the only parameter to DrawGraphicsItems.

LISTING 11.32: The PrintPage event handler

        private void pd_Print(object sender, PrintPageEventArgs ppeArgs)

The DrawGraphics Items method draws an image and text on the printer or the form, depending on the Graphics object. If we pass Form.Graphics, the DrawGraphicsItems method will draw graphics objects on the form, but if we pass PrintPageEventArgs.Graphics, this method will send drawings to the printer.

The code for the DrawGraphicsItems method is given in Listing 11.33. This method also sets the smoothing mode and text qualities via the SmoothingMode and TextRenderingHint properties. After that it calls DrawImage and DrawText.

LISTING 11.33: The DrawGraphicsItems method

        private void DrawGraphicsItems(Graphics gobj)
            //Set text and image quality
            gobj.SmoothingMode =
            gobj.TextRenderingHint = TextRenderingHint.AntiAlias;

            if (curImage != null)
                //Draw image using the DrawImage method
                CurImage.Width, curImage.Height);

            //Draw a string
            gobj.DrawString(""Printing Dialogs Text",
            new Font("Verdana", 14),
            new SolidBrush(color.Blue), 0, 0);

There's just one more thing to do before we write the menu item event handlers. We call DrawGraphicsItems from the form's paint event handler, as Listing 11.34 shows. Adding this code will display the drawing on the form.

LISTING 11.34: The form's paint event handler

        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)

Now we can write code for the menu items. The Open File menu item just let use browse images and creates an Image object by calling the Image.FromFile method, as Listing 11.35 shows.

LISTING 11.35: The Open File menu handler

        private void OpenFile_Click(object sender, System.EventArgs e)
            //Create a Graphics object
            Graphics g = this.CreateGraphics();

            //Create open file dialog
            OpenFileDialog openDlg = new OpenFileDialog();

            //Set filter as image
            OpenDlg.Filter =
            "All Image files | *.bmp; *.gif; *.jpg; *.ico;" +
            "*.emf, .wmf | Bitmap Files (.bmp; *.gif; *.jpg;" +
            "*.ico) | *.bmp; *.gif; *.jpg; *.gif; *.jpg; *.ico" +
            "Meta Files (*.emf; *.wmf) | *.emf; *.wmf";
            string filter = openDlg.Filter;

            //Set title and initial directory
            openDlg.InitialDirectory =
            openDlg.Title = "Open Image File";
            openDlg.ShowHelp = true;

            //Show dialog
            if (openDlg.ShowDialog() == dialogResult.OK)
                //Get the file name and create Image object from file
                curFileName = openDlg.FileName;
                curImage = Image.FromFile(curFileName);

            //Paint the form, which forces a call to the pain event

The code for PrintPreviewDialog, PageSetupDialog, and PrintDialog is given in Listing 11.36. We show PrintDialog and call its PrintDocument.Print method if the user selects OK on the print dialog. We set PageSetupDialog page and printer settings when the user selects OK on the page setup dialog. For the print preview dialog, we set the UseAntiAlias property and call ShowDialog.

LISTING 11.36: Print dialogs

        private void PrintDialog_Click(object sender, System.EventArgs e)
            if (printDlg.ShowDialog() == DialogResult.OK)

        private void PageSetupDialog_Click(object sender, System.EventArgs e)
            if (setupDlg.ShowDialog() == DialogResult.OK)
                printDoc.DefualtPageSettings =
                printDoc.PrinterSettings =

        private void PrintPreview_Click(object sender, System.EventArgs e)
            previewDlg.UseAntiAlias = true;
            previewDlg.WindowState =

Now when we run the application and browse an image using the Open File menu item, the form looks like Figure 11.18.

If we click on Print Preview, our program will display the print preview dialog, as shown in Figure 11.19.

As stated earlier, the page setup dialog allows us to set the page properties, including size, sources, orientation, and margins. Clicking on Print Setup on the dialog menu brings up the page setup dialog, which is shown in Figure 11.20.

We can use these dialogs as we would in any other Windows applications.

Figure 11.18.jpg

FIGURE 11.18: Viewing an image and text

Figure 11.19.jpg

FIGURE 11.19: The print preview dialog

Figure 11.20.gif

FIGURE 11.20: The page setup dialog

Figure 11.21.gif

FIGURE 11.21: The print dialog


Hope the article would have helped you in understanding image class methods and properties in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Up Next
    Ebook Download
    View all
    View all