OfficeToPDF

Sample Obj-C code for using Apryse SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses. Office to PDF conversion can be performed on a Linux or Windows server to automate Office-centric workflows, or entirely in the user's client (web browser, mobile device). The conversion functionality can be combined with our Viewer to display or annotate Office files (docx, xlsx, pptx) on all major platforms, including Web, Android, iOS, Xamarin, UWP, and Windows. Learn more about our iOS SDK and Office Document Conversion Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#import <OBJC/PDFNetOBJC.h>
7#import <Foundation/Foundation.h>
8
9//---------------------------------------------------------------------------------------
10// The following sample illustrates how to use the PDF::Convert utility class to convert
11// MS office files to PDF
12//
13// This conversion is performed entirely within the PDFNet and has *no* external or
14// system dependencies dependencies -- Conversion results will be the same whether
15// on Windows, Linux or Android.
16//
17// Please contact us if you have any questions.
18//---------------------------------------------------------------------------------------
19
20
21
22
23@interface OfficeToPDFTest : NSObject {}
24+ (void)SimpleConvertWithInputFilename:(NSString*)input_filename
25 outputFilename:(NSString*)output_filename;
26+ (bool)FlexibleConvertWithInputFilename:(NSString*)input_filename
27 outputFilename:(NSString*)output_filename;
28@end
29
30@implementation OfficeToPDFTest : NSObject {}
31
32+ (void)SimpleConvertWithInputFilename:(NSString*)input_filename
33 outputFilename:(NSString*)output_filename
34{
35 NSString *input_path = @"../../TestFiles/";
36 NSString *output_path = @"../../TestFiles/Output/";
37
38 // Start with a PDFDoc (the conversion destination)
39 PTPDFDoc* pdfDoc = [[PTPDFDoc alloc] init];
40
41 // perform the conversion with no optional parameters
42 [PTConvert OfficeToPDF:pdfDoc
43 in_filename:[NSString stringWithFormat:@"%@/%@", input_path, input_filename]
44 options:Nil];
45
46 [pdfDoc SaveToFile: [NSString stringWithFormat:@"%@/%@", output_path, output_filename]
47 flags: e_ptremove_unused];
48
49 NSLog(@"Saved: %@/%@\n", output_path, output_filename);
50}
51
52+ (bool)FlexibleConvertWithInputFilename:(NSString*)input_filename
53 outputFilename:(NSString*)output_filename
54{
55 NSString *input_path = @"../../TestFiles/";
56 NSString *output_path = @"../../TestFiles/Output/";
57
58 // Start with a PDFDoc (the conversion destination)
59 PTPDFDoc* pdfDoc = [[PTPDFDoc alloc] init];
60
61 PTOfficeToPDFOptions* options = [[PTOfficeToPDFOptions alloc] init];
62 [options SetSmartSubstitutionPluginPath:input_path];
63
64 // create a conversion object with optional parameters
65 PTDocumentConversion* conversion = [PTConvert StreamingPDFConversionWithDoc: pdfDoc
66 in_filename:[NSString stringWithFormat:@"%@/%@", input_path, input_filename]
67 options:options];
68 NSLog(@"%@: %.0f%% %@\n", input_filename, [conversion GetProgress]*100.0, [conversion GetProgressLabel]);
69
70 // convert each page, and report progress
71 while([conversion GetConversionStatus] == e_ptIncomplete)
72 {
73 [conversion ConvertNextPage];
74 NSLog(@"%@: %.0f%% %@\n", input_filename, [conversion GetProgress]*100.0, [conversion GetProgressLabel]);
75 }
76
77 if ([conversion TryConvert] == e_ptSuccess)
78 {
79 // print out any extra information about the conversion
80 int num_warnings = [conversion GetNumWarnings];
81 for (int i = 0; i < num_warnings; ++i)
82 {
83 NSLog(@"Warning: %@\n", [conversion GetWarningString: i]);
84 }
85
86 //save the result
87 [pdfDoc SaveToFile: [NSString stringWithFormat:@"%@/%@", output_path, output_filename]
88 flags: e_ptremove_unused];
89
90 NSLog(@"Saved: %@/%@\n", output_path, output_filename);
91
92 return true;
93 }
94 else
95 {
96 NSLog(@"Encountered an error during conversion: %@\n", [conversion GetErrorString]);
97 }
98
99 return false;
100}
101
102@end
103
104int main(int argc, char *argv[])
105{
106
107 @autoreleasepool {
108
109 [PTPDFNet Initialize: 0];
110
111 // convert using the simple one-line interface
112 [OfficeToPDFTest SimpleConvertWithInputFilename:@"Fishermen.docx"
113 outputFilename:@"Fishermen.pdf"];
114
115 // convert using the more flexible page-by-page interface
116 [OfficeToPDFTest FlexibleConvertWithInputFilename:@"the_rime_of_the_ancient_mariner.docx"
117 outputFilename:@"the_rime_of_the_ancient_mariner.pdf"];
118
119 // conversion of RTL content
120 [OfficeToPDFTest FlexibleConvertWithInputFilename:@"factsheet_Arabic.docx"
121 outputFilename:@"factsheet_Arabic.pdf"];
122 [PTPDFNet Terminate: 0];
123 return 0;
124 }
125}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales