officefileapi-116817-word-processing-document-api-examples-document-conversion-how-to-convert-a-docx-document-to-pdf-format.md
Call the RichEditDocumentServer.LoadDocument to load a document from a file or a stream. Refer to the following topic for a code sample:
Read Tutorial: How to: Load a Document
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:
using DevExpress.XtraRichEdit;
using DevExpress.XtraPrinting;
using System.IO;
//...
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
// Load a DOCX document.
wordProcessor.LoadDocument("Documents\\MovieRentals.docx");
// 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);
}
}
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraPrinting
Imports System.IO
'...
Using wordProcessor As New RichEditDocumentServer()
' Load a DOCX document.
wordProcessor.LoadDocument("Documents\MovieRentals.docx")
' Specify export options.
Dim options As New PdfExportOptions()
options.DocumentOptions.Author = "Mark Jones"
options.Compressed = False
options.ImageQuality = PdfJpegImageQuality.Highest
' Export the document to a stream.
Using pdfFileStream As New FileStream("Document_PDF.pdf", FileMode.Create)
wordProcessor.ExportToPdf(pdfFileStream, options)
End Using
End Using
Export a document as an accessible PDF You can save a document as a tagged (accessible) PDF document. Accessible PDF formats allow users 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:
Run Demo: Save Word Documents as Accessible PDF Files
For more information, review the following help topic: How to: Export Document as Accessible PDF.
Important
The RichEditDocumentServerExtensions class is defined in the DevExpress.Docs.v25.2.dll assembly and DevExpress.Document.Processor NuGet package. Add this assembly or package to your project to use the RichEditDocumentServerExtensions members. You need a 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.");
}
}
}
}
Imports DevExpress.XtraRichEdit
Imports System
Imports System.Threading
Imports System.Threading.Tasks
' ...
Private Async Sub ConvertDocx2PdfWithCancellation()
' Specify the cancellation token.
Using source As New CancellationTokenSource(10000)
Using wordProcessor As 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 e1 As OperationCanceledException
Console.WriteLine("Cancelled by timeout.")
End Try
End Using
End Using
End Sub