How to: Convert a Word Document to PDF Format

  • 6 minutes to read

Use the RichEditDocumentServer.ExportToPdf method to save a document as a PDF file. You can pass a PdfExportOptions class instance to this method to define PDF export options.

The following code sample specifies PDF export options and converts a DOCX document to PDF:

View Example

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

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    // Load a DOCX document.
    wordProcessor.LoadDocument("Documents\\MovieRentals.docx", DocumentFormat.OpenXml);

    // Specify export options.
    PdfExportOptions options = new PdfExportOptions();
    options.DocumentOptions.Author = "Mark Jones";
    options.Compressed = false;
    options.ImageQuality = PdfJpegImageQuality.Highest;

    // Export the document to a stream.
    using (FileStream pdfFileStream = new FileStream("Document_PDF.pdf", FileMode.Create))
    {
        wordProcessor.ExportToPdf(pdfFileStream, options);
    }
}

Export a Document to Accessible PDF

You can save a document as a tagged (accessible) PDF document. Accessible PDF formats allow users with disabilities to use screen readers and other assistive technologies to read information from PDF documents.

You can generate PDF files that conform to the following standards:

  • PDF/UA
  • PDF/A-1a
  • PDF/A-2a
  • PDF/A-3a

Run Demo: Save Word Documents as Accessible PDF Files

Generate PDF/UA Documents

The PDF/UA (Universal Accessibility) standard contains requirements for PDF documents to ensure accessibility and support for assistive technology. Requirements for PDF/UA compliance are consistent with the Web Content Accessibility Guidelines (WCAG) 2.0.

Below are some of the constraints the standard defines for PDF documents:

  • All meaningful content should be tagged and included in the structure tree of document tags. Objects that are not relevant for understanding document content are marked as artifacts (for example, background and decorative images, repeating information in headers and footers, page numbers).

  • The structure tree generated by document tags must reflect the document’s logical reading order.

  • Information should not be conveyed by visual means alone (for example, contrast, color, or position on the page).

  • All pictorial elements (image objects and other non-text objects such as vector objects or object groups) should have alternative text.

  • All fonts should be embedded.

To generate a document that conforms to the PDF/UA standard, create a PdfExportOptions class instance and set the PdfExportOptions.PdfUACompatibility property to PdfUA1. Pass the class instance to the RichEditDocumentServer.ExportToPdf method to save a document as a PDF/UA document.

using DevExpress.XtraRichEdit;
using DevExpress.XtraPrinting;
// ...

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    // Load a DOCX document.
    wordProcessor.LoadDocument("Documents\\MovieRentals.docx", DocumentFormat.OpenXml);

    // Specify PDF export options.
    PdfExportOptions options = new PdfExportOptions();

    // Specify document compatibility with the PDF/UA specification.
    options.PdfUACompatibility = PdfUACompatibility.PdfUA1;

    // Export the document to PDF.
    wordProcessor.ExportToPdf(@"Documents\Output_Document.pdf", options);
}

Generate Accessible PDF/A Documents

PDF/A is an archival PDF format used for long-term preservation of electronic documents. The PDF/A standard includes the following restrictions:

  • All PDF/A versions implicitly prohibit encryption.
  • All fonts that are used in PDF/A documents should be embedded.
  • The PDF/A-1 and PDF/A-2 standards do not support attachments.
  • The PDF/A-1 standard does not support transparency (the alpha channel in images is ignored).

To export a document to a PDF document that supports the PDF/A Conformance Level A (Accessible), create a PdfExportOptions class instance and set the PdfExportOptions.PdfACompatibility property to one of the following values: PdfA1a, PdfA2a, or PdfA3a. Pass the class instance to the RichEditDocumentServer.ExportToPdf method to generate a PDF file.

The following code sample saves a DOCX document as a PDF file that conforms to the PDF/A-3a standard:

using DevExpress.XtraRichEdit;
using DevExpress.XtraPrinting;
// ...

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
    // Load a DOCX document.
    wordProcessor.LoadDocument("Documents\\MovieRentals.docx", DocumentFormat.OpenXml);

    // Specify PDF export options.
    PdfExportOptions options = new PdfExportOptions();

    // Specify document compatibility with the PDF/A-3a specification.
    options.PdfACompatibility = PdfACompatibility.PdfA3a;

    // Export the document to PDF.
    wordProcessor.ExportToPdf(@"Documents\Output_Document.pdf", options);
}

Asynchronous Export

Important

The RichEditDocumentServerExtensions class is defined in the DevExpress.Docs.v21.2.dll assembly. Add this assembly to your project to use the RichEditDocumentServerExtensions members. You need an active license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this library in production code.

Use the RichEditDocumentServerExtensions.ExportToPdfAsync method to export a document to PDF asynchronously. The method overloads allow you to define PDF export options and cancel the operation if needed.

Important

Take into account the following when you call this method:

  • The events fired by this method call may occur in a different thread than the target operation.

  • The operation is not thread safe (the document should not be accessed simultaneously by different threads). Wait until the operation is completed before you continue to work with the document (for example, use the await operator).

The following code sample converts a DOCX file to PDF format asynchronously and cancels the operation if it takes longer than 10 seconds:

using DevExpress.XtraRichEdit;
using System;
using System.Threading;
using System.Threading.Tasks;
//...

private async void ConvertDocx2PdfWithCancellation()
{
    // Specify the cancellation token.
    using (CancellationTokenSource source = new CancellationTokenSource(10000))
    {
        using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
        {
            try
            {
                // Asynchronously load the DOCX file.
                await wordProcessor.LoadDocumentAsync("Document.docx", source.Token);
                // Asynchronously convert the DOCX file to PDF.
                await wordProcessor.ExportToPdfAsync("result.pdf", source.Token);
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("Cancelled by timeout.");
            }
        }
    }
}