# Printing

> Print WPF documents without the editor: ServerTextControl with PrintDocument in the background, several files in one job and merged letters with MailMerge.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Document Processing > Printing
- **2405 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/printing/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/printing/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/printing/llms-full.txt

---

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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/llms-full.txt) and [Printing and Print Preview](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/printing/llms-full.txt). The code goes into *MainWindow.xaml.cs* with the `TextControl` named `textControl1`.

### Print the Editor's Document Without a Dialog

[WPF.TextControl.Print](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/print-method/llms-full.txt) prints without a dialog when you pass it a `PageRange`, a number of copies and a `Collation` value, with or without a printer name. [Print Without a Dialog](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/printing/#print-without-a-dialog) in the Document Editor section shows the overloads and the code, and [Connect the WPF Print Dialog](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/printing/#connect-the-wpf-print-dialog) passes the settings of a WPF `PrintDialog` to them.

### Prepare the Project for PrintDocument

[ServerTextControl.Print](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/print-method/llms-full.txt) and [MailMerge.Print](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver/mailmerge/print-method/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/#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:

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

### Print a File in the Background

A `ServerTextControl` doesn't belong to the window, so it can load and print a document on a thread of its own while the window keeps responding:

```csharp
private async void PrintInBackground_Click(object sender, RoutedEventArgs e)
{
    printInBackgroundButton.IsEnabled = false;

    await Task.Run(() =>
    {
        using (ServerTextControl tx = new ServerTextControl())
        {
            // Initialize the new instance before using it
            tx.Create();
            tx.Load("report.docx", StreamType.WordprocessingML);

            PrintDocument printDocument = new PrintDocument();
            printDocument.DocumentName = "report.docx";
            printDocument.PrinterSettings.PrintRange = PrintRange.SomePages;
            printDocument.PrinterSettings.FromPage = 1;
            printDocument.PrinterSettings.ToPage = 3;

            // No status dialog of .NET
            printDocument.PrintController = new StandardPrintController();

            tx.Print(printDocument);
        }
    });

    // The code after await runs on the UI thread again
    printInBackgroundButton.IsEnabled = true;
}
```

The code works as follows:

- [Create](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) initializes the [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/llms-full.txt); the `using` block disposes it after printing.
- `PrintRange.SomePages` makes `Print` use `FromPage` and `ToPage`.
- A `StandardPrintController` suppresses the dialog with the page being printed.
- The WPF `TextControl` belongs to the UI thread and isn't used inside `Task.Run`. To print its document in the background, save it to a byte array first, as [Using ServerTextControl in Desktop Applications](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/servertextcontrol/llms-full.txt) shows.

### Print Several Documents in One Print Job

`ServerTextControl.Print(int page, PrintPageEventArgs e)` prints one page from the `PrintPage` event of a `PrintDocument`. With it, several documents go to the printer as a single job:

```csharp
private void PrintBatch_Click(object sender, RoutedEventArgs e)
{
    string[] files = { "letter.docx", "invoice.docx" };

    using (ServerTextControl tx = new ServerTextControl())
    {
        tx.Create();

        int fileIndex = 0;
        int pageNumber = 1;
        tx.Load(files[fileIndex], StreamType.WordprocessingML);

        PrintDocument printDocument = new PrintDocument();
        printDocument.DocumentName = "Letter and invoice";
        printDocument.PrintController = new StandardPrintController();

        printDocument.PrintPage += (s, args) =>
        {
            tx.Print(pageNumber, args);
            pageNumber++;

            // Last page of this file: continue with the next file
            if (pageNumber > tx.Pages)
            {
                fileIndex++;

                if (fileIndex < files.Length)
                {
                    tx.Load(files[fileIndex], StreamType.WordprocessingML);
                    pageNumber = 1;
                }
            }

            args.HasMorePages = fileIndex < files.Length;
        };

        printDocument.Print();
    }
}
```

`printDocument.Print()` starts the job, .NET raises `PrintPage` once per page, and `HasMorePages` is `false` after the last page of the last file. [ServerTextControl.Pages](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/pages-property/llms-full.txt) is the number of pages of the loaded document.

### Print a Merged Document

A [MailMerge](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver/mailmerge/llms-full.txt) connected to a `ServerTextControl` merges a template with data and prints the result with [MailMerge.Print](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver/mailmerge/print-method/llms-full.txt), which takes either a `PrintDocument` or a document name, a printer name, a number of copies and a collate flag. [Load, Save and Print With MailMerge](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/mail-merge/overview/#load-save-and-print-with-mailmerge) shows the code, with the printer chosen in the WPF print dialog.

### 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](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/printing/#print-exactly-what-the-screen-shows) explains. [ServerTextControl.FormattingPrinter](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/formattingprinter-property/llms-full.txt) takes the same values as [WPF.TextControl.FormattingPrinter](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/formattingprinter-property/llms-full.txt): 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)](https://www.textcontrol.com/blog/2022/02/28/initialize-text-control-without-a-printer-completely/) describes this.

### 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 with the print dialog.
- [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/printing/llms-full.txt) in Troubleshooting: disabled printing and unexpected breaks.
- [Using ServerTextControl in Desktop Applications](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/servertextcontrol/llms-full.txt): background work with `ServerTextControl`.
- [Sections, Pages, Headers, and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/sections-headers/llms-full.txt): pages and page numbers.

---

## Nearby

- **Previous:** [XML Processing](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/xml/llms-full.txt)
- **Next:** [Using ServerTextControl in Desktop Applications](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/servertextcontrol/llms-full.txt)
