TX Text Control .NET for Windows Forms

Printing and Print Preview

Topic

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:

CallResult
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 false when 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 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, 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);
    }
}

Next Steps