TX Text Control .NET for Windows Forms

Print a Document

Topic

Printing in a Windows Forms application with TX Text Control uses the printing of .NET: the standard print dialog, the print preview, and a PrintDocument for printing without asking the user. The button handlers below print the document of the editor in each of these ways, print a file the editor never shows, and explain why the formatting printer matters for what comes out of the printer.

Prerequisites

  • The application from Create Your First Windows Forms Application, with a TextControl named textControl1 on Form1.
  • For each section, a Button on the form. Double-click it in the designer to create its Click handler.
  • A printer. The examples print to Microsoft Print to PDF, which Windows provides. Replace its name with the name of your printer.

Form1.cs needs these using directives:

using System.Drawing.Printing;
using TXTextControl;

Show a Print Preview

private void previewButton_Click(object sender, EventArgs e)
{
    textControl1.PrintPreview("My Document");
}

PrintPreview shows a preview of the current document. Another overload takes a PrintDocument and shows the preview with its settings.

Format the Document for the Printer

TX Text Control formats a document with the text dimensions of a printer, so that lines and pages break on the screen exactly as on paper. FormattingPrinter names that printer:

private void formatForPrinterButton_Click(object sender, EventArgs e)
{
    // Line and page breaks now match this printer
    textControl1.FormattingPrinter = "Microsoft Print to PDF";
}

The values Standard and Display and what happens with a printer that is not installed are described in Print Exactly What the Screen Shows.

Next Steps