Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Export Documents to PDF

    • 10 minutes to read

    The Word Processing Document API allows you to export a document to PDF. Call one of the following WordDocument methods:

    Pass a PdfExportOptions object to an ExportToPdf method to configure the exported PDF document.

    Export a Document to a PDF File

    The following code snippet loads a DOCX document and exports it to a PDF file:

    using System.IO;
    using DevExpress.Docs.Word;
    
    using WordDocument document = new WordDocument(File.OpenRead("Document.docx"));
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document.pdf"));
    document.ExportToPdf(stream);
    

    Export a Document to a Byte Array

    The following code snippet exports a DOCX document to a byte array and writes the array to a PDF file:

    using System.IO;
    using DevExpress.Docs.Word;
    
    using WordDocument document = new WordDocument(File.OpenRead("Document.docx"));
    byte[] pdf = document.ExportToPdf();
    
    Directory.CreateDirectory("output");
    File.WriteAllBytes(Path.Combine("output", "Document.pdf"), pdf);
    

    Configure PDF Export Options

    Use the following PdfExportOptions properties to configure the exported PDF document:

    Document Content

    Property Description
    PageRange Gets or sets document pages to export.
    ExportComments Gets or sets whether to export document comments to PDF pages.
    ExportBookmarks Gets or sets whether to create PDF bookmarks for document bookmarks.
    ExportInteractiveControls Gets or sets whether to export content controls as interactive PDF form fields.
    ExportPageBackground Gets or sets whether to export the page background color.
    UpdateFieldsBeforeExport Gets or sets whether to update document fields before export.

    Images and Fonts

    Property Description
    ConvertImagesToJpeg Gets or sets whether to convert bitmap images to JPEG.
    ImageQuality Gets or sets the quality of JPEG images in the exported PDF document.
    RasterizeImages Gets or sets whether to rasterize vector images during export.
    RasterizationResolution Gets or sets the resolution for images that the export rasterizes.
    NonEmbeddedFonts Gets or sets names of fonts that the export does not embed in the PDF document.

    PDF Document Settings

    Property Description
    DocumentOptions Gets PDF document properties (for example, the title and author).
    MetadataExportMode Gets or sets how the export combines PDF document options with source document properties.
    Attachments Gets the collection of files to attach to the exported PDF document.
    ShowPrintDialogOnOpen Gets or sets whether a PDF viewer opens the Print dialog when a user opens the exported document.

    Security and Compliance

    Property Description
    EncryptionOptions Gets or sets encryption options for the exported PDF document.
    SignatureOptions Gets digital signature options for the exported PDF document.
    PdfACompatibility Gets or sets the PDF/A conformance level of the exported document.
    PdfUACompatibility Gets or sets the PDF/UA conformance level of the exported document.

    The following code snippet exports the first page of a DOCX document to PDF without comments and converts document images to JPEG with medium quality:

    using System.IO;
    using DevExpress.Docs.Pdf;
    using DevExpress.Docs.Word;
    using DevExpress.Docs.Word.Export;
    
    using WordDocument document = new WordDocument(File.OpenRead("Document.docx"));
    
    PdfExportOptions options = new PdfExportOptions() {
        PageRange = "1",
        ExportComments = false,
        ConvertImagesToJpeg = true,
        ImageQuality = ImageQuality.Medium
    };
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document.pdf"));
    document.ExportToPdf(stream, options);
    

    Export Specific Pages

    Set the PdfExportOptions.PageRange property to page numbers and page ranges separated by commas (for example, 1,3,5-7). Page numbers start with 1. If the PageRange property is set to an empty string, the API exports all pages.

    The following code snippet exports the first, third, and fourth pages of a document:

    PdfExportOptions options = new PdfExportOptions() {
        PageRange = "1,3-4"
    };
    document.ExportToPdf(stream, options);
    

    If the document does not contain any pages (at least one page) that the PageRange property specifies, the ExportToPdf method throws a System.Exception with the “Cannot export the document to PDF.” message.

    Specify which Document Elements to Export

    The API exports document content as it appears on document pages. The following PdfExportOptions properties specify how the API exports comments, bookmarks, form elements, and the page background:

    Comments
    If the ExportComments property is true, the exported PDF pages display document comments. Set the ExportComments property to false to exclude comments from the PDF document.
    Bookmarks
    If the ExportBookmarks property is true, the API creates PDF bookmarks for document bookmarks that hyperlinks reference (for example, table of contents entries). A PDF viewer displays PDF bookmarks in the navigation pane.
    Interactive Form Elements
    The API exports content control content as static page content. Set the ExportInteractiveControls property to true to export content controls (for example, check boxes, drop-down lists, and plain text controls) as interactive PDF form fields that users can fill in.
    Page Background
    Set the ExportPageBackground property to true to export the page background color of the source document.

    Update Fields Before Export

    The API exports field results stored in the document. If the document contains fields with outdated results (for example, PAGE, DATE, or DOCPROPERTY fields), set the UpdateFieldsBeforeExport property to true. The ExportToPdf method updates all document fields before it exports the document. Updated field results remain in the source document after export.

    The following code snippet updates document fields and exports the document to PDF:

    using System.IO;
    using DevExpress.Docs.Word;
    using DevExpress.Docs.Word.Export;
    
    using WordDocument document = new WordDocument(File.OpenRead("Document.docx"));
    
    PdfExportOptions options = new PdfExportOptions() {
        UpdateFieldsBeforeExport = true
    };
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document.pdf"));
    document.ExportToPdf(stream, options);
    

    Specify PDF Metadata

    The exported PDF document can get the author, title, subject, keywords, and application name from the following sources:

    Use the MetadataExportMode property to specify the source for these values:

    Mode Description
    Merge The exported document gets the author, title, subject, and keywords from PDF document options. If a PDF document option is not specified, the exported document gets the value from source document properties.
    PdfDocumentOptions The exported document gets the author, title, subject, and keywords from PDF document options.
    SourceDocumentProperties The exported document gets the author, title, subject, and keywords from source document properties.

    The following code snippet specifies the title and author of the exported PDF document. The PDF document gets the subject, keywords, and application name from document properties:

    using System.IO;
    using DevExpress.Docs.Pdf;
    using DevExpress.Docs.Word;
    using DevExpress.Docs.Word.Export;
    
    using WordDocument document = new WordDocument(File.OpenRead("Document.docx"));
    
    PdfExportOptions options = new PdfExportOptions() {
        MetadataExportMode = MetadataExportMode.Merge
    };
    options.DocumentOptions.Title = "Quarterly Status Report";
    options.DocumentOptions.Author = "Project Office";
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document.pdf"));
    document.ExportToPdf(stream, options);
    

    Protect and Sign the PDF Document

    To protect the PDF document with passwords and restrict user actions, assign an EncryptionOptions object to the EncryptionOptions property. The EncryptionOptions(ownerPassword, userPassword) constructor specifies passwords.

    The following code snippet exports a DOCX document to a password-protected PDF file. The user password opens the PDF document. The owner password removes print, copy, and edit restrictions:

    using System.IO;
    using DevExpress.Docs.Pdf;
    using DevExpress.Docs.Word;
    using DevExpress.Docs.Word.Export;
    
    using WordDocument document = new WordDocument(File.OpenRead("Document.docx"));
    
    PdfExportOptions options = new PdfExportOptions() {
        EncryptionOptions = new EncryptionOptions("OwnerPassword", "UserPassword") {
            Algorithm = EncryptionAlgorithm.AES256,
            PrintPermissions = DocumentPrintPermissions.NotAllowed,
            DataExtractionPermissions = DocumentDataExtractionPermissions.NotAllowed,
            ModificationPermissions = DocumentModificationPermissions.NotAllowed,
            InteractivityPermissions = DocumentInteractivityPermissions.NotAllowed
        }
    };
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document.pdf"));
    document.ExportToPdf(stream, options);
    

    To sign the PDF document, assign an X.509 certificate to the SignatureOptions.Certificate property of the object that the SignatureOptions property gets.

    Export to Accessible and Archival PDF

    PDF/UA

    PDF/UA (Universal Accessibility) is an ISO standard for accessible PDF documents. PDF/UA documents allow users with disabilities to access document content with screen readers and other assistive technologies. Set the PdfUACompatibility property to PdfUA1 or PdfUA2 to export a document to a tagged PDF/UA document. PDF/UA-2 documents use PDF 2.0.

    The following code snippet exports a DOCX document to a PDF/UA-1 document:

    using System.IO;
    using DevExpress.Docs.Pdf;
    using DevExpress.Docs.Word;
    using DevExpress.Docs.Word.Export;
    
    using WordDocument document = new WordDocument(File.OpenRead("Document.docx"));
    
    PdfExportOptions options = new PdfExportOptions() {
        PdfUACompatibility = PdfUACompatibility.PdfUA1
    };
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document_PdfUA.pdf"));
    document.ExportToPdf(stream, options);
    

    PDF/A

    PDF/A is an ISO standard for long-term document archiving. Set the PdfACompatibility property to the required PDF/A conformance level to export a document to a PDF/A document.

    The following code snippet exports a DOCX document to a PDF/A-3a document:

    using System.IO;
    using DevExpress.Docs.Pdf;
    using DevExpress.Docs.Word;
    using DevExpress.Docs.Word.Export;
    
    using WordDocument document = new WordDocument(File.OpenRead("Document.docx"));
    
    PdfExportOptions options = new PdfExportOptions() {
        PdfACompatibility = PdfACompatibility.PdfA3a
    };
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document_PdfA.pdf"));
    document.ExportToPdf(stream, options);
    

    Note

    PDF/A and PDF/UA documents must embed all fonts. Do not specify the NonEmbeddedFonts property for these documents. PDF/A documents do not support encryption and JavaScript (the ShowPrintDialogOnOpen property adds JavaScript to the PDF document). PDF/A-1 and PDF/A-2 documents do not support attachments.

    Handle Export Errors

    If the API cannot export the document, ExportToPdf methods throw a System.Exception with the “Cannot export the document to PDF.” message. The InnerException property contains the original exception. For example, the API throws this exception in the following instances:

    • The PageRange property specifies a page that the document does not contain.
    • The Attachments collection contains files and the PdfACompatibility property specifies a PDF/A-1 or PDF/A-2 level.
    • The EncryptionOptions object contains an unsupported combination of permissions.

    If you pass null as a stream or an options object, the API throws an ArgumentNullException.

    Next Steps