PDFPackage

Sample Obj-C code to use Apryse SDK for creating, extracting, and manipulating PDF packages (also known as PDF portfolios). Learn more about our iOS SDK and PDF Data Extraction SDK Capabilities.

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/// This sample illustrates how to create, extract, and manipulate PDF Portfolios
11/// (a.k.a. PDF Packages) using PDFNet SDK.
12//-----------------------------------------------------------------------------------
13
14
15void AddPackage(PTPDFDoc *doc, NSString *file, NSString* desc)
16{
17 PTNameTree *files = [PTNameTree Create: [doc GetSDFDoc] name: @"EmbeddedFiles"];
18 PTFileSpec *fs = [PTFileSpec Create: [doc GetSDFDoc] path: file embed: true];
19 [files Put: [file dataUsingEncoding: NSUTF8StringEncoding] key_sz:(int)file.length value: [fs GetSDFObj]];
20 [fs SetDesc: desc];
21
22 PTObj * collection = [[doc GetRoot] FindObj: @"Collection"];
23 if (!collection) collection = [[doc GetRoot] PutDict: @"Collection"];
24
25 // You could here manipulate any entry in the Collection dictionary.
26 // For example, the following line sets the tile mode for initial view mode
27 // Please refer to section '2.3.5 Collections' in PDF Reference for details.
28 [collection PutName: @"View" name: @"T"];
29}
30
31void AddCoverPage(PTPDFDoc *doc)
32{
33 // Here we dynamically generate cover page (please see ElementBuilder
34 // sample for more extensive coverage of PDF creation API).
35 PTPDFRect * rect = [[PTPDFRect alloc] init];
36 [rect Set: 0 y1: 0 x2: 200 y2: 200];
37 PTPage *page = [doc PageCreate: rect];
38
39 PTElementBuilder *b = [[PTElementBuilder alloc] init];
40 PTElementWriter *w = [[PTElementWriter alloc] init];
41 [w WriterBeginWithPage: page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL];
42 PTFont *font = [PTFont Create: [doc GetSDFDoc] type: e_pthelvetica embed: NO];
43 [w WriteElement: [b CreateTextBeginWithFont: font font_sz: 12]];
44 PTElement *e = [b CreateTextRun: @"My PDF Collection"];
45 PTMatrix2D *mtx = [[PTMatrix2D alloc] initWithA: 1 b: 0 c: 0 d: 1 h: 50 v: 96];
46 [e SetTextMatrixWithMatrix2D: mtx];
47 [[e GetGState] SetFillColorSpace: [PTColorSpace CreateDeviceRGB]];
48 [[e GetGState] SetFillColorWithColorPt: [[PTColorPt alloc] initWithX: 1 y: 0 z: 0 w: 0]];
49 [w WriteElement: e];
50 [w WriteElement: [b CreateTextEnd]];
51 [w End];
52 [doc PagePushBack: page];
53
54 // Alternatively we could import a PDF page from a template PDF document
55 // (for an example please see PDFPage sample project).
56 // ...
57}
58
59int main(int argc, char *argv[])
60{
61 @autoreleasepool {
62 int ret = 0;
63 [PTPDFNet Initialize: 0];
64
65 // Create a PDF Package.
66 @try
67 {
68 PTPDFDoc *doc = [[PTPDFDoc alloc] init];
69 AddPackage(doc, @"../../TestFiles/numbered.pdf", @"My File 1");
70 AddPackage(doc, @"../../TestFiles/newsletter.pdf", @"My Newsletter...");
71 AddPackage(doc, @"../../TestFiles/peppers.jpg", @"An image");
72 AddCoverPage(doc);
73 [doc SaveToFile: @"../../TestFiles/Output/package.pdf" flags: e_ptlinearized];
74 NSLog(@"Done.");
75 }
76 @catch(NSException *e)
77 {
78 NSLog(@"%@", e.reason);
79 ret = 1;
80 }
81
82 // Extract parts from a PDF Package.
83 @try
84 {
85 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/Output/package.pdf"];
86 [doc InitSecurityHandler];
87
88 PTNameTree *files = [PTNameTree Find: [doc GetSDFDoc] name: @"EmbeddedFiles"];
89 if([files IsValid])
90 {
91 // Traverse the list of embedded files.
92 PTDictIterator *i = [files GetIterator];
93 int counter = 0;
94 for (; [i HasNext]; [i Next], ++counter)
95 {
96 NSString *entry_name = [[i Key] GetAsPDFText];
97 NSLog(@"Part: %@", entry_name);
98 PTFileSpec *file_spec = [[PTFileSpec alloc] initWithF: [i Value]];
99 PTFilter *stm = [file_spec GetFileData];
100 if (stm)
101 {
102 NSString *str = [NSString stringWithFormat: @"../../TestFiles/Output/extract_%d.%@", counter, [entry_name pathExtension]];
103 [stm WriteToFile: str append: NO];
104 }
105 }
106 }
107
108 NSLog(@"Done.");
109 }
110 @catch(NSException *e)
111 {
112 NSLog(@"%@", e.reason);
113 ret = 1;
114 }
115 [PTPDFNet Terminate: 0];
116 return ret;
117 }
118}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales