Control the print preview dialog in C#

example

The PrintPreviewDialog component provides a lot of features for the print preview dialog, but most of them are hard to find. Some are provided by the PrintPreviewControl that the dialog contains. You can set others if you treat the dialog as a form. This example shows how to do the following to the print preview dialog:

  • Set its size and determine whether it’s maximized
  • Set title bar text
  • Set the zoom level (including Auto)
  • Determine whether the results are anti-aliased
  • Determine whether the page display rows and columns
  • Set the background color and “paper” color
  • Set the starting page number

The following code shows how the program displays the dialog.

// Display a print preview.
private void btnPreview_Click(object sender, EventArgs e)
{
    // Set the size.
    Form frm = ppdShapes as Form;
    if (chkMaximized.Checked)
    {
        // Display maximized.
        frm.WindowState = FormWindowState.Maximized;
    }
    else
    {
        // Make the client area 400 x 400.
        frm.WindowState = FormWindowState.Normal;
        frm.StartPosition = FormStartPosition.CenterScreen;
        ppdShapes.ClientSize = new Size(400, 400);
    }

    // Set the dialog's title.
    frm.Text = "Numbers";

    // Set the zoom level.
    if (chkZoom100.Checked)
    {
        // 100%.
        ppdShapes.PrintPreviewControl.Zoom = 1.0;
    }
    else
    {
        // Auto.
        ppdShapes.PrintPreviewControl.AutoZoom = true;
    }

    // Set anti-aliasing.
    ppdShapes.PrintPreviewControl.UseAntiAlias =
        chkAntiAlias.Checked;

    // Set other properties.
    ppdShapes.PrintPreviewControl.Columns = 3;
    ppdShapes.PrintPreviewControl.Rows = 3;

    // Background color.
    ppdShapes.PrintPreviewControl.BackColor = Color.Orange;

    // Paper color.
    ppdShapes.PrintPreviewControl.ForeColor = Color.Yellow;

    // Page 3 in the upper left.
    ppdShapes.PrintPreviewControl.StartPage = 3;
    
    // Display the dialog.
    ppdShapes.ShowDialog();
}

The code starts by making a variable that represents the dialog as a Form. This provides access to its form-related properties such as size.

If the program’s Maximized checkbox is checked, the code set the Form‘s WindowState property to Maximized. If the checkbox is not checked, the code sets WindowState to Normal, sets StartPosition to center the Form, and sets the dialog’s client size (the area inside its borders).

Next the code sets the Form‘s Text property to give the dialog a title.

The other dialog properties set by this example are provided by the PrintPreviewControl contained within the dialog.

If the 100% Zoom checkbox is checked, the code sets the control’s Zoom property to 1.0. If the checkbox is not checked, the code sets the control’s AutoZoom property to true.

The code next sets the control’s UseAntiAlias property to true or false depending on whether its checkbox is checked. Turning anti-aliasing on makes the result smoother (you’ll probably have to look closely to see the difference), but can make drawing the preview take longer if it’s a very complicate printout.

The code sets the control’s Columns and Rows properties so the dialog display 9 pages in 3 rows and columns. It sets the background color (behind the pages) to orange and sets the foreground color (used for the pages’ “paper”) to yellow. Finally the code sets the control’s StartPage property to 3, so initially page number 3 is the one shown in the preview’s upper left corner.

The way the actual printing works isn’t described here. For more information, see these examples:


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in graphics, printing and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *