Back to Devexpress

Print Rich Text Documents

wpf-11217-controls-and-libraries-rich-text-editor-printing.md

latest10.6 KB
Original Source

Print Rich Text Documents

  • Nov 29, 2023
  • 4 minutes to read

This document outlines the following techniques used to print from RichEditControl:

RichEditControl allows you to print a document with the default settings without end-user interaction by calling the RichEditControl.Print method.

Call the Print method with the passed PrinterSettings instance to specify the desired printer options (the range of pages to print, the number of copies, etc.) to print a document, as shown below:

csharp
if (layoutCheckItem.IsChecked == true) { ChangeDocumentLayout(); }
PrinterSettings printerSettings = new PrinterSettings();
printerSettings.FromPage = 2;
printerSettings.ToPage = 3;
MessageBox.Show("Printing "+ printerSettings.Copies.ToString()+" copy(ies) of pages "+ printerSettings.FromPage+ "-"+ printerSettings.ToPage);
richEditControl1.Print(printerSettings);
vb
If layoutCheckItem.IsChecked = True Then
    ChangeDocumentLayout()
End If
Dim printerSettings As New PrinterSettings()
printerSettings.FromPage = 2
printerSettings.ToPage = 3
MessageBox.Show("Printing " & printerSettings.Copies.ToString() & " copy(ies) of pages " & printerSettings.FromPage & "-" & printerSettings.ToPage)
richEditControl1.Print(printerSettings)

You can specify additional printing settings (whether to print the comments’ background color or update document variables before printing) using the DXRichEditPrintingOptions class properties. Access an object using the RichEditControl.PrintingOptions property, as shown below:

xaml
<dxre:RichEditControl.PrintingOptions>
                    <dxre:DXRichEditPrintingOptions PrintPreviewFormKind="Bars"
                                                     EnableCommentBackgroundOnPrint="False"
                                                     UpdateDocVariablesBeforePrint="False"/>
                    </dxre:RichEditControl.PrintingOptions>

Commands

DXRichEdit provides the following built-in commands for printing documents.

CommandDescription
PrintCommandInvokes the Print dialog to print the current document.
QuickPrintCommandPrints the current document using the default printer. The command is executed without user intervention.
PrintPreviewCommandDisplays the Print Preview window for the current document.

All built-in commands are available on the File ribbon tab. To learn how to provide an application with the Ribbon Command UI, check the Lesson 5 - Create Separate Ribbon Pages for a Rich Text Editor topic.

Tip

You can restrict end-users from printing documents. Set the DXRichEditBehaviorOptions.Printing property to DocumentCapability.Disabled or DocumentCapability.Hidden to disable or hide the corresponding commands in the Ribbon UI and the pop-up menu.

DevExpress Printing Library

To specify additional printing settings, use the DevExpress Printing Library and the RichEditDocumentServer instance. The API from the table below allows you to accomplish this task as shown in the following code example.

MemberDescription
RichEditDocumentServerInitializes a new instance of the RichEditDocumentServer class with the default settings.
LegacyPrintableComponentLinkA link to print Windows Forms controls that implement the IPrintable interface.
LinkBase.CreateDocumentCreates a document from the link, so it can be displayed or printed.
LinkBase.PrintingSystemGets the Printing System used to create and print a document for this link.
LinkBase.PrintDirectPrints the current document to a default printer.

Important

Such LegacyPrintableComponentLink properties as LinkBase.Landscape or LinkBase.PaperKind do not affect the layout of a printed document. To change the document page layout properties before printing, change the corresponding settings of the document section (can be accessed by the Section.Page property).

csharp
//Initialize a new server and printer 
PrintDialog printDialog = new PrintDialog();
RichEditDocumentServer docServer = new RichEditDocumentServer();

//Pass the document content to the server  
docServer.RtfText = richEdit.RtfText;

//Change the document layout
docServer.Document.Sections[0].Page.Landscape = true;
docServer.Document.Sections[0].Page.PaperKind = DXPaperKind.A4;

//Create a new component link 
LegacyPrintableComponentLink printableComponent = new LegacyPrintableComponentLink(docServer);

//Create a document to print 
printableComponent.CreateDocument(false);

//Silently print the document
printableComponent.PrintDirect();
vb
'Initialize a new server and printer 
Dim printDialog As New PrintDialog()
Dim docServer As New RichEditDocumentServer()

'Pass the document content to the server  
docServer.RtfText = richEdit.RtfText

'Change the document layout
docServer.Document.Sections(0).Page.Landscape = True
docServer.Document.Sections(0).Page.PaperKind = DXPaperKind.A4

'Create a new component link 
Dim printableComponent As New LegacyPrintableComponentLink(docServer)

'Create a document to print 
printableComponent.CreateDocument(False)

'Silently print the document
printableComponent.PrintDirect()

XpfRichEditPrinter class descendant

To use the FixedDocument instance in your custom printing logic, use the XpfRichEditPrinter class. Declare a class descendant and implement the methods according to your current needs.

The code snippet below shows how to use this class to set a printer directly from code without showing the printer dialog.

csharp
using System.Printing;
using System.Windows;
using System.Windows.Controls;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Printing;
using System.Windows.Documents;
using System.Drawing.Printing;
    public class CustomXpfRichEditPrinter : XpfRichEditPrinter
    {
        public CustomXpfRichEditPrinter(IRichEditControl control)
            : base(control) {}

        public void PrintToMyPrinter()
        {
            PrintDialog pDialog = new PrintDialog();
            PrintQueueCollection queues = new PrintServer().GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local,
                EnumeratedPrintQueueTypes.Connections });
            System.Collections.IEnumerator localPrinterEnumerator = queues.GetEnumerator();
            PrintQueue printQueue = null;

            if (localPrinterEnumerator.MoveNext()) {
                printQueue = (PrintQueue)localPrinterEnumerator.Current;
            }

            if (printQueue != null) {
                pDialog.PrintQueue = printQueue;
                FixedDocument document = this.CreateFixedDocument();
                pDialog.PrintDocument(document.DocumentPaginator, string.Empty);
            }
        }
    }
vb
Imports System.Printing
Imports System.Windows
Imports System.Windows.Controls
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.Printing
Imports System.Windows.Documents
Imports System.Drawing.Printing
    Public Class CustomXpfRichEditPrinter
        Inherits XpfRichEditPrinter
        Public Sub New(ByVal control As IRichEditControl)
            MyBase.New(control)
        End Sub

        Public Sub PrintToMyPrinter()
            Dim pDialog As New PrintDialog()
            Dim enumerationFlags() As EnumeratedPrintQueueTypes = {EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections}
            Dim queues As PrintQueueCollection = New PrintServer().GetPrintQueues(enumerationFlags)
            Dim localPrinterEnumerator As System.Collections.IEnumerator = queues.GetEnumerator()
            Dim printQueue As PrintQueue = Nothing

            If localPrinterEnumerator.MoveNext() Then
                printQueue = CType(localPrinterEnumerator.Current, PrintQueue)
            End If

            If printQueue IsNot Nothing Then
                pDialog.PrintQueue = printQueue
                Dim document As FixedDocument = Me.CreateFixedDocument()
                pDialog.PrintDocument(document.DocumentPaginator, String.Empty)
            End If
        End Sub
    End Class