How to: Print a Document using the PrintableComponentLink

  • 3 minutes to read

This example demonstrates how to use a PrintableComponentLinkBase to print from the RichEditDocumentServer. An instance of the RichEditDocumentServer loads a document, inserts a datetime stamp in its header and prints it. Various print options, such as page size, orientation and margins are set in code at run time using the Section interface. The application searches a list of installed printers for a printer containing a certain text (“Canon”) in its name and prints to that printer. If a printer with the specified name is not found, the default printer is used.

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

namespace RichEdit_PrintingSystem
{
public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    private void btn_PrintFromServer_Click(object sender, EventArgs e)
    {
        RichEditDocumentServer srv = new RichEditDocumentServer();
        srv.LoadDocument("Grimm.docx", DocumentFormat.OpenXml);
        // Insert a field displaying the current date/time into the document header.
        srv.BeginUpdate();
        SubDocument  _header = srv.Document.Sections[0].BeginUpdateHeader();
        _header.Fields.Create(_header.Range.Start,"DATE \\@ \"dddd, MMMM dd, yyyy  HH:mm:ss\" \\MERGEFORMAT");
        _header.Paragraphs[0].Alignment = ParagraphAlignment.Right;
        srv.Document.Sections[0].EndUpdateHeader(_header);
        // Specify page margins, orientation, etc.
        SetPrintOptions(srv);
        srv.EndUpdate();
        // Display field values instead of code.
        srv.Options.MailMerge.ViewMergedData = true;
        // Create a printable link to print a document.
        PrintViaLink(srv);
    }

    private static void SetPrintOptions(IRichEditDocumentServer richedit)
    {
        foreach (Section _section in richedit.Document.Sections) {
            _section.Page.PaperKind = System.Drawing.Printing.PaperKind.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 PrintViaLink(RichEditDocumentServer srv)
    {
        if (!srv.IsPrintingAvailable) return;
        using (PrintingSystemBase ps = new PrintingSystemBase())
        using (PrintableComponentLinkBase link = new PrintableComponentLinkBase(ps)) {
            link.Component = srv;
            // Disable warnings.
            ps.ShowMarginsWarning = false;
            ps.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("PDF")) {
                    printerName = pName;
                    break;
                }
            }

        //Run document creation
        link.CreateDocument();

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

Important

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