Printing
- 4 minutes to read
This document outlines the techniques used to print from the RichEditDocumentServer. It consists of the following sections:
- Print with the Default Printer
- Change Printer Settings
- DevExpress Printing Library (WinForms)
- RichEditDocumentXpfPrinter (WPF)
- Export to PDF (ASP.NET)
Print with the Default Printer
RichEditDocumentServer allows you to print a document with the default settings without end-user interaction. Use the RichEditDocumentServer.Print method as shown below to complete the task:
Note
A complete sample project is available at https://github.com/DevExpress-Examples/word-processing-document-api-examples-t418535
server.Document.AppendDocumentContent("Documents\\Grimm.docx", DocumentFormat.OpenXml);
server.Print();
Change Printer Settings
Pass the System.Drawing.Printing.PrinterSettings instance and call the RichEditDocumentServer.Print method to specify printer options (page range, number of copies, etc.).
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-print-from-the-richeditdocumentserver-t590908
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:
server.Print(printerSettings)
You can specify additional print settings (to print the comment background color or update document variables before printing) using the PrintingOptions class properties. Use the RichEditControlOptionsBase.Printing property to access an object, as shown below:
PrintingOptions printOptions = richEditDocumentServer1.Options.Printing;
printOptions.EnableCommentBackgroundOnPrint = true;
printOptions.EnablePageBackgroundOnPrint = true;
DevExpress Printing Library (WinForms)
Use the DevExpress Printing Library to set options that are unavailable from RichEditDocumentServer directly. The PrintableComponentLink class from this library allows you to change print settings (e.g., use a specific printer or disable error messages). Use the API from the table below as shown in the following code snippet to complete the task.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-print-from-the-richeditdocumentserver-using-the-printablecomponentlink-e4846
private static void PrintViaLink(RichEditDocumentServer srv)
{
if (!srv.IsPrintingAvailable) return;
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.
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);
}
RichEditDocumentXpfPrinter (WPF)
Create a FixedDocument using the RichEditDocumentXpfPrinter.CreateFixedDocument method and utilize the PrintDialog class to print this document from the RichEditDocumentServer.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-print-from-richeditdocumentserver-in-wpf-application-e3862
RichEditDocumentServer srv = new RichEditDocumentServer();
srv.LoadDocument("test.docx");
FixedDocument document = RichEditDocumentXpfPrinter.CreateFixedDocument(srv);
PrintDialog pDialog = new PrintDialog();
PrintQueueCollection queues = new PrintServer().GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local,
EnumeratedPrintQueueTypes.Connections});
System.Collections.IEnumerator localPrinterEnumerator = queues.GetEnumerator();
PrintQueue printQueue = null;
do
{
if (!localPrinterEnumerator.MoveNext())
break;
printQueue = (PrintQueue)localPrinterEnumerator.Current;
}
while (!printQueue.FullName.Contains("Canon"));
if (printQueue != null)
{
pDialog.PrintQueue = printQueue;
pDialog.PrintDocument(document.DocumentPaginator, string.Empty);
}
Export to PDF (ASP.NET)
Use the RichEditDocumentServer.ExportToPdf method which sends PDF output to a stream, as illustrated in the following code snippet:
Tip
Create the PdfExportOptions instance, set its PdfExportOptions.ShowPrintDialogOnOpen property to true and pass this object to the RichEditDocumentServer.ExportToPdf method to show the Print dialog when opening the file.
RichEditDocumentServer documentServer = new RichEditDocumentServer();
// Your code here to create, load or modify a document in the RichEditDocumentServer
using (MemoryStream stream = new MemoryStream())
{
PdfExportOptions options = new PdfExportOptions();
options.ShowPrintDialogOnOpen = true;
documentServer.ExportToPdf(stream,options);
stream.Seek(0, SeekOrigin.Begin);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline; filename=ExportedPdf.pdf");
stream.CopyTo(Response.OutputStream);
Response.End();
}
Important
If an application is hosted on Microsoft Azure, set the AzureCompatibility.Enable property to true.