TX Text Control .NET for WPF

Printing and Print Preview

Topic

Printing in WPF works differently from Windows Forms: there is no PrintDocument with an event per page. Instead, the print queue and the print ticket of System.Printing describe the printer and the job, and the Print method of the WPF TextControl takes their values as parameters. What follows covers printing with a dialog, printing a page range without asking the user and connecting the WPF PrintDialog to the editor.

All samples assume a window with a TextControl named textControl1, as in Create Your First WPF Application. Print a Document introduces printing in a first tutorial.

The Print Method

Print has three overloads:

CallResult
Print(string docName, bool useLastPrinterSettings)Opens the standard print dialog and prints; with true, the dialog starts with the settings of the previous call
Print(string docName, PageRange pageRange, int copyCount, Collation collation)Prints on the standard printer without a dialog
Print(string docName, string printerName, PageRange pageRange, int copyCount, Collation collation)Prints on the named printer without a dialog

docName names the print job. PageRange is a structure of System.Windows.Controls and Collation an enumeration of System.Printing, both part of WPF.

Unlike the Windows Forms control, the WPF TextControl has no PrintPreview method.

Connect the WPF Print Dialog

The PrintDialog of System.Windows.Controls offers page ranges, the current page and the copies, and it passes back the printer queue and the print ticket. Its results fit the parameters of Print:

private void PrintWithDialogButton_Click(object sender, RoutedEventArgs e)
{
    PrintDialog dialog = new PrintDialog();

    // Pages the user can choose from
    dialog.MinPage = 1;
    dialog.MaxPage = (uint)textControl1.Pages;
    dialog.UserPageRangeEnabled = true;
    dialog.CurrentPageEnabled = true;

    if (dialog.ShowDialog() != true)
    {
        return;
    }

    PageRange pages = new PageRange(1, textControl1.Pages);

    if (dialog.PageRangeSelection == PageRangeSelection.UserPages)
    {
        pages = dialog.PageRange;
    }
    else if (dialog.PageRangeSelection == PageRangeSelection.CurrentPage)
    {
        pages = new PageRange(textControl1.InputPosition.Page);
    }

    textControl1.Print("My Document",
        dialog.PrintQueue.FullName,
        pages,
        dialog.PrintTicket.CopyCount ?? 1,
        dialog.PrintTicket.Collation ?? Collation.Uncollated);
}

The code works as follows:

  • MinPage and MaxPage limit the page numbers the user can enter. UserPageRangeEnabled and CurrentPageEnabled offer the Pages and Current Page options.
  • PageRangeSelection says which option the user chose. The current page is the one with the input position, InputPosition.Page, which is available in page view.
  • PrintQueue.FullName is the name of the selected printer. CopyCount and Collation of the PrintTicket are nullable, so the code supplies defaults.

The blog article Printing Using TX Text Control .NET for WPF compares WPF printing with Windows Forms printing and also handles a selection as the page range.

Next Steps