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 converts Word documents (.docx) to PDF files, and can optionally generate PDF bookmarks and output only selected pages. The feature is provided by ComPDFKitConverter.startWordToPdf and its options are set through WordOptions, the same class used for PDF conversion; only the bookmark mode and the page range take effect for Word to PDF, and the other options are ignored.

ComPDFKitConverter.startWordToPdf provides the following overloads, all returning ErrorCode:

OverloadDescription
startWordToPdf(filePath: String, outputPath: String, callback: ConvertCallback? = null)Default options: no bookmarks, every page; pass a callback to get the progress or cancel the conversion
startWordToPdf(filePath: String, outputPath: String, options: WordOptions, callback: ConvertCallback? = null)Bookmarks and page range set through WordOptions, with an optional callback

A pageRanges string longer than 255 bytes returns ErrorCode.INVALID_ARG instead of being truncated.

Conversion Options ​

Word to PDF uses only the following two members of WordOptions:

FieldDefaultMeaning
wordPdfBookmarkModeWordPdfBookmarkMode.NONEHow PDF bookmarks (the outline shown in a reader's navigation panel) are generated
pageRangesEmptyPages to output, e.g. "1-3,5,7-9"; empty outputs every page

Values of WordPdfBookmarkMode:

ValueMeaning
WordPdfBookmarkMode.NONENo bookmarks
WordPdfBookmarkMode.HEADINGSOne bookmark per heading (a paragraph with an outline level), nested by level
WordPdfBookmarkMode.WORD_BOOKMARKSOne bookmark per Word bookmark, with the same name as in the Word document

Return Values and Diagnostics ​

  • When the input document contains features that cannot be fully reproduced, ErrorCode.UNSUPPORTED_FEATURE (95) is returned. The PDF is usually still produced in this case, so do not assume that no output file exists just because 95 is returned.

  • If the conversion itself fails, the real error code is returned and takes precedence over 95.

  • The C / C++ interfaces also provide an optional diagnostics buffer that reports the feature identifiers and the output status; the Android interface does not provide this parameter.

Sample ​

kotlin
// Default options.
var error = ComPDFKitConverter.startWordToPdf("input.docx", "output.pdf")

// Generate bookmarks from headings and output pages 1-3, 5 and 7-9.
val wordOptions = WordOptions(
    wordPdfBookmarkMode = WordPdfBookmarkMode.HEADINGS,
    pageRanges = "1-3,5,7-9"
)
error = ComPDFKitConverter.startWordToPdf("input.docx", "output.pdf", wordOptions)
if (error == ErrorCode.UNSUPPORTED_FEATURE) {
    // The document contains features that may affect fidelity; the PDF is usually still produced.
} else if (error != ErrorCode.SUCCESS) {
    // The conversion failed.
}