Skip to main content
All docs
V25.2
  • How to: Use DevExpress Printing Library to Print Word Documents

    • 3 minutes to read

    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 (for example, 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 following code snippet 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);
            }
        }
    }