TX Text Control .NET for Windows Forms

Printing

Topic

Printing from code means printing without asking the user each time: a delivery note after an order is saved, a stack of merged letters, all documents of a folder. TX Text Control prints through the PrintDocument class of .NET, both from the TextControl on your form and from a ServerTextControl that never shows the document. You choose the printer, the pages and the copies, combine several documents into one print job, print mail merge results and pick the printer whose measurements format the document.

The print dialog, the print preview and printing a single page from the editor are covered in Print a Document and Printing and Print Preview. The examples here are handlers in Form1.cs with the TextControl named textControl1; Form1.cs needs these using directives:

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

How Printing Works

Three classes print, all with a System.Drawing.Printing.PrintDocument:

MemberPrints
TextControl.PrintThe document in the editor; with a document name, after the user confirmed the print dialog
ServerTextControl.PrintA document your code loaded or created, without showing it and without a dialog
MailMerge.PrintThe document a mail merge created

All three use these settings of the PrintDocument: DocumentName, PrintController, DefaultPageSettings.Color and, in PrinterSettings, PrinterName, FromPage, ToPage, Copies, Collate, PrintToFile and PrintRange. FromPage and ToPage take effect only when PrintRange is PrintRange.SomePages, and without a PrinterName, the default printer prints. The overloads of TextControl.Print are listed in The Print Methods.

Before printing the editor's document, check CanPrint: a protected document can forbid printing.

Choose the Formatting Printer

TX Text Control formats text with the measurements of a device, so that line and page breaks on the screen match those on paper. FormattingPrinter names the printer whose measurements are used:

ValueFormats with
The name of a printerThe measurements of that printer; if it isn't installed, the standard printer
"Standard"The standard printer of the system
"Display"The font measurements of the display device

A document printed on its formatting printer has the same breaks as on the screen, and the paper sizes the editor offers depend on it, as Print Exactly What the Screen Shows shows. ServerTextControl.FormattingPrinter does the same for a ServerTextControl; give both the same value when a document goes from one to the other. A printer that responds late, such as a network printer in energy-saving mode, can delay the initialization of TX Text Control, as the blog article Initialize Text Control Without a Printer (Completely) explains.

Next Steps