Products Technologies Demo Docs Blog Support Company

Printing Using TX Text Control .NET for WPF

The printing approach in WPF is fundamentally different from printing in Windows Forms. In Windows Forms, you have a PrintDocument and events for each page that can be used for interaction. In WPF, the PrintTicket class enables you to configure the printer's features such as Collation, CopyCount and PageOrder. The PrintQueue class manages the printers and the print job itself. Important: Even though the classes PrintQueue and PrintTicket are embedded in the same namespace System.Printing,…

Printing Using TX Text Control .NET for WPF

The printing approach in WPF is fundamentally different from printing in Windows Forms. In Windows Forms, you have a PrintDocument and events for each page that can be used for interaction.

In WPF, the PrintTicket class enables you to configure the printer's features such as Collation, CopyCount and PageOrder. The PrintQueue class manages the printers and the print job itself.

Important: Even though the classes PrintQueue and PrintTicket are embedded in the same namespace System.Printing, the PrintQueue class is part of the assembly System.Printing.dll and the PrintTicket class is available through the ReachFramework.dll assembly. Make sure that you are adding both dependencies to your references.

TX Text Control .NET for WPF implements a very easy way to print without any settings to the default printer. This method is easy and works without additional settings:

textControl1.Print(string docName, true);

In order to adjust all printer and print job settings such as the collation, the number of copies and the various page selection options of the printer dialog, these settings must be handled separately. The following code opens a new PrintDialog with all page selection options enabled. The MinPage and MaxPage must be defined based on the existing pages in TextControl.

The user settings are available through the PrintTicket and PrintQueue properties of the PrintDialog class. The PageRangeSelection returns the selected page selection options. Based on these settings, a new PageRange object (customRange) is created and passed to the Print method of TextControl.

PrintDialog dlg = new PrintDialog();
dlg.MinPage = 1;
dlg.MaxPage = (uint)textControl1.Pages;
dlg.CurrentPageEnabled = true;
dlg.SelectedPagesEnabled = true;
dlg.UserPageRangeEnabled = true;

PageRange customRange = new PageRange();

if (dlg.ShowDialog() == true)
{
switch (dlg.PageRangeSelection)
{
    case PageRangeSelection.AllPages:
        // print all pages
        customRange = new PageRange((int)dlg.MinPage, (int)dlg.MaxPage);
        break;
    case PageRangeSelection.CurrentPage:
        // use the page at the current input position
        customRange = new PageRange(textControl1.InputPosition.Page);
        break;
    case PageRangeSelection.UserPages:
        // user selection
        customRange = dlg.PageRange;
        break;
    case PageRangeSelection.SelectedPages:
        // get the start and end page of the current selection
        int iEndPage = textControl1.InputPosition.Page;
        textControl1.Selection.Length = 0;
        int iStartPage = textControl1.InputPosition.Page;
        customRange = new PageRange(iStartPage, iEndPage);
        break;
}

textControl1.Print(
    "Doc",
    dlg.PrintQueue.FullName,
    customRange,
    (int)dlg.PrintTicket.CopyCount,
    (Collation)dlg.PrintTicket.Collation);

The following screenshot shows the print dialog and the various available settings:

Printing using TX Text Control .NET for WPF

The following table shows the printer dialog options and the appropriate properties:

Print dialog setting Property
Page Range: All PageRangeSelection.AllPages
Page Range: Selection PageRangeSelection.SelectedPages
Page Range: Current Page PageRangeSelection.CurrentPage
Page Range: Pages PageRangeSelection.UserPages, PageRange
Number of copies PrintTicket.CopyCount
Collate PrintTicket.Collation
Select(ed) Printer PrintQueue.FullName

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

Windows FormsGetting StartedTutorial

Windows Forms Tutorial: Create Your First Windows Forms C# Application

This tutorial shows how to create your first Windows Forms application with C# using TX Text Control .NET for Windows Forms in Visual Studio 2022.


ASP.NETASP.NET CoreMailMerge

How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#

Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file. This tutorial is a walkthrough of the steps necessary to…


AngularDocument EditorNode.js

Creating an Angular Document Editor Application with a Node.js WebSocket Server

This tutorial shows how to create an Angular application that uses the Document Editor with a Node.js WebSocket server.


ASP.NETASP.NET CoreMailMerge

Adding SVG Watermarks to Documents

This article shows how to add SVG images to document section headers that repeat automatically on each page. This watermark will be inserted vertically and horizontally centered on each section page.


ASP.NETASP.NET CoreMailMerge

Using MailMerge in ASP.NET Core 6 Web Applications

This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.