Sample Obj-C code for using Apryse SDK to programmatically convert generic PDF documents to Word, Excel, PowerPoint. Learn more about our Obj-C PDF to Office
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// documents and files to Word, Excel and PowerPoint.
12//
13// The Structured Output module is an optional PDFNet Add-on that can be used to convert PDF
14// and other documents into Word, Excel, PowerPoint and HTML format.
15//
16// The Apryse SDK Structured Output module can be downloaded from
17// https://docs.apryse.com/core/info/modules/
18//
19// Please contact us if you have any questions.
20//---------------------------------------------------------------------------------------
21
22int main (int argc, const char * argv[])
23{
24 @autoreleasepool {
25
26 NSString *inputPath = @"../../TestFiles/";
27 NSString *outputPath = @"../../TestFiles/Output/";
28
29 // The first step in every application using PDFNet is to initialize the
30 // library. The library is usually initialized only once, but calling
31 // Initialize() multiple times is also fine.
32 [PTPDFNet Initialize: 0];
33
34 int ret = 0;
35
36 //------------------------------------------------------------------------
37
38 [PTPDFNet AddResourceSearchPath:@"../../../Lib/"];
39
40 if (![PTStructuredOutputModule IsModuleAvailable]) {
41 NSLog(@"");
42 NSLog(@"Unable to run the sample: Apryse SDK Structured Output module not available.");
43 NSLog(@"---------------------------------------------------------------");
44 NSLog(@"The Structured Output module is an optional add-on, available for download");
45 NSLog(@"at https://docs.apryse.com/core/info/modules/. If you have already");
46 NSLog(@"downloaded this module, ensure that the SDK is able to find the required files");
47 NSLog(@"using the PDFNet::AddResourceSearchPath() function.");
48 NSLog(@"");
49
50 return 1;
51 }
52
53 //------------------------------------------------------------------------
54 // Word
55 //------------------------------------------------------------------------
56
57 @try {
58 // Convert PDF document to Word
59 NSLog(@"Converting PDF to Word");
60
61 NSString *inputFile = [inputPath stringByAppendingString:@"paragraphs_and_tables.pdf"];
62 NSString *outputFile = [outputPath stringByAppendingString:@"paragraphs_and_tables.docx"];
63
64 // Convert PDF to Word
65 [PTConvert ToWord:inputFile out_path:outputFile];
66
67 NSLog(@"Result saved in %@", outputFile);
68 }
69 @catch (NSException *e) {
70 NSLog(@"Exception: %@ - %@\n", e.name, e.reason);
71 ret = 1;
72 }
73
74 @try {
75 // Convert PDF document to Word with options
76 NSLog(@"Converting PDF to Word with options");
77
78 NSString *inputFile = [inputPath stringByAppendingString:@"paragraphs_and_tables.pdf"];
79 NSString *outputFile = [outputPath stringByAppendingString:@"paragraphs_and_tables_first_page.docx"];
80
81 PTWordOutputOptions *wordOutputOptions = [[PTWordOutputOptions alloc] init];
82
83 // Convert only the first page
84 [wordOutputOptions SetPages:1 page_to:1];
85
86 // Convert PDF to Word
87 [PTConvert ToWordWithFilename:inputFile out_path:outputFile options:wordOutputOptions];
88
89 NSLog(@"Result saved in %@", outputFile);
90 }
91 @catch (NSException *e) {
92 NSLog(@"Exception: %@ - %@\n", e.name, e.reason);
93 ret = 1;
94 }
95
96 //------------------------------------------------------------------------
97 // Excel
98 //------------------------------------------------------------------------
99
100 @try {
101 // Convert PDF document to Excel
102 NSLog(@"Converting PDF to Excel");
103
104 NSString *inputFile = [inputPath stringByAppendingString:@"paragraphs_and_tables.pdf"];
105 NSString *outputFile = [outputPath stringByAppendingString:@"paragraphs_and_tables.xlsx"];
106
107 // Convert PDF to Excel
108 [PTConvert ToExcel:inputFile out_path:outputFile];
109
110 NSLog(@"Result saved in %@", outputFile);
111 }
112 @catch (NSException *e) {
113 NSLog(@"Exception: %@ - %@\n", e.name, e.reason);
114 ret = 1;
115 }
116
117 @try {
118 // Convert PDF document to Excel with options
119 NSLog(@"Converting PDF to Excel with options");
120
121 NSString *inputFile = [inputPath stringByAppendingString:@"paragraphs_and_tables.pdf"];
122 NSString *outputFile = [outputPath stringByAppendingString:@"paragraphs_and_tables_second_page.xlsx"];
123
124 PTExcelOutputOptions *excelOutputOptions = [[PTExcelOutputOptions alloc] init];
125
126 // Convert only the second page
127 [excelOutputOptions SetPages:2 page_to:2];
128
129 // Convert PDF to Excel
130 [PTConvert ToExcelWithFilename:inputFile out_path:outputFile options:excelOutputOptions];
131
132 NSLog(@"Result saved in %@", outputFile);
133 }
134 @catch (NSException *e) {
135 NSLog(@"Exception: %@ - %@\n", e.name, e.reason);
136 ret = 1;
137 }
138
139 //------------------------------------------------------------------------
140 // PowerPoint
141 //------------------------------------------------------------------------
142
143 @try {
144 // Convert PDF document to PowerPoint
145 NSLog(@"Converting PDF to PowerPoint");
146
147 NSString *inputFile = [inputPath stringByAppendingString:@"paragraphs_and_tables.pdf"];
148 NSString *outputFile = [outputPath stringByAppendingString:@"paragraphs_and_tables.pptx"];
149
150 // Convert PDF to PowerPoint
151 [PTConvert ToPowerPoint:inputFile out_path:outputFile];
152
153 NSLog(@"Result saved in %@", outputFile);
154 }
155 @catch (NSException *e) {
156 NSLog(@"Exception: %@ - %@\n", e.name, e.reason);
157 ret = 1;
158 }
159
160 @try {
161 // Convert PDF document to PowerPoint with options
162 NSLog(@"Converting PDF to PowerPoint with options");
163
164 NSString *inputFile = [inputPath stringByAppendingString:@"paragraphs_and_tables.pdf"];
165 NSString *outputFile = [outputPath stringByAppendingString:@"paragraphs_and_tables_first_page.pptx"];
166
167 PTPowerPointOutputOptions *powerPointOutputOptions = [[PTPowerPointOutputOptions alloc] init];
168
169 // Convert only the first page
170 [powerPointOutputOptions SetPages:1 page_to:1];
171
172 // Convert PDF to PowerPoint
173 [PTConvert ToPowerPointWithFilename:inputFile out_path:outputFile options:powerPointOutputOptions];
174
175 NSLog(@"Result saved in %@", outputFile);
176 }
177 @catch (NSException *e) {
178 NSLog(@"Exception: %@ - %@\n", e.name, e.reason);
179 ret = 1;
180 }
181
182 //------------------------------------------------------------------------
183
184 [PTPDFNet Terminate: 0];
185 return ret;
186 }
187}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales