windowsforms-118355-controls-and-libraries-rich-text-editor-printing.md
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. 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();
}
}
Private Shared Sub buttonCustomAction_ItemClick_PrintDocumentMethod(ByVal sender As Object, ByVal e As ItemClickEventArgs)
Dim richEdit As RichEditControl = TryCast(e.Item.Tag, RichEditControl)
If richEdit.IsPrintingAvailable Then
richEdit.Print()
End If
End Sub
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);
Dim printerSettings As 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, PaperSize properties (accessible by the PrinterSettings.DefaultPageSettings property) do not affect the printed document’s layout. Change a document section settings (accessible by the Section.Page property) to modify the page layout properties before printing.
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;
Dim printOptions As PrintingOptions = richEditControl1.Options.Printing
printOptions.EnableCommentBackgroundOnPrint = True
printOptions.EnablePageBackgroundOnPrint = True
printOptions.PrintPreviewFormKind = PrintPreviewFormKind.Bars
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.
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 |
|---|---|
| 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. |
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);
}
Private Shared Sub PrintViaLink(ByVal srv As IPrintable)
Dim link As 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
Dim settings As New PrinterSettings()
Dim printerName As String = String.Empty
For i As Integer = 0 To PrinterSettings.InstalledPrinters.Count - 1
Dim pName As String = PrinterSettings.InstalledPrinters(i)
If pName.Contains("Canon") Then
printerName = pName
Exit For
End If
Next i
' Print to the specified printer.
link.Print(printerName)
End Sub
Note
The Margins, PageHeaderFooter, PaperKind properties do not affect the printed document’s layout. Change a document section settings (accessible by the Section.Page property) to modify the page layout properties before printing.
See Also
How to: Utilize a Print Preview Dialog to Adjust Margins in the RichEditControl Document
How to: Use the PrintableComponentLink to Print DevExpress Controls