Print Rich Text Documents
- 4 minutes to read
This document outlines the following techniques used to print from RichEditControl.
- Print Documents Using the Default Printer
- Change Print Settings
- Print Documents Using the Command UI
- DevExpress Printing Library
#Use the Default Printer to Print a Document
RichEditControl allows you to print a document with the default settings without end-user interaction. To do that, use the RichEditControl.Print method as shown below:
static void buttonCustomAction_ItemClick_PrintDocumentMethod(object sender, ItemClickEventArgs e) {
RichEditControl richEdit = e.Item.Tag as RichEditControl;
if(richEdit.IsPrintingAvailable) {
richEdit.Print();
}
}
#Change Print Settings
Call the RichEditControl.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.
PrinterSettings printerSettings = new PrinterSettings();
//Set the document pages to print:
printerSettings.FromPage = 2;
printerSettings.ToPage = 3;
//Specify the number of copies:
printerSettings.Copies = 2;
//Print the document:
richEditControl1.Print(printerSettings);
Note
The Landscape, Margins, PaperPrinter
property) do not affect the printed document’s layout. Change a document section settings (accessible by the Section.
Use the PrintingOptions class properties to specify additional printing settings (whether to print the comments’ background color or update document variables before printing).
PrintingOptions printOptions = richEditControl1.Options.Printing;
printOptions.EnableCommentBackgroundOnPrint = true;
printOptions.EnablePageBackgroundOnPrint = true;
printOptions.PrintPreviewFormKind = PrintPreviewFormKind.Bars;
#Print Documents from the User Interface
All built-in printing commands are available on the File Ribbon tab. To learn how to provide your application with the Ribbon Command UI, refer to the How to: Create the RichEditControl with a Ribbon UI topic.
Tip
You can disable printing documents from the User Interface. Set the Rich
#DevExpress Printing Library
Use the DevExpress Printing Library to set options that are unavailable from RichEditControl directly. This library has the PrintableComponentLinkBase class, which allows you to change required printing settings (e.g., use the specific printer or disable showing error messages). For this, use the API from the table below as shown in the following code snippet.
Member | Description |
---|---|
Printable |
Creates a new Printable |
Printable |
Sets user implementation represented by the IPrintable instance for the current component link. |
Printable |
Obtains settings provided by the given Printing |
Printable |
Prints the current document using the specified printer. |
private static void PrintViaLink(IPrintable srv)
{
PrintableComponentLink link = new PrintableComponentLink(new PrintingSystem());
link.Component = srv;
// Disable warnings.
link.PrintingSystem.ShowMarginsWarning = false;
link.PrintingSystem.ShowPrintStatusDialog = false;
// Find a printer containing Canon in its name
PrinterSettings settings = new PrinterSettings();
string printerName = String.Empty;
for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++) {
string pName = PrinterSettings.InstalledPrinters[i];
if (pName.Contains("Canon")) {
printerName = pName;
break;
}
}
// Print to the specified printer.
link.Print(printerName);
}
Note
The Margins, Page