The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

Printing

  • 4 min to read

This document outlines the techniques used to print from the RichEditDocumentServer. It consists of the following sections:

Important

The Print method works only on Windows OS. The PlatformNotSupportedException is thrown on other operating systems.

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:

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.).

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.

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.

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.

See Also