TX Text Control .NET for WPF

Printing

Topic

A WPF application with TX Text Control prints in two ways. The WPF TextControl prints the document the user sees with the print system of WPF: a printer name, a PageRange, a number of copies and a collation. ServerTextControl and MailMerge print documents that are never shown, with the PrintDocument class of System.Drawing.Printing. What follows prints documents from code without the editor: in the background, as one print job for several documents and as the result of a mail merge.

Printing the editor's document, with or without the print dialog, is covered in Print a Document and Printing and Print Preview. The code goes into MainWindow.xaml.cs with the TextControl named textControl1.

Prepare the Project for PrintDocument

ServerTextControl.Print and MailMerge.Print take a System.Drawing.Printing.PrintDocument. A WPF project for .NET 8 or higher needs the NuGet package System.Drawing.Common for this class, and Print uses only some settings of the PrintDocument; Print a File Without the Editor lists them and prints a first file. MainWindow.xaml.cs needs these using directives for the rest of the examples:

using System.Drawing.Printing;
using System.Threading.Tasks;
using System.Windows;
using TXTextControl;

Choose the Formatting Printer

Line and page breaks depend on the printer whose text measurements TX Text Control uses, as Print Exactly What the Screen Shows explains. ServerTextControl.FormattingPrinter takes the same values as WPF.TextControl.FormattingPrinter: a printer name, "Standard" or "Display". Give the TextControl and the ServerTextControl the same value when documents move between them. A network printer that answers slowly can delay the initialization of TX Text Control; the blog article Initialize Text Control Without a Printer (Completely) describes this.

Next Steps