# Printing and Print Preview

> Print from a Windows Forms TextControl: Print and PrintPreview overloads, a PrintDialog with page ranges, single pages from PrintPage and the FormattingPrinter.

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

---

The Windows Forms `TextControl` prints with the printing classes of .NET: the standard print dialog, a print preview and a `PrintDocument` that carries the printer, the pages and the copies. [Print a Document](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/print/llms-full.txt) is the quick start. What follows goes further: print dialogs with page ranges, previews with settings, single pages printed from your own `PrintDocument`, and documents whose protection forbids printing.

The handlers go into *Form1.cs* of a form with a `TextControl` named `textControl1`, like the one of [Create Your First Windows Forms Application](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/first-application/llms-full.txt), and need `using System.Drawing.Printing;` besides `using TXTextControl;`.

### The Print Methods

[Print](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/print-method/llms-full.txt) and [PrintPreview](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/printpreview-method/llms-full.txt) have these overloads:

| Call | Result |
|---|---|
| `Print(string docName)` | Opens the standard print dialog and prints with the settings the user chooses |
| `Print(PrintDocument printDocument)` | Prints without a dialog, with the settings of the `PrintDocument` |
| `Print(int page, PrintPageEventArgs ppe)` | Prints one page, from the `PrintPage` event of a `PrintDocument` |
| `PrintPreview(string docName)` | Shows a print preview of the document |
| `PrintPreview(PrintDocument printDocument)` | Shows a print preview with the settings of the `PrintDocument` |

Which settings of a `PrintDocument` TX Text Control uses, such as the printer, the copies and a page range with `PrintRange.SomePages`, is listed in [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/printing/#how-printing-works).

### Let the User Choose the Pages

`Print(string)` opens a print dialog of its own. To offer the current page as well, open the `PrintDialog` of Windows Forms with a `PrintDocument` and print with its settings:

```csharp
private void printButton_Click(object sender, EventArgs e)
{
    if (!textControl1.CanPrint)
    {
        MessageBox.Show("This document cannot be printed.");
        return;
    }

    using (PrintDocument printDocument = new PrintDocument())
    using (PrintDialog printDialog = new PrintDialog())
    {
        printDocument.DocumentName = "My Document";

        // Pages the user can enter in the dialog
        printDocument.PrinterSettings.MinimumPage = 1;
        printDocument.PrinterSettings.MaximumPage = textControl1.Pages;
        printDocument.PrinterSettings.FromPage = 1;
        printDocument.PrinterSettings.ToPage = textControl1.Pages;

        printDialog.Document = printDocument;
        printDialog.AllowSomePages = true;
        printDialog.AllowCurrentPage = true;

        if (printDialog.ShowDialog(this) != DialogResult.OK)
        {
            return;
        }

        // Translate "Current Page" into a page range
        if (printDocument.PrinterSettings.PrintRange == PrintRange.CurrentPage)
        {
            int currentPage = textControl1.InputPosition.Page;
            printDocument.PrinterSettings.PrintRange = PrintRange.SomePages;
            printDocument.PrinterSettings.FromPage = currentPage;
            printDocument.PrinterSettings.ToPage = currentPage;
        }

        textControl1.Print(printDocument);
    }
}
```

The code works as follows:

- [CanPrint](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/canprint-property/llms-full.txt) is `false` when the document is protected and its [AllowPrinting](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/documentpermissions/allowprinting-property/llms-full.txt) permission is turned off; see [Document Protection and Editable Regions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/protection/llms-full.txt).
- [Pages](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/pages-property/llms-full.txt) is the number of pages, which limits the page numbers of the dialog.
- The dialog writes the choices of the user into the `PrinterSettings` of the `PrintDocument`: the printer, the copies, the page range.
- `PrintRange.CurrentPage` is turned into the range of the page with the input position, [InputPosition.Page](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/inputposition/page-property/llms-full.txt), which is available in page view.

### Show a Print Preview

`PrintPreview` shows a preview of the document as it will be printed. With a `PrintDocument`, the preview uses its settings, for example a page range:

```csharp
private void previewButton_Click(object sender, EventArgs e)
{
    using (PrintDocument printDocument = new PrintDocument())
    {
        printDocument.DocumentName = "My Document";

        // Preview the first two pages only
        printDocument.PrinterSettings.PrintRange = PrintRange.SomePages;
        printDocument.PrinterSettings.FromPage = 1;
        printDocument.PrinterSettings.ToPage = Math.Min(2, textControl1.Pages);

        textControl1.PrintPreview(printDocument);
    }
}
```

### Print Pages From Your Own PrintDocument

`Print(int page, PrintPageEventArgs ppe)` renders one page of the document onto the page that a `PrintDocument` is printing. Your code handles the `PrintPage` event and decides which page of the document goes where. The following code prints the page with the input position:

```csharp
private int pageToPrint;

private void printCurrentPageButton_Click(object sender, EventArgs e)
{
    pageToPrint = textControl1.InputPosition.Page;

    using (PrintDocument printDocument = new PrintDocument())
    {
        printDocument.DocumentName = "Current page";
        printDocument.PrintPage += PrintDocument_PrintPage;
        printDocument.Print();
    }
}

private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
    // Render the page of the document on the printed page
    textControl1.Print(pageToPrint, e);

    // One sheet only
    e.HasMorePages = false;
}
```

`PrintDocument.Print` starts the print job, .NET raises `PrintPage` for each sheet, and `textControl1.Print(pageToPrint, e)` draws the page with the given number, counted from 1. `HasMorePages` set to `false` ends the job after one sheet. [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/printing/#print-several-documents-in-one-print-job) uses the same overload to print several pages and documents in one print job, or to draw your own content under a page.

### Print Exactly What the Screen Shows

TX Text Control formats a document with the text dimensions of a printer, which [FormattingPrinter](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/formattingprinter-property/llms-full.txt) names, so that lines and pages break on paper as on the screen when the document is printed on that printer. [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/printing/#choose-the-formatting-printer) lists its values.

The paper sizes that the editor supports depend on the formatting printer as well. [GetSupportedPaperSizes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/getsupportedpapersizes-method/llms-full.txt) returns them as an array of [PaperSize](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/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 paperSizesButton_Click(object sender, EventArgs 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(list.ToString(), "Paper sizes");
}
```

### Next Steps

- [Print a Document](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/print/llms-full.txt): the first steps of printing, and printing files with `ServerTextControl`.
- [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/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-winforms/document-editor/viewing/llms-full.txt): the page view and the input position that printing relies on.
- [Document Protection and Editable Regions](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/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-winforms/document-editor/viewing/llms-full.txt)
- **Next:** [Ribbon and UI](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/ribbon-ui/llms-full.txt)
