Skip to content
DemoFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

Convert Word to PDF ​

Overview ​

ComPDF Conversion SDK supports converting Word documents to PDF files. You can choose to generate PDF bookmarks, or to output only specific pages.

Basic Usage ​

c#
string inputFilePath = "***";
string outputFileName = "***";

ErrorCode error = CPDFConversion.StartWordToPDF(inputFilePath, outputFileName);

Set Bookmarks and Page Range ​

Word to PDF reuses WordOptions; only WordPdfBookmarkMode (how bookmarks are generated) and PageRanges (which pages are output) take effect, and the other settings are ignored:

OptionTypeDefaultDescription
WordPdfBookmarkModeWordPdfBookmarkModee_NoneHow PDF bookmarks (the table of contents in a reader's navigation pane) are generated
PageRangesstringemptyThe pages to output, for example "1-3,5,7-9"; empty outputs all pages

Values of WordPdfBookmarkMode:

ValueDescription
e_NoneDo not generate bookmarks
e_HeadingsOne bookmark per heading (a paragraph with an outline level), nested by level
e_WordBookmarksOne bookmark per Word bookmark, named after the Word bookmark
c#
WordOptions options = new WordOptions();
options.WordPdfBookmarkMode = WordPdfBookmarkMode.e_Headings;
options.PageRanges = "1-3,5,7-9";

ErrorCode error = CPDFConversion.StartWordToPDF(inputFilePath, outputFileName, options);

Progress and Cancellation ​

As with the other conversion interfaces, use ConvertCallback to get progress or to cancel the conversion:

c#
OnProgress getProgress = (pageIndex, total) =>
{
    // Add callback function code here.
};

ConvertCallback callback = new ConvertCallback();
callback.progress = Marshal.GetFunctionPointerForDelegate(getProgress);

ErrorCode error = CPDFConversion.StartWordToPDF(inputFilePath, outputFileName, options, callback);

Diagnostics ​

When the document contains features that may affect fidelity, the conversion returns ErrorCode.e_ErrUnsupportedFeature (95). The PDF is usually still generated in this case. Use the overload with the out string diagnostics parameter to get the diagnostic text:

c#
string diagnostics;
ErrorCode error = CPDFConversion.StartWordToPDF(inputFilePath, outputFileName, options, out diagnostics);
if (error == ErrorCode.e_ErrUnsupportedFeature)
{
    // The first line is either output=produced or output=not_produced.
    Console.WriteLine(diagnostics);
}
  • The diagnostic text is UTF-8. The SDK allocates the buffer (8192 bytes) internally, so the caller does not need to prepare one.
  • The first line output=produced means a PDF was generated, and output=not_produced means no complete PDF was generated.
  • Each following line is one "feature ID:reason" entry, for example ruby:unsupported ruby layout. The same feature is reported only once per conversion.
  • If the conversion itself fails, the real error code is returned and takes precedence over 95.
  • If the buffer cannot hold the complete diagnostics, ErrorCode.e_ErrOutOfMemory is returned; the diagnostics are never truncated.