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-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 Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales