PDFPage

Sample Obj-C code for using Apryse SDK to copy pages from one document to another, delete and rearrange pages, and use ImportPages() method for very efficient copy and merge operations. Learn more about our iOS SDK and PDF Editing & Manipulation 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
9int main(int argc, char *argv[])
10{
11 @autoreleasepool {
12 int ret = 0;
13 [PTPDFNet Initialize: 0];
14
15 // Sample 1 - Split a PDF document into multiple pages
16 @try
17 {
18 NSLog(@"_______________________________________________");
19 NSLog(@"Sample 1 - Split a PDF document into multiple pages...");
20 NSLog(@"Opening the input pdf...");
21 PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
22 [in_doc InitSecurityHandler];
23
24 int page_num = [in_doc GetPageCount];
25 int i;
26 for (i=1; i<=page_num; ++i)
27 {
28 PTPDFDoc *new_doc = [[PTPDFDoc alloc] init];
29 NSString *output_file = [@"../../TestFiles/Output/" stringByAppendingFormat: @"newsletter_split_page_%d.pdf", i];
30 [new_doc InsertPages: 0 src_doc: in_doc start_page: i end_page: i flag: e_ptinsert_none];
31 [new_doc SaveToFile: output_file flags: e_ptremove_unused];
32 NSLog(@"Done. Result saved in newsletter_split_page_%d.pdf", i);
33 }
34 }
35 @catch(NSException *e)
36 {
37 NSLog(@"%@", e.reason);
38 ret = 1;
39 }
40
41 // Sample 2 - Merge several PDF documents into one
42 @try
43 {
44 NSLog(@"_______________________________________________");
45 NSLog(@"Sample 2 - Merge several PDF documents into one...");
46 PTPDFDoc *new_doc = [[PTPDFDoc alloc] init];
47 [new_doc InitSecurityHandler];
48
49 int page_num = 15;
50 int i;
51 for (i=1; i<=page_num; ++i)
52 {
53 NSString *input_file = [@"../../TestFiles/Output/" stringByAppendingFormat: @"newsletter_split_page_%d.pdf", i];
54 NSLog(@"Opening newsletter_split_page_%d.pdf", i);
55 PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: input_file];
56 [new_doc InsertPages: i src_doc: in_doc start_page: 1 end_page: [in_doc GetPageCount] flag: e_ptinsert_none];
57 }
58 [new_doc SaveToFile: @"../../TestFiles/Output/newsletter_merge_pages.pdf" flags: e_ptremove_unused];
59 NSLog(@"Done. Result saved in newsletter_merge_pages.pdf");
60
61 }
62 @catch(NSException *e)
63 {
64 NSLog(@"%@", e.reason);
65 ret = 1;
66 }
67
68
69 // Sample 3 - Delete every second page
70 @try
71 {
72 NSLog(@"_______________________________________________");
73 NSLog(@"Sample 3 - Delete every second page...");
74 NSLog(@"Opening the input pdf...");
75 PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
76 [in_doc InitSecurityHandler];
77
78 int page_num = [in_doc GetPageCount];
79 while (page_num>=1)
80 {
81 PTPageIterator *itr = [in_doc GetPageIterator: page_num];
82 [in_doc PageRemove: itr];
83 page_num -= 2;
84 }
85
86 [in_doc SaveToFile: @"../../TestFiles/Output/newsletter_page_remove.pdf" flags: 0];
87 NSLog(@"Done. Result saved in newsletter_page_remove.pdf...");
88
89 }
90 @catch(NSException *e)
91 {
92 NSLog(@"%@", e.reason);
93 ret = 1;
94 }
95
96 // Sample 4 - Inserts a page from one document at different
97 // locations within another document
98 @try
99 {
100 NSLog(@"_______________________________________________");
101 NSLog(@"Sample 4 - Insert a page at different locations...");
102 NSLog(@"Opening the input pdf...");
103
104 PTPDFDoc *in1_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
105 [in1_doc InitSecurityHandler];
106
107 PTPDFDoc *in2_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/fish.pdf"];
108 [in2_doc InitSecurityHandler];
109
110 PTPageIterator *src_page = [in2_doc GetPageIterator: 1];
111 PTPageIterator *dst_page = [in1_doc GetPageIterator: 1];
112 int page_num = 1;
113
114 while ([dst_page HasNext]) {
115 if ((page_num++ % 3) == 0) {
116 [in1_doc PageInsert: dst_page page: [src_page Current]];
117 }
118
119 [dst_page Next];
120 }
121
122 [in1_doc SaveToFile: @"../../TestFiles/Output/newsletter_page_insert.pdf" flags: 0];
123 NSLog(@"Done. Result saved in newsletter_page_insert.pdf...");
124
125
126 }@catch(NSException *e)
127 {
128 NSLog(@"%@", e.reason);
129 ret = 1;
130 }
131
132 // Sample 5 - Replicate pages within a single document
133 @try
134 {
135
136 NSLog(@"_______________________________________________");
137 NSLog(@"Sample 5 - Replicate pages within a single document...");
138 NSLog(@"Opening the input pdf...");
139 PTPDFDoc *doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
140 [doc InitSecurityHandler];
141
142 // Replicate the cover page three times (copy page #1 and place it before the
143 // seventh page in the document page sequence)
144 PTPage *cover = [doc GetPage: 1];
145 PTPageIterator *p7 = [doc GetPageIterator: 7];
146 [doc PageInsert: p7 page: cover];
147 [doc PageInsert: p7 page: cover];
148 [doc PageInsert: p7 page: cover];
149
150 // Replicate the cover page two more times by placing it before and after
151 // existing pages.
152 [doc PagePushFront: cover];
153 [doc PagePushBack: cover];
154
155 [doc SaveToFile: @"../../TestFiles/Output/newsletter_page_clone.pdf" flags: 0];
156 NSLog(@"Done. Result saved in newsletter_page_clone.pdf...");
157
158 }
159 @catch(NSException *e)
160 {
161 NSLog(@"%@", e.reason);
162 ret = 1;
163 }
164
165 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
166 // in order to preserve shared resources between pages (e.g. images, fonts,
167 // colorspaces, etc.)
168 @try
169 {
170
171 NSLog(@"_______________________________________________");
172 NSLog(@"Sample 6 - Preserving shared resources using ImportPages...");
173 NSLog(@"Opening the input pdf...");
174 PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: @"../../TestFiles/newsletter.pdf"];
175 [in_doc InitSecurityHandler];
176
177 PTPDFDoc *new_doc = [[PTPDFDoc alloc] init];
178
179 PTVectorPage *copy_pages = [[PTVectorPage alloc] init];
180 PTPageIterator *itr;
181 for (itr=[in_doc GetPageIterator: 1]; [itr HasNext]; [itr Next])
182 {
183 [copy_pages add: [itr Current]];
184 }
185
186 PTVectorPage *imported_pages = [new_doc ImportPages: copy_pages import_bookmarks: NO];
187 int i;
188 for (i=0; i<[imported_pages size]; ++i)
189 {
190 [new_doc PagePushFront: [imported_pages get: i]]; // Order pages in reverse order.
191 // Use PagePushBack() if you would like to preserve the same order.
192 }
193
194 [new_doc SaveToFile: @"../../TestFiles/Output/newsletter_import_pages.pdf" flags: 0];
195 NSLog(@"Done. Result saved in newsletter_import_pages.pdf...");
196 NSLog(@"\n");
197 NSLog(@"Note that the output file size is less than half the size");
198 NSLog(@"of the file produced using individual page copy operations");
199 NSLog(@"between two documents");
200
201 }
202 @catch(NSException *e)
203 {
204 NSLog(@"%@", e.reason);
205 ret = 1;
206 }
207 [PTPDFNet Terminate: 0];
208 return ret;
209 }
210}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales