Home

Printing Controls: The Page Setup Dialog

     

Description

As opposed to directly printing a file, a user may want to perform some preliminary preparation on the document or the printer. Microsoft Windows provides another dialog box used to control printing. It is called Page Setup:

 

To provide a Page Setup to your application, you can use the PageSetupDialog button from the Toolbox. The PageSetupDialog control is based on the PageSetupDialog class which derives from the CommonDialog class.

 This means that, instead of visually adding a PageSetupDialog to a form, you can declare a variable of that class. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;

public class Exercise : Form
{
    PageSetupDialog dlgPageSetup;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        dlgPageSetup = new PageSetupDialog();

        Text = "Printing";
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Exercise());
    }
}

Characteristics of the Page Setup Dialog Box

When using the Page Setup dialog box, the user must first select a printer. This is usually done already by the operating system that selects the default printer of the computer that called this dialog box. Otherwise, to select a printer or to change the printer, the user can click the Printer button and select or change it using the Name combo box:

Page Setup

In most cases, the operation system is in charge of communication between the document that the user wants to print, the change the user would make on the Page Setup dialog box. This means that the first thing you need to do is to display the Page Setup dialog box. To let you display the Page Setup dialog box, the PageSetupDialog class is equipped with the ShowDialog() method. To let specify the document that will be managed by the Page Setup dialog box, the PageSetupDialog class is equipped with the Document property that is of type PrintDocument:

public PrintDocument Document { get; set; }

This can be done as follows:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;

public class Exercise : Form
{
    PrintDialog dlgPrint;
    PrintDocument docPrint;
    PageSetupDialog dlgPageSetup;
    Button btnPrint, btnPageSetup;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        btnPrint = new Button();
        btnPrint.Width = 120;
        btnPrint.Text = "&Print...";
        btnPrint.Location = new Point(12, 12);
        btnPrint.Click += new EventHandler(btnPrintDocumentClick);

        btnPageSetup = new Button();
        btnPageSetup.Width = 120;
        btnPageSetup.Text = "P&age Setup...";
        btnPageSetup.Location = new Point(12, 44);
        btnPageSetup.Click += new EventHandler(btnPageSetupClick);

        dlgPrint = new PrintDialog();

        docPrint = new PrintDocument();
        dlgPrint.Document = docPrint;
        docPrint.PrintPage += new PrintPageEventHandler(docPrintPrintPage);

        dlgPageSetup = new PageSetupDialog();
        dlgPageSetup.Document = docPrint;

        Controls.Add(btnPrint);
        Controls.Add(btnPageSetup);
        Text = "Printing";
    }

    private void docPrintPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawString("Picture Preview", new Font("Times New Roman", 28, FontStyle.Bold), Brushes.Red, 60F, 80F);
        e.Graphics.DrawLine(new Pen(Color.Black, 2), 60, 120, 700, 120);
        e.Graphics.DrawImage(Image.FromFile(@"C:\Exercise1\fall1.jpg"), 60F, 200F);
    }

    void btnPrintDocumentClick(object sender, EventArgs e)
    {
        if (dlgPrint.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            docPrint.Print();
    }

    void btnPageSetupClick(object sender, EventArgs e)
    {
        dlgPageSetup.ShowDialog();
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Exercise());
    }
}

This would display the Page Setup dialog box. After selecting the printer, the user can click OK.

On the Page Setup dialog box, the user can click the arrow of the Size combo box and select one of the configured sizes of paper. The characteristics of the paper are controlled by the PageSettings class that we mentioned earlier. For example, if the printer has many trays, as indicated by the driver of the selected printer, the user can select which tray would be used when printing. As it happens, one printer can have only one tray while another printer can have 3, 5, or more trays. If you want to programmatically change the characteristics of the paper, declare a variable of type PageSettings and specify its values using its properties. To actually specify the values of the page, assign the PageSettings object to the PageSettings property of the PageSetupDialog object. This can be done as follows:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;

public class Exercise : Form
{
    PrintDialog dlgPrint;
    PrintDocument docPrint;
    PageSetupDialog dlgPageSetup;
    Button btnPrint, btnPageSetup;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        btnPrint = new Button();
        btnPrint.Width = 120;
        btnPrint.Text = "&Print...";
        btnPrint.Location = new Point(12, 12);
        btnPrint.Click += new EventHandler(btnPrintDocumentClick);

        btnPageSetup = new Button();
        btnPageSetup.Width = 120;
        btnPageSetup.Text = "P&age Setup...";
        btnPageSetup.Location = new Point(12, 44);
        btnPageSetup.Click += new EventHandler(btnPageSetupClick);

        dlgPrint = new PrintDialog();

        docPrint = new PrintDocument();
        dlgPrint.Document = docPrint;
        docPrint.PrintPage += new PrintPageEventHandler(docPrintPrintPage);

        dlgPageSetup = new PageSetupDialog();
        dlgPageSetup.Document = docPrint;

        PageSettings psSetup = new PageSettings();
        psSetup.Margins = new Margins(160, 120, 80, 200);
        psSetup.Landscape = true;
        dlgPageSetup.PageSettings = psSetup;

        Controls.Add(btnPrint);
        Controls.Add(btnPageSetup);
        Text = "Printing";
    }

    private void docPrintPrintPage(object sender,
            System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawString("Picture Preview", new Font("Times New Roman", 28, FontStyle.Bold), Brushes.Red, 60F, 80F);
        e.Graphics.DrawLine(new Pen(Color.Black, 2), 60, 120, 700, 120);
        e.Graphics.DrawImage(Image.FromFile(@"C:\Exercise1\fall1.jpg"), 60F, 200F);
    }

    void btnPrintDocumentClick(object sender, EventArgs e)
    {
        if (dlgPrint.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            docPrint.Print();
    }

    void btnPageSetupClick(object sender, EventArgs e)
    {
        dlgPageSetup.ShowDialog();
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Exercise());
    }
}

This would produce:

Page Setup Dialog Box

While the page settings are used to customize the page that is being printed, the printer settings are used to characteristics of the printer. If you want to do take care of this, you can use the PageSetupDialog.PrinterSettings property which is a value of the PrinterSettings class reviewed earlier. If the desired printer is on a network, the user can click the Network button to locate it. To programmatically show or hide the Network button, specify a false or true result to the PageSetupDialog.ShowNetwork Boolean property.

The user also has the option to print the document in Portrait (vertical) or in Landscape (horizontal) position. The option to allow the user to select Portrait or Landscape is controlled by the AllowOrientation Boolean property.

 

Home Copyright © 2014, FunctionX