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 through CPDF_StartWordToPDF. You can choose how PDF bookmarks are generated, select the pages to output, and obtain diagnostic information about features that may affect the fidelity of the result.

c
CSDKErrorCode CPDF_StartWordToPDF(
    COMPDFKIT_STRING file_path,
    COMPDFKIT_STRING output_path,
    CConvertOption options,
    CConvertCallback* callback,
    char* diagnostics);
ParameterMeaning
file_pathPath of the input Word document.
output_pathPath of the output PDF file.
optionsConversion options. Start from CPDF_CONVERT_OPTION_DEFAULT; Word to PDF uses only word_pdf_bookmark_mode and page_ranges, and the other fields are ignored.
callbackOptional. Used to obtain the progress and to cancel the conversion. Pass NULL if not needed.
diagnosticsOptional. A buffer allocated by the caller, with a size of CPDF_WORD_TO_PDF_DIAGNOSTICS_SIZE (8192 bytes). The SDK writes UTF-8 diagnostic text into it, and neither allocates nor frees it. Pass NULL if diagnostics are not needed.

Word to PDF Options ​

Word to PDF uses only the following fields of CConvertOption:

FieldTypeDefaultMeaning
word_pdf_bookmark_modeCWordPdfBookmarkModee_CWordPdfBookmarkNoneHow the PDF bookmarks (the outline displayed in the navigation panel of a PDF reader) are generated.
page_rangeschar[256]EmptyThe pages to output, for example "1-3,5,7-9". Empty outputs every page.

CWordPdfBookmarkMode values:

ValueMeaning
e_CWordPdfBookmarkNoneDo not generate bookmarks.
e_CWordPdfBookmarkHeadingsOne bookmark per heading (a paragraph with an outline level), nested by level.
e_CWordPdfBookmarkWordBookmarksOne bookmark per Word bookmark, named after the Word bookmark.

Diagnostics ​

  • When the document contains a feature that may affect the fidelity of the result, the interface returns e_CErrUnsupportedFeature (95). In this case a PDF is usually still produced. The first line of the diagnostic text is output=produced when the PDF has been produced, and output=not_produced when no complete PDF has been produced.
  • Each following line is one feature ID:reason entry, for example ruby:unsupported ruby layout. The same feature is reported only once in one conversion.
  • If the conversion itself fails, the real error code is returned and takes precedence over 95.
  • If the buffer cannot hold the complete diagnostic text, e_CErrOutOfMemory is returned and the text is not truncated.
  • If diagnostics is NULL, error 95 is still returned when such a feature exists, but no text is provided.
  • If the license does not include the Word conversion permission, e_CErrLicensePermissionDeny is returned.

Sample ​

Convert a Word document to PDF with the default options:

c
CConvertOption options = CPDF_CONVERT_OPTION_DEFAULT;

CSDKErrorCode code = CPDF_StartWordToPDF(
    CPDF_TEXT("input.docx"),
    CPDF_TEXT("path/output.pdf"),
    options,
    NULL,
    NULL);

Generate bookmarks from headings and output only part of the pages:

c
CConvertOption options = CPDF_CONVERT_OPTION_DEFAULT;
options.word_pdf_bookmark_mode = e_CWordPdfBookmarkHeadings;
strcpy(options.page_ranges, "1-3,5,7-9");

CSDKErrorCode code = CPDF_StartWordToPDF(
    CPDF_TEXT("input.docx"),
    CPDF_TEXT("path/output.pdf"),
    options,
    NULL,
    NULL);

Get the diagnostics and the progress:

c
#include <stdio.h>

static void Progress(int current_page_count, int total_page_count)
{
    printf("progress: %d / %d\n", current_page_count, total_page_count);
}

char diagnostics[CPDF_WORD_TO_PDF_DIAGNOSTICS_SIZE] = {0};

CConvertOption options = CPDF_CONVERT_OPTION_DEFAULT;
options.word_pdf_bookmark_mode = e_CWordPdfBookmarkWordBookmarks;

CConvertCallback callback = {0};
callback.progress = Progress;

CSDKErrorCode code = CPDF_StartWordToPDF(
    CPDF_TEXT("input.docx"),
    CPDF_TEXT("path/output.pdf"),
    options,
    &callback,
    diagnostics);

if (code == e_CErrUnsupportedFeature) {
    // The PDF is usually still produced. Check the first line of the text:
    // "output=produced" or "output=not_produced".
    printf("%s", diagnostics);
} else if (code != e_CErrSuccess) {
    return code;
}