# Print a Document

> Print from the WPF TextControl with the print dialog or a PageRange and printer, print files with ServerTextControl and PrintDocument, set FormattingPrinter.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Getting Started > Document Processing > Print a Document
- **2217 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/llms-full.txt

---

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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/first-application/llms-full.txt), 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*:

    ```xml
    <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:

```csharp
using System;
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using TXTextControl;
```

### Print With the Print Dialog

```csharp
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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/print-method/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/canprint-property/llms-full.txt) is `false` when 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/printing/#the-settings-of-the-print-dialog-are-ignored) 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.

```csharp
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:

- `PageRange` from `System.Windows.Controls` names the first and the last page to print. [Pages](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/pages-property/llms-full.txt) 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.
- `2` is the number of copies, and `Collation.Collated` from `System.Printing` prints 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/llms-full.txt) prints a document without showing it. [ServerTextControl.Print](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/print-method/llms-full.txt) 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](https://www.nuget.org/packages/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.

```csharp
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.Print` takes the document name, the printer and the page and copy settings from the `PrintDocument`. [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/printing/#prepare-the-project-for-printdocument) lists the settings it uses.
- A `StandardPrintController` prints 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/formattingprinter-property/llms-full.txt) sets that printer:

```csharp
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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/printing/#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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/printing/llms-full.txt): printing from the editor in detail.
- [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/printing/llms-full.txt): printing in code and batch printing.
- [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/printing/llms-full.txt) 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](https://www.textcontrol.com/blog/2014/07/21/printing-using-tx-text-control-net-for-wpf/): the print system of WPF and a print dialog with all page options.

---

## Nearby

- **Previous:** [Save as DOCX and PDF](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/save-docx-pdf/llms-full.txt)
- **Next:** [Mail Merge](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/mail-merge/llms-full.txt)
