Print Word Documents
- 3 minutes to read
This document outlines the techniques used to print from the Word Processing Document API.
Print with the Default Printer
Use the RichEditDocumentServer.Print method to print a document with the default settings without end user interaction.
// 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.
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.
Print Word Documents in Unix-based 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);