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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import PDFNet
7import Foundation
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
20class OfficeToPDFTest: NSObject {
21 class func simpleConvert(inputPath: String, outputPath: String) {
22 // Start with a PDFDoc (the conversion destination)
23 let pdfDoc: PTPDFDoc = PTPDFDoc()
24
25 // perform the conversion with no optional parameters
26 PTConvert.office(toPDF: pdfDoc, in_filename: inputPath, options: nil)
27
28 pdfDoc.save(toFile: outputPath, flags: e_ptremove_unused.rawValue)
29
30 print("Saved: \(outputPath)")
31 }
32
33 class func flexibleConvert(inputPath: String, outputPath: String, pluginPath: String) -> Bool {
34 // Start with a PDFDoc (the conversion destination)
35 let pdfDoc: PTPDFDoc = PTPDFDoc()
36
37 let options: PTOfficeToPDFOptions = PTOfficeToPDFOptions()
38 options.setSmartSubstitutionPluginPath(pluginPath)
39
40 // create a conversion object with optional parameters
41 let conversion: PTDocumentConversion = PTConvert.streamingPDFConversion(with: pdfDoc, in_filename: inputPath, options: options)
42 print(String(format: "\(inputPath): %.0f%% \(conversion.getProgressLabel()!)", conversion.getProgress() * 100.0))
43
44 // convert each page, and report progress
45 while conversion.getStatus() == e_ptIncomplete {
46 conversion.convertNextPage()
47 print(String(format: "\(inputPath): %.0f%% \(conversion.getProgressLabel()!)", conversion.getProgress() * 100.0))
48 }
49
50 if conversion.tryConvert() == e_ptSuccess {
51 // print out any extra information about the conversion
52 let num_warnings: UInt32 = conversion.getNumWarnings()
53 for i in 0..<num_warnings {
54 print("Warning: \(conversion.getWarningString(i)!)")
55 }
56
57 //save the result
58 pdfDoc.save(toFile: "\(outputPath)", flags: e_ptremove_unused.rawValue)
59
60 print("Saved: \(outputPath)")
61
62 return true
63 }
64 else {
65 print("Encountered an error during conversion: \(conversion.getErrorString()!)")
66 }
67
68 return false
69 }
70}
71
72func runOfficeToPDFTest() -> Int {
73 return autoreleasepool {
74 var ret = 0
75
76
77 do {
78 try PTPDFNet.catchException {
79 do {
80 let inputPath: String! = Bundle.main.path(forResource: "simple-word_2007", ofType: "docx")
81 let outputPath: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("simple-word_2007_a.pdf").path
82
83 // convert using the simple one-line interface
84 OfficeToPDFTest.simpleConvert(inputPath: inputPath, outputPath: outputPath)
85 }
86
87 do {
88 let inputPath: String! = Bundle.main.path(forResource: "the_rime_of_the_ancient_mariner", ofType: "docx")
89 let outputPath: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("the_rime_of_the_ancient_mariner.pdf").path
90
91 // convert using the more flexible page-by-page interface
92 _ = OfficeToPDFTest.flexibleConvert(inputPath: inputPath, outputPath: outputPath, pluginPath: Bundle.main.resourcePath!)
93 }
94
95 do {
96 let inputPath: String! = Bundle.main.path(forResource: "wrap_poly_demo", ofType: "docx")
97 let outputPath: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("wrap_poly_demo.pdf").path
98
99 // convert a document with a complex layout
100 OfficeToPDFTest.simpleConvert(inputPath: inputPath, outputPath: outputPath)
101 }
102 }
103 } catch let e as NSError {
104 print("\(e)")
105 ret = 1
106 }
107
108 return ret
109 }
110}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales