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. During conversion you can choose to generate PDF bookmarks, output only specific pages, and obtain diagnostic information about features that may affect fidelity. This function is provided by CPDFConversion::StartWordToPdf. Its options are set through ConvertOptions, the same structure used for PDF conversion; only word_pdf_bookmark_mode and page_ranges take effect for Word to PDF, and the other members are ignored.

StartWordToPdf provides the following overloads (each exists in a const char* path version and a const wchar_t* path version):

c++
// Use the default options.
static ErrorCode StartWordToPdf(const char* file_path, const char* output_path,
                                CConvertCallback* callback = nullptr, char* diagnostics = nullptr);

// Specify the options; only the bookmark mode and page range take effect.
static ErrorCode StartWordToPdf(const char* file_path, const char* output_path,
                                const ConvertOptions& options,
                                CConvertCallback* callback = nullptr, char* diagnostics = nullptr);
ParameterDescription
file_pathPath of the input Word document
output_pathPath of the output PDF
optionsConversion options; Word to PDF uses only word_pdf_bookmark_mode and page_ranges, see below
callbackOptional; used to get conversion progress and cancel the conversion, see 3.4 Get Conversion Progress and 3.5 Cancel Conversion Task
diagnosticsOptional caller-allocated buffer that receives diagnostic information; pass nullptr if you do not need it

Conversion Options ​

Word to PDF uses only the following members of ConvertOptions:

FieldTypeDefaultDescription
word_pdf_bookmark_modeWordPdfBookmarkModee_NoneHow PDF bookmarks (the outline shown in a reader's navigation panel) are generated
page_rangeschar[256]emptyPages to output, e.g. "1-3,5,7-9"; empty outputs every page

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 it

Diagnostics ​

  • The diagnostics buffer must be 8192 bytes in size. The SDK writes UTF-8 diagnostic text into it and neither allocates nor frees the buffer. (The macro CPDF_WORD_TO_PDF_DIAGNOSTICS_SIZE in the C header compdf_conversion_c.h denotes this size; the C++ headers do not contain it, so define your own constant.)
  • When the document contains a feature that may affect fidelity, e_ErrUnsupportedFeature (95) is returned. The PDF is usually still produced in this case: the first line of the diagnostic text, output=produced, means the PDF was produced, and output=not_produced means no complete PDF was written.
  • 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, e_ErrOutOfMemory is returned; the text is never truncated.

Sample ​

c++
#include <cstdio>
#include <iostream>
#include "compdf_conversion.h"

using namespace compdf::base;
using namespace compdf::common;
using namespace compdf::conversion;

// Default options.
ErrorCode code = CPDFConversion::StartWordToPdf("input.docx", "path/output.pdf");

// Generate bookmarks from headings, output pages 1-3, 5, and 7-9, and get diagnostics.
const size_t kDiagnosticsSize = 8192;
char diagnostics[kDiagnosticsSize] = {0};

ConvertOptions opt;
opt.word_pdf_bookmark_mode = WordPdfBookmarkMode::e_Headings;
std::snprintf(opt.page_ranges, sizeof(opt.page_ranges), "%s", "1-3,5,7-9");

code = CPDFConversion::StartWordToPdf("input.docx", "path/output.pdf", opt, nullptr, diagnostics);
if (code == ErrorCode::e_ErrUnsupportedFeature) {
    // The document contains features that may affect fidelity. The PDF is usually still produced;
    // check output=produced / output=not_produced on the first line of the diagnostic text.
    std::cout << diagnostics << std::endl;
} else if (code != ErrorCode::e_ErrSuccess) {
    // The conversion failed.
}