PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
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):
// 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);| Parameter | Description |
|---|---|
file_path | Path of the input Word document |
output_path | Path of the output PDF |
options | Conversion options; Word to PDF uses only word_pdf_bookmark_mode and page_ranges, see below |
callback | Optional; used to get conversion progress and cancel the conversion, see 3.4 Get Conversion Progress and 3.5 Cancel Conversion Task |
diagnostics | Optional caller-allocated buffer that receives diagnostic information; pass nullptr if you do not need it |
Word to PDF uses only the following members of ConvertOptions:
| Field | Type | Default | Description |
|---|---|---|---|
word_pdf_bookmark_mode | WordPdfBookmarkMode | e_None | How PDF bookmarks (the outline shown in a reader's navigation panel) are generated |
page_ranges | char[256] | empty | Pages to output, e.g. "1-3,5,7-9"; empty outputs every page |
Values of WordPdfBookmarkMode:
| Value | Description |
|---|---|
e_None | Do not generate bookmarks |
e_Headings | One bookmark per heading (a paragraph with an outline level), nested by level |
e_WordBookmarks | One bookmark per Word bookmark, named after it |
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.)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.ruby:unsupported ruby layout. The same feature is reported only once per conversion.e_ErrOutOfMemory is returned; the text is never truncated.#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.
}