Printing and Print Preview
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 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, and need using System.Drawing.Printing; besides using TXTextControl;.
The Print Methods
Print and PrintPreview 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.
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:
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 is
falsewhen the document is protected and its AllowPrinting permission is turned off; see Document Protection and Editable Regions. - Pages is the number of pages, which limits the page numbers of the dialog.
- The dialog writes the choices of the user into the
PrinterSettingsof thePrintDocument: the printer, the copies, the page range. PrintRange.CurrentPageis turned into the range of the page with the input position, InputPosition.Page, 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:
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:
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 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 names, so that lines and pages break on paper as on the screen when the document is printed on that printer. Printing lists its values.
The paper sizes that the editor supports depend on the formatting printer as well. GetSupportedPaperSizes returns them as an array of PaperSize objects, each with a name and a width and height in twips, or null while the control isn't completely initialized:
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: the first steps of printing, and printing files with
ServerTextControl. - Printing: printing documents in code, without the editor.
- Viewing, Zooming, and Navigation: the page view and the input position that printing relies on.
- Document Protection and Editable Regions: documents that may not be printed.