# Print a Document

> Print a Windows Forms TextControl document with the print dialog, PrintPreview or a PrintDocument for pages and copies, and print files with ServerTextControl.

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

---

Printing in a Windows Forms application with TX Text Control uses the printing of .NET: the standard print dialog, the print preview, and a `PrintDocument` for printing without asking the user. The button handlers below print the document of the editor in each of these ways, print a file the editor never shows, and explain why the formatting printer matters for what comes out of the printer.

### Prerequisites

- The application from [Create Your First Windows Forms Application](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/first-application/llms-full.txt), with a `TextControl` named `textControl1` on `Form1`.
- For each section, a `Button` on the form. Double-click it in the designer to create its `Click` handler.
- A printer. The examples print to **Microsoft Print to PDF**, which Windows provides. Replace its name with the name of your printer.

*Form1.cs* needs these `using` directives:

```csharp
using System.Drawing.Printing;
using TXTextControl;
```

### Print With the Print Dialog

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

    // Open the print dialog. The name identifies the print job.
    textControl1.Print("My Document");
}
```

- [Print](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/print-method/llms-full.txt) with a document name opens the standard print dialog, in which the user selects the printer and the pages, and then prints the document.
- [CanPrint](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/canprint-property/llms-full.txt) tells whether the document may be printed. A protected document can forbid printing.

### Show a Print Preview

```csharp
private void previewButton_Click(object sender, EventArgs e)
{
    textControl1.PrintPreview("My Document");
}
```

[PrintPreview](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/printpreview-method/llms-full.txt) shows a preview of the current document. Another overload takes a `PrintDocument` and shows the preview with its settings.

### Print Without a Dialog

A `PrintDocument` of .NET carries the settings a print job needs. Pass it to `Print`, and TX Text Control prints with these settings, without asking the user:

```csharp
private void quickPrintButton_Click(object sender, EventArgs e)
{
    PrintDocument printDocument = new PrintDocument();

    printDocument.DocumentName = "My Document";
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
    printDocument.PrinterSettings.Copies = 2;

    // Print pages 1 to 2 only. PrintRange must be SomePages.
    printDocument.PrinterSettings.PrintRange = PrintRange.SomePages;
    printDocument.PrinterSettings.FromPage = 1;
    printDocument.PrinterSettings.ToPage = Math.Min(2, textControl1.Pages);

    // Suppress the "Page n of document" status dialog
    printDocument.PrintController = new StandardPrintController();

    textControl1.Print(printDocument);
}
```

The code works as follows:

- TX Text Control uses the document name, the print controller and the printer settings of the `PrintDocument`. [The Print Methods](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/printing/#the-print-methods) lists the settings it reads.
- A page range takes effect only with `PrintRange` set to `PrintRange.SomePages`. [Pages](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/pages-property/llms-full.txt) returns the number of pages of the document.
- .NET shows a status dialog with the page being printed. A `StandardPrintController` prints without it.

### Print a File Without the Editor

A [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/llms-full.txt) loads and prints a document without showing it. It has no print dialog, so it always prints with a `PrintDocument`:

```csharp
private void printFileButton_Click(object sender, EventArgs 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";

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

[ServerTextControl.Print](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/print-method/llms-full.txt) uses the same settings of the `PrintDocument` as the editor.

### Format the Document for the Printer

TX Text Control formats a document with the text dimensions of a printer, so that lines and pages break on the screen exactly as on paper. [FormattingPrinter](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/formattingprinter-property/llms-full.txt) names that printer:

```csharp
private void formatForPrinterButton_Click(object sender, EventArgs e)
{
    // Line and page breaks now match this printer
    textControl1.FormattingPrinter = "Microsoft Print to PDF";
}
```

The values `Standard` and `Display` and what happens with a printer that is not installed are described in [Print Exactly What the Screen Shows](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/printing/#print-exactly-what-the-screen-shows).

### Next Steps

- [Printing and Print Preview](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/printing/llms-full.txt): printing from the editor in detail, including single pages.
- [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/printing/llms-full.txt): printing in code, batch printing and background output.
- [Save as DOCX and PDF](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-processing/save-docx-pdf/llms-full.txt): save the document instead of printing it.
- The blog article [Print MS Word DOCX Documents in .NET using C# Without Interop](https://www.textcontrol.com/blog/2024/06/28/print-ms-word-docx-documents-in-net-using-c-sharp-without-interop/): printing DOCX files with `ServerTextControl`.

---

## Nearby

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