Converting Word Documents to PDFs

Converting Word documents to PDF is straightforward using the DynamicPDF Converter for .NET.

WordConverter Class

Word conversion is accomplished using the WordConverter class. This class converts Word documents to PDF documents.

WordConverter wordDocConverter = new WordConverter(@"sample.doc");
wordDocConverter.Convert(outputPdf);
Dim wordDocConverter As New WordConverter("sample.doc")
wordDocConverter.Convert(outputPdf)

WordConversionOptions

The WordConversionOptions class can also take conversion options to specify how to format the created PDF. Conversion option properties include formatting the created PDF's dimensions, margins, and metadata. It also has options to create bookmarks from a Word document's headings and bookmarked words.

The WordConversionOptions class also has an AppendToPdf property that allows specifying if the created PDF should be appended to a pre-existing PDF.

WordConversionOptions options = new WordConversionOptions(false);
options.Author = "John Doe";
options.TopMargin = 144;
options.BottomMargin = 72;
options.LeftMargin = 72;
options.RightMargin = 72;
WordConverter wordDocConverter = new WordConverter(@"sample.doc", options);
wordDocConverter.Convert(outputPdf);
Dim options As New WordConversionOptions(False)
options.Author = "John Doe"
options.TopMargin = 144
options.BottomMargin = 72
options.LeftMargin = 72
options.RightMargin = 72

Dim wordDocConverter As New WordConverter("sample.doc", options)
wordDocConverter.Convert(outputPdf)

Bookmarks

The WordConversionOptions class also has a BookmarksType property that takes a WordBookmarkType. The WordBookmarkType values are WordBookmarksType.Headings, WordBookmarksType.None, and WordBookmarksType.Words.

public static void ConvertBookmarks(string outputPdf)
{
    WordConversionOptions conversionOptions = new(false);
    conversionOptions.BookmarksType = WordBookmarksType.Headings;
    WordConverter converter = new(@"sample.doc", conversionOptions);
    converter.Convert(Program.GetOutputDocPath(outputPdf));
}
Dim conversionOptions As New WordConversionOptions(False)
conversionOptions.BookmarksType = WordBookmarksType.Headings
Dim converter As New WordConverter("sample.doc", conversionOptions)
converter.Convert(outputPdf)

Figure 1. Preserving a Word document's headings as bookmarks.

In this topic