Printing
- 3 minutes to read
This document outlines the following techniques used to print from RichEditControl.
- Print Documents Using the Default Printer
- Change Printing Settings
- Print Documents Using the Command UI
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 Printing Settings
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).
Print Documents using the Command UI
RichEditControl provides the following built-in commands to print the document.
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 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 a Simple Word Processor with a Ribbon UI topic.