# Printing and Print Preview

> Print from a WPF TextControl with the Print overloads, PageRange and Collation, connect the WPF PrintDialog and set the FormattingPrinter for exact line breaks.

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

---

Printing in WPF works differently from Windows Forms: there is no `PrintDocument` with an event per page. Instead, the print queue and the print ticket of `System.Printing` describe the printer and the job, and the `Print` method of the WPF `TextControl` takes their values as parameters. What follows covers printing with a dialog, printing a page range without asking the user and connecting the WPF `PrintDialog` to the editor.

All samples assume a window with a `TextControl` named `textControl1`, as in [Create Your First WPF Application](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/first-application/llms-full.txt). [Print a Document](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/llms-full.txt) introduces printing in a first tutorial.

### The Print Method

[Print](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/print-method/llms-full.txt) has three overloads:

| Call | Result |
|---|---|
| `Print(string docName, bool useLastPrinterSettings)` | Opens the standard print dialog and prints; with `true`, the dialog starts with the settings of the previous call |
| `Print(string docName, PageRange pageRange, int copyCount, Collation collation)` | Prints on the standard printer without a dialog |
| `Print(string docName, string printerName, PageRange pageRange, int copyCount, Collation collation)` | Prints on the named printer without a dialog |

`docName` names the print job. `PageRange` is a structure of `System.Windows.Controls` and `Collation` an enumeration of `System.Printing`, both part of WPF.

Unlike the Windows Forms control, the WPF `TextControl` has no `PrintPreview` method.

### Print With the Print Dialog

The simplest way lets TX Text Control open the standard print dialog with `Print("My Document", true)`, after checking [CanPrint](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/canprint-property/llms-full.txt). [Print With the Print Dialog](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/#print-with-the-print-dialog) in the tutorial Print a Document shows the handler. `CanPrint` is `false` when the document is protected and its [AllowPrinting](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/documentpermissions/allowprinting-property/llms-full.txt) permission is turned off, as [Document Protection and Editable Regions](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/protection/llms-full.txt) describes.

### Print Without a Dialog

The overloads with a `PageRange` print directly, on the standard printer or on the printer you name. [Print Without a Dialog](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/#print-without-a-dialog) in the tutorial Print a Document prints two collated copies of the first two pages on **Microsoft Print to PDF**. A `PageRange` covers the pages from its first to its last argument: `new PageRange(1, 2)` covers pages 1 to 2, and `new PageRange(3)` covers page 3 only. `Collation.Collated` prints the copies one after the other, each complete.

### Connect the WPF Print Dialog

The `PrintDialog` of `System.Windows.Controls` offers page ranges, the current page and the copies, and it passes back the printer queue and the print ticket. Its results fit the parameters of `Print`:

```csharp
private void PrintWithDialogButton_Click(object sender, RoutedEventArgs e)
{
    PrintDialog dialog = new PrintDialog();

    // Pages the user can choose from
    dialog.MinPage = 1;
    dialog.MaxPage = (uint)textControl1.Pages;
    dialog.UserPageRangeEnabled = true;
    dialog.CurrentPageEnabled = true;

    if (dialog.ShowDialog() != true)
    {
        return;
    }

    PageRange pages = new PageRange(1, textControl1.Pages);

    if (dialog.PageRangeSelection == PageRangeSelection.UserPages)
    {
        pages = dialog.PageRange;
    }
    else if (dialog.PageRangeSelection == PageRangeSelection.CurrentPage)
    {
        pages = new PageRange(textControl1.InputPosition.Page);
    }

    textControl1.Print("My Document",
        dialog.PrintQueue.FullName,
        pages,
        dialog.PrintTicket.CopyCount ?? 1,
        dialog.PrintTicket.Collation ?? Collation.Uncollated);
}
```

The code works as follows:

- `MinPage` and `MaxPage` limit the page numbers the user can enter. `UserPageRangeEnabled` and `CurrentPageEnabled` offer the **Pages** and **Current Page** options.
- `PageRangeSelection` says which option the user chose. The current page is the one with the input position, [InputPosition.Page](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/inputposition/page-property/llms-full.txt), which is available in page view.
- `PrintQueue.FullName` is the name of the selected printer. `CopyCount` and `Collation` of the `PrintTicket` are nullable, so the code supplies defaults.

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/) compares WPF printing with Windows Forms printing and also handles a selection as the page range.

### Print Exactly What the Screen Shows

A document is formatted with the text dimensions of a printer, so that line and page breaks on the screen match those on paper. [FormattingPrinter](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/formattingprinter-property/llms-full.txt) names this *formatting printer*: an installed printer, `Standard` for the standard printer of the system, or `Display` for the font dimensions of the screen. When the named printer isn't installed, the standard printer is used. A document printed on its formatting printer has the same dimensions as on the screen.

The paper sizes that the editor supports depend on the formatting printer as well. [GetSupportedPaperSizes](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/getsupportedpapersizes-method/llms-full.txt) returns them as an array of [PaperSize](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/papersize/llms-full.txt) objects, each with a name and a width and height in twips, or `null` while the control isn't completely initialized:

```csharp
private void PaperSizes_Click(object sender, RoutedEventArgs e)
{
    System.Text.StringBuilder list = new System.Text.StringBuilder();

    // TXTextControl.PaperSize, not System.Drawing.Printing.PaperSize
    TXTextControl.PaperSize[] paperSizes = textControl1.GetSupportedPaperSizes();

    foreach (TXTextControl.PaperSize paperSize in paperSizes ?? new TXTextControl.PaperSize[0])
    {
        // Width and Height are in twips; 1 inch is 1440 twips
        list.AppendLine($"{paperSize.Name}: {paperSize.Width / 1440.0:0.##} x {paperSize.Height / 1440.0:0.##} in");
    }

    MessageBox.Show(this, list.ToString(), "Paper sizes");
}
```

### Next Steps

- [Print a Document](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/llms-full.txt): the first steps of printing.
- [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/printing/llms-full.txt): printing documents in code, without the editor.
- [Viewing, Zooming, and Navigation](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/viewing/llms-full.txt): page view, pages and the input position.
- [Document Protection and Editable Regions](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/protection/llms-full.txt): documents that may not be printed.

---

## Nearby

- **Previous:** [Viewing, Zooming, and Navigation](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/viewing/llms-full.txt)
- **Next:** [Ribbon and UI](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/ribbon-ui/llms-full.txt)
