Printing

  • 5 min 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:

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);

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:

 <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.

Command Description
PrintCommand Invokes the Print dialog to print the current document.
QuickPrintCommand Prints the current document using the default printer. The command is executed without user intervention.
PrintPreviewCommand Displays 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.

DXRichEdit_Ribbon_Common

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.

Member Description
RichEditDocumentServer Initializes a new instance of the RichEditDocumentServer class with the default settings.
LegacyPrintableComponentLink A link to print Windows Forms controls that implement the IPrintable interface.
LinkBase.CreateDocument Creates a document from the link, so it can be displayed or printed.
LinkBase.PrintingSystem Gets the Printing System used to create and print a document for this link.
LinkBase.PrintDirect Prints 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).

//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 = PaperKind.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();

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.

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);
            }
        }
    }