Control the print preview dialog to change its size, scale, colors, and more in C#

The PrintPreviewDialog component provides a lot of features 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 set the dialog's:

  • Size and whether it is maximized
  • Title bar text
  • Zoom level (including Auto)
  • Anti-aliasing
  • Page display rows and columns
  • Background color and "paper" color
  • 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;
ppdShapes.PrintPreviewControl.BackColor = Color.Orange; // Background color.
ppdShapes.PrintPreviewControl.ForeColor = Color.Yellow; // Paper color.
ppdShapes.PrintPreviewControl.StartPage = 3; // Page 3 in the upper left.

// 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 Maximized checkbox is checked, the program 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 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:

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.