Print a Document
Printing works differently in WPF than in Windows Forms, and TX Text Control follows both models. The WPF TextControl prints through the print system of WPF, with a printer name, a PageRange and a copy count, while ServerTextControl prints with a PrintDocument of System.Drawing.Printing. The button handlers below print the editor's document with the standard print dialog and without one, print a file the user never sees, and set the printer that decides where lines and pages break.
Prerequisites
The application from Create Your First WPF Application, with a
TextControlnamedtextControl1in MainWindow.xaml.A button for each section in the
DockPanelof MainWindow.xaml, before theTextControlelement, with aClickhandler in MainWindow.xaml.cs:<Button x:Name="printButton" DockPanel.Dock="Top" Content="Print..." Click="printButton_Click" />A printer. The examples print to Microsoft Print to PDF, which comes with Windows. Use the name of your own printer instead.
MainWindow.xaml.cs needs these using directives:
using System;
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using TXTextControl;
Print With the Print Dialog
private void printButton_Click(object sender, RoutedEventArgs e)
{
if (!textControl1.CanPrint)
{
MessageBox.Show(this, "This document cannot be printed.");
return;
}
// Open the print dialog with the settings of the previous print job
textControl1.Print("My Document", true);
}
- Print with a document name and a Boolean value opens the standard print dialog, in which the user chooses the printer and the pages, and prints the document. With
true, the dialog starts with the settings the user chose the last time. - CanPrint is
falsewhen the document may not be printed. It depends on the document permissions and the edit mode, which a protected document can set.
The WPF TextControl has no print preview method like the PrintPreview of the Windows Forms version. To show the PrintDialog of WPF with your own options, pass its settings to Print as Printing in the Troubleshooting section shows.
Print Without a Dialog
Two further overloads of Print take the settings of the print job as arguments and print without asking the user: the pages to print as a PageRange, the number of copies and a Collation value, with or without the name of a printer.
private void quickPrintButton_Click(object sender, RoutedEventArgs e)
{
// Pages 1 to 2, or fewer if the document is shorter
PageRange pageRange = new PageRange(1, Math.Min(2, textControl1.Pages));
textControl1.Print("My Document", "Microsoft Print to PDF", pageRange, 2, Collation.Collated);
}
The code works as follows:
PageRangefromSystem.Windows.Controlsnames the first and the last page to print. Pages returns the number of pages of the document.- The second argument is the name of the printer. The overload without it prints on the standard printer.
2is the number of copies, andCollation.CollatedfromSystem.Printingprints them as complete sets.
In a .NET Framework project, System.Printing requires references to the assemblies System.Printing.dll and ReachFramework.dll. A .NET 8 or higher project has them already.
Print a File Without the Editor
A ServerTextControl prints a document without showing it. ServerTextControl.Print takes a PrintDocument of System.Drawing.Printing and prints with its settings, without a dialog.
A WPF project for .NET 8 or higher doesn't reference System.Drawing.Printing, and the compiler reports that PrintDocument is in System.Drawing.Common. Install the NuGet package System.Drawing.Common in the major version of your .NET, for example 8.0 for .NET 8, and add using System.Drawing.Printing; to MainWindow.xaml.cs. A .NET Framework project references System.Drawing.dll by default.
private void printFileButton_Click(object sender, RoutedEventArgs e)
{
using (ServerTextControl tx = new ServerTextControl())
{
// Initialize the new instance before using it
tx.Create();
tx.Load("document.docx", StreamType.WordprocessingML);
PrintDocument printDocument = new PrintDocument();
printDocument.DocumentName = "document.docx";
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Print without the "Page n of document" status dialog
printDocument.PrintController = new StandardPrintController();
tx.Print(printDocument);
}
}
The code works as follows:
ServerTextControl.Printtakes the document name, the printer and the page and copy settings from thePrintDocument. Printing lists the settings it uses.- A
StandardPrintControllerprints without the status dialog that .NET otherwise shows for each page.
Format the Document for the Printer
TX Text Control lays out a document with the text dimensions of a printer, so that the lines and pages on the screen break where they break on paper. FormattingPrinter sets that printer:
private void formatForPrinterButton_Click(object sender, RoutedEventArgs e)
{
// Line and page breaks now match this printer
textControl1.FormattingPrinter = "Microsoft Print to PDF";
}
A document printed on its formatting printer has the same dimensions on paper as on the screen. Print Exactly What the Screen Shows describes the other values and what happens when the printer isn't installed.
Next Steps
- Printing and Print Preview: printing from the editor in detail.
- Printing: printing in code and batch printing.
- Printing in Troubleshooting: ignored dialog settings, breaks that differ from the screen and other problems.
- The blog article Printing Using TX Text Control .NET for WPF: the print system of WPF and a print dialog with all page options.