Print a Document
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, with a
TextControlnamedtextControl1onForm1. - For each section, a
Buttonon the form. Double-click it in the designer to create itsClickhandler. - 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:
using System.Drawing.Printing;
using TXTextControl;
Print With the Print Dialog
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");
}
Show a Print Preview
private void previewButton_Click(object sender, EventArgs e)
{
textControl1.PrintPreview("My Document");
}
PrintPreview 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:
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 lists the settings it reads. - A page range takes effect only with
PrintRangeset toPrintRange.SomePages. Pages returns the number of pages of the document. - .NET shows a status dialog with the page being printed. A
StandardPrintControllerprints without it.
Print a File Without the Editor
A ServerTextControl loads and prints a document without showing it. It has no print dialog, so it always prints with a PrintDocument:
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 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 names that printer:
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.
Next Steps
- Printing and Print Preview: printing from the editor in detail, including single pages.
- Printing: printing in code, batch printing and background output.
- Save as DOCX and PDF: save the document instead of printing it.
- The blog article Print MS Word DOCX Documents in .NET using C# Without Interop: printing DOCX files with
ServerTextControl.