TX Text Control .NET for WPF

Print a Document

Topic

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 TextControl named textControl1 in MainWindow.xaml.

  • A button for each section in the DockPanel of MainWindow.xaml, before the TextControl element, with a Click handler 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;

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