Sample Obj-C code to use Apryse SDK for direct, high-quality conversion between PDF, XPS, SVG, TIFF, PNG, JPEG, and other image formats ('pdftron.PDF.Convert' namespace). The sample also shows how to convert MS Office files using our built in conversion. Learn more about our iOS SDK and PDF 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// documents and files to PDF, XPS, SVG, or EMF.
12//
13// Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
14// converted to PDF or XPS. Other formats are converted using a virtual driver. To check
15// if ToPDF (or ToXPS) require that PDFNet printer is installed use Convert::RequiresPrinter(filename).
16// The installing application must be run as administrator. The manifest for this sample
17// specifies appropriate the UAC elevation.
18//
19// Note: the PDFNet printer is a virtual XPS printer supported on Vista SP1 and Windows 7.
20// For Windows XP SP2 or higher, or Vista SP0 you need to install the XPS Essentials Pack (or
21// equivalent redistributables). You can download the XPS Essentials Pack from:
22// http://www.microsoft.com/downloads/details.aspx?FamilyId=B8DCFFDD-E3A5-44CC-8021-7649FD37FFEE&displaylang=en
23// Windows XP Sp2 will also need the Microsoft Core XML Services (MSXML) 6.0:
24// http://www.microsoft.com/downloads/details.aspx?familyid=993C0BCF-3BCF-4009-BE21-27E85E1857B1&displaylang=en
25//
26// Note: Convert.FromEmf and Convert.ToEmf will only work on Windows and require GDI+.
27//
28// Please contact us if you have any questions.
29//---------------------------------------------------------------------------------------
30
31
32NSArray *testfiles;
33
34@interface Testfile : NSObject {
35 NSString *inputFile;
36 NSString *outputFile;
37 bool requiresWindowsPlatform;
38}
39- (instancetype)init: (NSString*) input output: (NSString*) output win: (bool) win NS_DESIGNATED_INITIALIZER;
40@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSString *GetInputFile;
41@property (NS_NONATOMIC_IOSONLY, readonly, copy) NSString *GetOutputFile;
42@property (NS_NONATOMIC_IOSONLY, readonly) bool RequiresWindowsPlatform;
43@end
44
45@implementation Testfile
46
47- (Testfile*)init: (NSString*) input output: (NSString*) output win: (bool) win {
48 self = [super init];
49 inputFile = input;
50 outputFile = output;
51 requiresWindowsPlatform = win;
52 return self;
53}
54
55- (NSString*)GetInputFile {
56 return inputFile;
57}
58
59- (NSString*)GetOutputFile {
60 return outputFile;
61}
62
63- (bool)RequiresWindowsPlatform {
64 return requiresWindowsPlatform;
65}
66
67@end
68
69
70// convert to/from PDF, XPS, EMF, SVG
71int ConvertSpecificFormats() {
72
73 int ret = 0;
74
75 // Start with a PDFDoc to collect the converted documents
76 PTPDFDoc *pdfdoc = [[PTPDFDoc alloc] init];
77 NSString *outputFile;
78
79 NSString *s1 = @"../../TestFiles/simple-xps.xps";
80 NSLog(@"Converting from XPS\n");
81 [PTConvert FromXpsWithFilename: pdfdoc in_filename: s1];
82 outputFile = @"../../TestFiles/Output/xps2pdf v2.pdf";
83 [pdfdoc SaveToFile: outputFile flags: e_ptremove_unused];
84 NSLog(@"Saved %@\n", [outputFile lastPathComponent]);
85 //////////////////////////////////////////////////////////
86 // add a dictionary
87 PTObjSet *set = [[PTObjSet alloc] init];
88 PTObj* options = [set CreateDict];
89
90 // Put options
91 [options PutNumber:@"FontSize" value: 15];
92 [options PutBool:@"UseSourceCodeFormatting" value: YES];
93 [options PutNumber:@"PageWidth" value: 12];
94 [options PutNumber:@"PageHeight" value: 6];
95
96 // Convert from .txt file
97 outputFile = @"../../TestFiles/Output/simple-text.pdf";
98 NSLog(@"Converting from txt");
99 [PTConvert FromText:pdfdoc in_filename:@"../../TestFiles/simple-text.txt" in_options: options];
100 [pdfdoc SaveToFile: outputFile flags: e_ptremove_unused];
101 NSLog(@"Saved %@\n", [outputFile lastPathComponent]);
102
103 // Convert the two page PDF document to SVG
104 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath:@"../../TestFiles/newsletter.pdf"];
105 NSLog(@"Converting pdfdoc to SVG\n");
106 outputFile = @"../../TestFiles/Output/pdf2svg v2.svg";
107 [PTConvert ToSvgWithPDFDoc: doc in_filename:outputFile in_options: [[PTSVGOutputOptions alloc] init]];
108 NSLog(@"Saved %@\n", [outputFile lastPathComponent]);
109
110 // Convert the PNG image to XPS
111 NSLog(@"Converting PNG to XPS\n");
112 outputFile = @"../../TestFiles/Output/butterfly.xps";
113 [PTConvert ToXpsWithFilename: @"../../TestFiles/butterfly.png" in_outputFilename: outputFile options: [[PTXPSOutputOptions alloc] init]];
114 NSLog(@"Saved %@\n", [outputFile lastPathComponent]);
115
116 // Convert PDF document to XPS
117 NSLog(@"Converting PDF to XPS\n");
118 outputFile = @"../../TestFiles/Output/newsletter.xps";
119 [PTConvert ToXpsWithFilename: @"../../TestFiles/newsletter.pdf" in_outputFilename: outputFile options: [[PTXPSOutputOptions alloc] init]];
120 NSLog(@"Saved %@", [outputFile lastPathComponent]);
121
122 // Convert PDF document to HTML
123 NSLog(@"Converting PDF to HTML\n");
124 outputFile = @"../../TestFiles/Output/newsletter";
125 [PTConvert ToHtmlWithFilename: @"../../TestFiles/newsletter.pdf" out_path: outputFile options: [[PTHTMLOutputOptions alloc] init]];
126 NSLog(@"Saved newsletter as HTML\n");
127
128 // Convert PDF document to EPUB
129 NSLog(@"Converting PDF to EPUB\n");
130 outputFile = @"../../TestFiles/Output/newsletter.epub";
131 [PTConvert ToEpubWithFilename: @"../../TestFiles/newsletter.pdf" out_path: outputFile html_options: [[PTHTMLOutputOptions alloc] init] epub_options: [[PTEPUBOutputOptions alloc] init]];
132 NSLog(@"Saved %@", [outputFile lastPathComponent]);
133
134
135 // Convert PDF document to multipage TIFF
136 NSLog(@"Converting PDF to multipage TIFF");
137 outputFile = @"../../TestFiles/Output/newsletter.tiff";
138 PTTiffOutputOptions* tiff_options = [[PTTiffOutputOptions alloc] init];
139 [tiff_options SetDPI : 200];
140 [tiff_options SetDither: YES];
141 [tiff_options SetMono: YES];
142 [PTConvert ToTiff: @"../../TestFiles/newsletter.pdf" out_path: outputFile options:tiff_options];
143 NSLog(@"Saved %@", [outputFile lastPathComponent]);
144
145 // Convert from .svg file
146 NSLog(@"Converting SVG to PDF");
147 PTPDFDoc *svgpdfdoc = [[PTPDFDoc alloc] init];
148 outputFile = @"../../TestFiles/Output/svg2pdf.pdf";
149 [PTConvert FromSVG:svgpdfdoc in_filename:@"../../TestFiles/tiger.svg" opts: NULL];
150 [svgpdfdoc SaveToFile: outputFile flags: e_ptremove_unused];
151 NSLog(@"Saved %@", [outputFile lastPathComponent]);
152
153 return ret;
154}
155
156// convert from a file to PDF automatically
157int ConvertToPdfFromFile() {
158
159 NSString *inputPath = @"../../TestFiles/";
160 NSString *outputPath = @"../../TestFiles/Output/";
161
162 int ret = 0;
163
164 unsigned int count = testfiles.count;//sizeof (testfiles) / sizeof (Testfile);
165
166 unsigned int i = 0;
167 for (; i < count; i++)
168 {
169 Testfile *file = testfiles[i];
170
171 if ([file RequiresWindowsPlatform])
172 {
173 continue;
174 }
175
176 PTPDFDoc *pdfdoc = [[PTPDFDoc alloc] init];
177 NSString *inputFile = [inputPath stringByAppendingString: [file GetInputFile]];
178 NSString *outputFile = [outputPath stringByAppendingString: [file GetOutputFile]];
179
180 [PTConvert ToPdf: pdfdoc in_filename: inputFile];
181 [pdfdoc SaveToFile: outputFile flags: e_ptlinearized];
182 NSLog(@"Converted file: %@\n", [inputFile lastPathComponent]);
183 NSLog(@"to: %@\n", [outputFile lastPathComponent]);
184 }
185 return ret;
186}
187
188
189int main(int argc, char *argv[])
190{
191 @autoreleasepool {
192
193 testfiles = @[[[Testfile alloc] init: @"simple-word_2007.docx" output: @"docx2pdf.pdf" win: false],
194 [[Testfile alloc] init: @"simple-powerpoint_2007.pptx" output: @"pptx2pdf.pdf" win: false],
195 [[Testfile alloc] init: @"simple-excel_2007.xlsx" output: @"xlsx2pdf.pdf" win: false],
196 [[Testfile alloc] init: @"simple-publisher.pub" output: @"pub2pdf.pdf" win: true],
197 // [[Testfile alloc] init: @"simple-visio.vsd" output: @"vsd2pdf.pdf" win: true], // requires Microsoft Office Visio
198 [[Testfile alloc] init: @"simple-text.txt" output: @"txt2pdf.pdf" win: false],
199 [[Testfile alloc] init: @"simple-rtf.rtf" output: @"rtf2pdf.pdf" win: true],
200 [[Testfile alloc] init: @"butterfly.png" output: @"png2pdf.pdf" win: false],
201 [[Testfile alloc] init: @"simple-emf.emf" output: @"emf2pdf.pdf" win: true],
202 [[Testfile alloc] init: @"simple-xps.xps" output: @"xps2pdf.pdf" win: false],
203 // [[Testfile alloc] init: @"simple-webpage.mht" output: @"mht2pdf.pdf" win: true],
204 [[Testfile alloc] init: @"simple-webpage.html" output: @"html2pdf.pdf" win: true]];
205
206 // The first step in every application using PDFNet is to initialize the
207 // library. The library is usually initialized only once, but calling
208 // Initialize() multiple times is also fine.
209 int err = 0;
210
211 [PTPDFNet Initialize: 0];
212
213 // Demonstrate Convert::ToPdf and Convert::Printer
214 err = ConvertToPdfFromFile();
215 if (err)
216 {
217 NSLog(@"ConvertFile failed\n");
218 }
219 else
220 {
221 NSLog(@"ConvertFile succeeded\n");
222 }
223
224 // Demonstrate Convert::[FromEmf, FromXps, ToEmf, ToSVG, ToXPS]
225 err = ConvertSpecificFormats();
226 if (err)
227 {
228 NSLog(@"ConvertSpecificFormats failed\n");
229 }
230 else
231 {
232 NSLog(@"ConvertSpecificFormats succeeded\n");
233 }
234
235 NSLog(@"Done.\n");
236 [PTPDFNet Terminate: 0];
237 return err;
238 }
239
240}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales