Visual C#

The Print Dialog Box

One of the ways users print consists of sending the document to the printer. To directly send a document to the printer, you need to make sure that the…

7 min read1,528 words5 code samples

Introduction#

Printing often involves transmitting a document directly to a print device. For this to work, the specific control containing the data must have built-in printing support. You can make this feature accessible to users by adding a button or menu item to your application's interface. Clicking this print command will typically output the entire document in its current state, including colors if both the document and the printer support them. In environments with multiple printers, the system will automatically route the job to the default printer unless specified otherwise.

If you want users to be able to configure or customize the printing process, Microsoft Windows provides a common dialog box called Print. Here is an example:

The Print dialog box allows a user to select a printer if more than one is available. The user can decide either to print the whole document, to print a range of pages, or to print a portion of the document that was previously selected. The user can also decide on the number of copies to print from the document, the range specified, or the selected portion. Furthermore, the user can access the particular characteristics of the selected printer and specify how the printer should perform the job. For example, if the selected printer can print in color and the document is in color but the user wants to print in black and white, he or she can specify this using the Properties button.

Providing a Printer#

To provide the users with the ability to customize printing through the Print dialog box, you can add a PrintDialog object from the Dialogs section of the Toolbox to your form. The PrintDialog control is implemented through the PrintDialog class of the System.Windows.Forms namespace. To programmatically create a PrintDialog object, you can declare a variable of type PrinterDialog. Here is an example:

C#
using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : Form
{
    Button btnPrint;

    public Exercise()
    {
        InitializeComponent();
    }

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

        Controls.Add(btnPrint);
    }

    void btnPrintDocument(object sender, EventArgs e)
    {
        PrintDialog dlgPrint = new PrintDialog();
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

To present the Print dialog box to the user, you can call its ShowDialog() method.

The Printing Process#

The Document to Print#

In order to print, the Print dialog box must be given a document to print. This means that you must first prepare a document prior to printing. To support this, the .NET Framework provides the PrintDocument class that is defined in the System.Drawing.Printing namespace. This class is represented in the Toolbox by the PrintDocument button . Based on this, to prepare a document for printing, you can either add a PrintDocument object to your project or declare a variable of type PrintDocument. Here is an example:

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

public class Exercise : Form
{
    Button btnPrint;

    public Exercise()
    {
        InitializeComponent();
    }

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

        Controls.Add(btnPrint);
    }

    void btnPrintDocument(object sender, EventArgs e)
    {
        PrintDialog dlgPrint = new PrintDialog();
        PrintDocument docPrint = new PrintDocument();
    }
}

After creating the document to print through a PrintDocument object, you can associate it with a PrintDialog. To support this, the PrintDialog class is equipped with the Document property. To specify the object that would carry the printing, you can assign the PrintDocument object to the PrintDialog.Document property. Here is an example:

C#
System.Void btnPrint_Click(System.Object  sender, System.EventArgs  e)
{
	 PrintDialog       dlgPrint = new PrintDialog;
	 PrintDocument docPrint = new PrintDocument;

	 dlgPrint.Document = docPrint;
	 dlgPrint.ShowDialog();
}

Printed documents can consist of a single page or multiple pages, each possessing specific attributes. These attributes are managed through the PrintDocument.PageSettings property, which relies on the PageSettings class within the System.Drawing.Printing namespace. This class tracks various details, such as page size, margin settings, paper tray selection, resolution, color options, and page orientation. If you prefer to use standard settings, you can assign DefaultPageSettings to this property. Furthermore, the PrintDocument.DocumentName property allows you to label the document being processed. Here is a practical example:

C#
void btnPrintDocument(object sender, EventArgs e)
{
        PrintDialog dlgPrint = new PrintDialog();
        PrintDocument docPrint = new PrintDocument();

        dlgPrint.Document = docPrint;
        dlgPrint.ShowDialog();
}

To actually print the document, you can call the PrintDocument.Print() method. Its syntax is:

C#
public void Print();

When the PrintDocument.Print() method is called, the printing process would start by firing the BeginPrint event but this event occurs before the first page is printed. The BeginPrint event is of type PrintEventArgs which does not hold any particular information, especially for the BeginPrint event. This event allows you to take some early actions, if necessary, before the printer receives the job. Once the printer is ready, the application would then need to know what needs to be printed on the paper. At this time, the PrintPage event is fired. The PrintPage event is of type PrintPageEventArgs. The PrintPageEventArgs class allows you to fully customize the page of the document to be printed. For example, it is equipped with a Graphics property that allows you to "draw" anything you want on the paper. The PrintPageEventArgs class also allows you to get the location and dimensions of the area to be printed. This is represented by the PageBounds property, which produces a Rectangle object. Once the PrintDocument object is ready, you must pass it to the Print dialog box. To do that, assign the name of the PrintDocument variable to the PrintDialog.Document property.

The Printer Settings#

In the above example, we saw a somewhat simplistic way of making the Print dialog box available to the user. This dialog box offers many options defined as the Printer Settings. To support the various options of a Print dialog box, the PrintDialog class is equipped with a property called PrinterSettings, which itself is defined from the PrinterSettings class, which holds all possible characteristics of a printer. The first option presented to the user is the name of the printer to be used. Because there can be many printers available to the user, the printers are presented as a combo box:

The available printers may also be presented as a list view:

The Name combo box in the Printer section or the Select Printer list view allows the user to select the printer that will handle the job. If you are writing a universal application and cannot predict what printer(s) the user would have, you would not be concerned with this characteristic. If you are writing an application for a special company or you are creating a particular application and you know for sure what printer should be used to print the current document, then you can specify the printer to use. To do this, assign the (exact) name of the printer to the PrinterSettings.PrinterName property. On the other hand, if for some reason you want to know what printer the user selected to print the document, you can get the value of this PrinterName property. Under the Name combo box, the labels provide the status of the selected printer (whether it is ready or not), the type of printer (such as its manufacturer), its location (such as where in the building the printer is located; the person who installed the printer or may have provided this printer), and an optional comment (this information is created by the person who installed the printer or it can be changed in the printer's properties). After selecting the printer, the user can access the properties of that particular printer. Different printers support different options. To configure the printed paper based on the selected printer, the user can click either Properties or Preferences. This opens the Document Properties or the Printing Preferences dialog box. The content of this dialog box (highly) depends on the printer that was selected but some characteristics are shared among printers. On the lower-right side of the Printer section or of the Select Printer section, there is a check box labeled Print To File. When this check box is checked, the document is transformed into a file rather than being printed. In this case, if the user clicks OK or Print, a dialog box would come up, asking the user to specify the path and a name for the new file that will be created. The appearance of that dialog box depends. Here is an example:

If you want the Print To File check box to be checked, set the PrinterSettings.PrintFoFile Boolean property to true. Its default value is false, which lets the user decide whether to check it or not. After selecting the printer and deciding whether to physically print or to only create a printed file, the user can click OK. If the document is made of only one page, it would be printed. If the document contains more than one page, the user may want to print only one page, a range of pages, or all pages. The user can also select a section in the document and print only that section. This decision is made using the Page Range section. This section provides four radio buttons. The first radio button labeled All is selected by default and allows the user to print the whole document. By default, the second radio button is disabled. If the user had selected a section of the document to print, then the second radio button, labeled Selection would be enabled:

Users have the flexibility to print either the entire document or just a highlighted portion. For multi-page documents, options like the Current Page radio button allow for printing a single specific page, or a custom range can be defined. While these choices are typically made by the user at runtime, you can pre-define page ranges using the FromPage and ToPage properties of PrinterSettings, or set boundaries with MinimumPage and MaximumPage. The Page Range section also includes a control for selecting the number of copies, which corresponds to the Copies property. When multiple copies are requested, the Collate property (and its associated checkbox) determines the order in which pages are printed. Index Copyright © 2008 C# Key