Skip to main content

Print Word Documents

  • 6 minutes to read

This document outlines the techniques used to print from the Word Processing Document API.

Use the RichEditDocumentServer.Print method to print a document with the default settings without end user interaction.

View Example

// Load a document from a file.
wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);

// Print the document to the default printer with the default settings.
wordProcessor.Print();

Change Printer Settings

Pass the PrinterSettings instance as the RichEditDocumentServer.Print method parameter to specify printer options (page range, number of copies, etc.).

Warning

The Print(PrinterSettings) method oberload works only on Windows OS. The PlatformNotSupportedException is thrown on other operating systems. Use the Print method overloads with DXPrinterSettings printerSettings parameter to print on other operating systems.

View Example

using DevExpress.XtraRichEdit;
using System.Drawing.Printing;

using (var wordProcessor = new RichEditDocumentServer())
{
    wordProcessor.LoadDocument("Grimm.docx");

    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: 
    wordProcessor.Print(printerSettings);
}

Note

The Margins or Landscape property (accessible by the PrinterSettings.DefaultPageSettings property) do not affect the printed document’s layout. Change document section settings (accessible by the Section.Page property) to modify page layout properties before printing.

You can use the PrintingOptions class properties to specify additional print settings (to print the comment background color or update document variables before printing).

using DevExpress.XtraRichEdit;

PrintingOptions printOptions = wordProcessor.Options.Printing;
printOptions.EnableCommentBackgroundOnPrint = false;
printOptions.EnablePageBackgroundOnPrint = true;

Warning

The Print(PrinterSettings) method overload works only on Windows OS. The PlatformNotSupportedException is thrown on other operating systems. Use the Print method overloads with DXPrinterSettings printerSettings parameter to print on other operating systems.

Use the Print method overloads with the DXPrinterSettings printerSettings parameter to print documents in non-Windows environments ( macOS and Unix-based systems that support printing through Common UNIX Printing System (CUPS)).

Note

Install the libcups2 package separately to enable printing.

The following code sample specifies printing settings and prints the loaded document:

using DevExpress.XtraRichEdit;
using DevExpress.Drawing.Printing;

using RichEditDocumentServer wordProcessor = new RichEditDocumentServer();
wordProcessor.LoadDocument(@"C:\\Downloads\Document.docx");

DXPrinterSettings printerSettings = new DXPrinterSettings();
printerSettings.Copies = 2;
printerSettings.PageRange = "2-3";

wordProcessor.Print(printerSettings);

DevExpress Printing Library (WinForms)

Use the DevExpress Printing Library to set options that are unavailable from RichEditDocumentServer directly. The PrintableComponentLinkBase class from this library allows you to change print settings (e.g., use a specific printer or disable error messages).

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.

The code sample below changes document layout settings and prints a document with the specified printer.

View Example

using DevExpress.XtraPrinting;
using DevExpress.XtraPrintingLinks;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System;
using DevExpress.Drawing.Printing;
using System.Windows.Forms;


private void btn_PrintFromServer_Click(object sender, EventArgs e)
{
    using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
    {
        wordProcessor.LoadDocument("Grimm.docx");

        // Specify page layout settings.
        SetPrintOptions(wordProcessor);
        wordProcessor.EndUpdate();

        // Create a printable link to print a document.
        PrintWithLink(wordProcessor);
    }
}
private static void SetPrintOptions(IRichEditDocumentServer richEdit)
{
    foreach (Section _section in richEdit.Document.Sections)
    {
        _section.Page.PaperKind = DXPaperKind.A4;
        _section.Page.Landscape = false;
        _section.Margins.Left = 150f;
        _section.Margins.Right = 150f;
        _section.Margins.Top = 50f;
        _section.Margins.Bottom = 50f;
        _section.PageNumbering.NumberingFormat = NumberingFormat.CardinalText;
        _section.PageNumbering.FirstPageNumber = 0;
    }
}
private static void PrintWithLink(RichEditDocumentServer wordProcessor)
{
    if (!wordProcessor.IsPrintingAvailable) return;
    using (PrintingSystemBase ps = new PrintingSystemBase()) {
        using (PrintableComponentLinkBase link = new PrintableComponentLinkBase(ps)) {
            link.Component = wordProcessor;

            // Disable warnings.
            ps.ShowMarginsWarning = false;
            ps.ShowPrintStatusDialog = false;

            // Find a printer containing 'PDF' in its name.
            string printerName = String.Empty;
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                string pName = PrinterSettings.InstalledPrinters[i];
                if (pName.Contains("PDF"))
                {
                    printerName = pName;
                    break;
                }
            }

            // Run document creation
            link.CreateDocument();

            // Print to the specified printer.
            PrintToolBase tool = new PrintToolBase(ps);
            tool.Print(printerName);
        }
    }
}

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.

using DevExpress.XtraPrinting;
using DevExpress.XtraRichEdit;
using System;
using System.IO;

using (var wordProcessor = new RichEditDocumentServer()) {

    // Your code here to create, load or modify a document

    using (MemoryStream stream = new MemoryStream()) {
        PdfExportOptions options = new PdfExportOptions();
        options.ShowPrintDialogOnOpen = true;
        wordProcessor.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();
    }
}
See Also