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 [CPDFConversion startWordToPDF:outputPath:options:] and shares WordOptions with PDF to Word; only wordPdfBookmarkMode and pageRanges take effect for Word to PDF, and the other properties are ignored. Word to PDF does not depend on any AI model.

CPDFConversion provides the following startWordToPDF methods:

objective-c
// Default options.
+ (ErrorCode)startWordToPDF:(NSString *)filePath outputPath:(NSString *)outputPath;

// Bookmark and page range options.
+ (ErrorCode)startWordToPDF:(NSString *)filePath outputPath:(NSString *)outputPath
                    options:(WordOptions *)options;

// Progress and cancellation through blocks, or through a ConvertCallback object.
+ (ErrorCode)startWordToPDF:(NSString *)filePath outputPath:(NSString *)outputPath
                    options:(WordOptions *)options
                   progress:(COMProgressBlock)progress cancel:(COMCancelBlock)cancel;
+ (ErrorCode)startWordToPDF:(NSString *)filePath outputPath:(NSString *)outputPath
                    options:(WordOptions *)options callback:(id<ConvertCallback>)callback;

// Also returns the diagnostic information.
+ (ErrorCode)startWordToPDF:(NSString *)filePath outputPath:(NSString *)outputPath
                    options:(WordOptions *)options
                   progress:(COMProgressBlock)progress cancel:(COMCancelBlock)cancel
                diagnostics:(NSString **)diagnostics;
ParameterMeaning
filePathPath of the input Word document
outputPathPath of the output PDF
optionsConversion options; Word to PDF uses only their wordPdfBookmarkMode and pageRanges, see below
progress / cancel / callbackOptional, used to get the conversion progress and cancel the conversion
diagnosticsOptional, receives the diagnostic information; pass nil if not needed

Conversion Options ​

Word to PDF uses only the following properties of WordOptions:

FieldDefaultMeaning
wordPdfBookmarkModeWordPdfBookmarkModeNoneHow 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
WordPdfBookmarkModeNoneNo bookmarks
WordPdfBookmarkModeHeadingsOne bookmark per heading (a paragraph with an outline level), nested by level
WordPdfBookmarkModeWordBookmarksOne 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, ErrorCodeUnsupportedFeature (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.

Pass the address of an NSString * as diagnostics to receive the diagnostic text, or nil if you do not need it. The SDK manages the 8192-byte buffer itself.

  • The first line of the diagnostic text is output=produced when the PDF was produced, or output=not_produced when no complete PDF was produced.
  • Each following line is one "feature:reason" entry. The same feature is reported only once per conversion.
  • If the buffer cannot hold the complete diagnostics, ErrorCodeOutOfMemory is returned; the text is never truncated.

Sample ​

objective-c
// Default options.
ErrorCode code = [CPDFConversion startWordToPDF:@"input.docx" outputPath:@"path/output.pdf"];

// Generate bookmarks from headings, output pages 1-3, 5 and 7-9, and get diagnostics.
WordOptions *options = [[WordOptions alloc] init];
options.wordPdfBookmarkMode = WordPdfBookmarkModeHeadings;
options.pageRanges = @"1-3,5,7-9";

NSString *diagnostics = nil;
code = [CPDFConversion startWordToPDF:@"input.docx"
                           outputPath:@"path/output.pdf"
                              options:options
                             progress:nil
                               cancel:nil
                          diagnostics:&diagnostics];
if (code == ErrorCodeUnsupportedFeature) {
    // 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.
    NSLog(@"%@", diagnostics);
} else if (code != ErrorCodeSuccess) {
    // The conversion failed.
}