Printing and Print Preview
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:
| Call | Result |
|---|---|
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.
Print With the Print Dialog
The simplest way lets TX Text Control open the standard print dialog with Print("My Document", true), after checking CanPrint. Print With the Print Dialog in the tutorial Print a Document shows the handler. CanPrint is false when the document is protected and its AllowPrinting permission is turned off, as Document Protection and Editable Regions describes.
Print Without a Dialog
The overloads with a PageRange print directly, on the standard printer or on the printer you name. Print Without a Dialog in the tutorial Print a Document prints two collated copies of the first two pages on Microsoft Print to PDF. A PageRange covers the pages from its first to its last argument: new PageRange(1, 2) covers pages 1 to 2, and new PageRange(3) covers page 3 only. Collation.Collated prints the copies one after the other, each complete.
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:
MinPageandMaxPagelimit the page numbers the user can enter.UserPageRangeEnabledandCurrentPageEnabledoffer the Pages and Current Page options.PageRangeSelectionsays which option the user chose. The current page is the one with the input position, InputPosition.Page, which is available in page view.PrintQueue.FullNameis the name of the selected printer.CopyCountandCollationof thePrintTicketare 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.
Print Exactly What the Screen Shows
A document is formatted with the text dimensions of a printer, so that line and page breaks on the screen match those on paper. FormattingPrinter names this formatting printer: an installed printer, Standard for the standard printer of the system, or Display for the font dimensions of the screen. When the named printer isn't installed, the standard printer is used. A document printed on its formatting printer has the same dimensions as on the screen.
The paper sizes that the editor supports depend on the formatting printer as well. GetSupportedPaperSizes returns them as an array of PaperSize objects, each with a name and a width and height in twips, or null while the control isn't completely initialized:
private void PaperSizes_Click(object sender, RoutedEventArgs e)
{
System.Text.StringBuilder list = new System.Text.StringBuilder();
// TXTextControl.PaperSize, not System.Drawing.Printing.PaperSize
TXTextControl.PaperSize[] paperSizes = textControl1.GetSupportedPaperSizes();
foreach (TXTextControl.PaperSize paperSize in paperSizes ?? new TXTextControl.PaperSize[0])
{
// Width and Height are in twips; 1 inch is 1440 twips
list.AppendLine($"{paperSize.Name}: {paperSize.Width / 1440.0:0.##} x {paperSize.Height / 1440.0:0.##} in");
}
MessageBox.Show(this, list.ToString(), "Paper sizes");
}
Next Steps
- Print a Document: the first steps of printing.
- Printing: printing documents in code, without the editor.
- Viewing, Zooming, and Navigation: page view, pages and the input position.
- Document Protection and Editable Regions: documents that may not be printed.