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 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.
CSDKErrorCode CPDF_StartWordToPDF(
COMPDFKIT_STRING file_path,
COMPDFKIT_STRING output_path,
CConvertOption options,
CConvertCallback* callback,
char* diagnostics);| Parameter | Meaning |
|---|---|
file_path | Path of the input Word document. |
output_path | Path of the output PDF file. |
options | Conversion 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. |
callback | Optional. Used to obtain the progress and to cancel the conversion. Pass NULL if not needed. |
diagnostics | Optional. 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 uses only the following fields of CConvertOption:
| Field | Type | Default | Meaning |
|---|---|---|---|
word_pdf_bookmark_mode | CWordPdfBookmarkMode | e_CWordPdfBookmarkNone | How the PDF bookmarks (the outline displayed in the navigation panel of a PDF reader) are generated. |
page_ranges | char[256] | Empty | The pages to output, for example "1-3,5,7-9". Empty outputs every page. |
CWordPdfBookmarkMode values:
| Value | Meaning |
|---|---|
e_CWordPdfBookmarkNone | Do not generate bookmarks. |
e_CWordPdfBookmarkHeadings | One bookmark per heading (a paragraph with an outline level), nested by level. |
e_CWordPdfBookmarkWordBookmarks | One bookmark per Word bookmark, named after the Word bookmark. |
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.feature ID:reason entry, for example ruby:unsupported ruby layout. The same feature is reported only once in one conversion.e_CErrOutOfMemory is returned and the text is not truncated.diagnostics is NULL, error 95 is still returned when such a feature exists, but no text is provided.e_CErrLicensePermissionDeny is returned.Convert a Word document to PDF with the default options:
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:
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:
#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;
}