Printing
- 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
Print Documents Using the Default Printer
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:
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-the-richeditcontrols-common-api-t210437
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.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-print-from-the-xtrarichedit-t595964
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);
You can specify additional printing settings (whether to print the comments’ background color or update document variables before printing) using the PrintingOptions class properties. Access an object using the RichEditControlOptionsBase.Printing property, as shown below:
PrintingOptions printOptions = richEditControl1.Options.Printing;
printOptions.EnableCommentBackgroundOnPrint = true;
printOptions.EnablePageBackgroundOnPrint = true;
printOptions.PrintPreviewFormKind = PrintPreviewFormKind.Bars;
Print Documents using the Command UI
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 RichEditBehaviorOptions.Printing property to DocumentCapability.Hidden or DocumentCapability.Disabled to disable or hide the corresponding commands in the Ribbon UI and the pop-up menu.
DevExpress Printing Library
To set options that are unavailable from RichEditControl directly, use the DevExpress Printing Library. This library has the PrintableComponentLink 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 |
---|---|
PrintableComponentLink | Creates a new PrintableComponentLink instance with a given printing system. |
PrintableComponentLinkBase.Component | Sets user implementation represented by the IPrintable instance for the current component link. |
PrintableComponentLink.PrintingSystem | Obtains settings provided by the given PrintingSystem instance. |
PrintableComponentLink.Print | Prints the current document using the specified printer. |
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-use-different-printing-techniques-and-set-printing-options-in-code-e3882
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);
}
Important
Such PrintableComponentLink 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 a document section ( accessed by the Section.Page property).