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}
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
9func runPDFPageTest() -> Int {
10 return autoreleasepool {
11 var ret = 0
12
13
14 // Sample 1 - Split a PDF document into multiple pages
15 do {
16 try PTPDFNet.catchException {
17 print("_______________________________________________")
18 print("Sample 1 - Split a PDF document into multiple pages...")
19 print("Opening the input pdf...")
20 let in_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
21 in_doc.initSecurityHandler()
22
23 let page_num = in_doc.getPageCount()
24 for i in 1...page_num {
25 let new_doc: PTPDFDoc = PTPDFDoc()
26 let output_file: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_split_page_\(i).pdf").path
27 new_doc.insertPages(0, src_doc: in_doc, start_page: i, end_page: i, flag: e_ptinsert_none)
28 new_doc.save(toFile: output_file, flags: e_ptremove_unused.rawValue)
29 print("Done. Result saved in newsletter_split_page_\(i).pdf")
30 }
31 }
32 } catch let e as NSError {
33 print("\(e)")
34 ret = 1
35 }
36
37 // Sample 2 - Merge several PDF documents into one
38 do {
39 try PTPDFNet.catchException {
40 print("_______________________________________________")
41 print("Sample 2 - Merge several PDF documents into one...")
42 let new_doc: PTPDFDoc = PTPDFDoc()
43 new_doc.initSecurityHandler()
44
45 let page_num: Int = 15
46 for i in 1...page_num {
47 let input_file: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_split_page_\(i).pdf").path
48 print("Opening newsletter_split_page_\(i).pdf")
49 let in_doc: PTPDFDoc = PTPDFDoc(filepath: input_file)
50 new_doc.insertPages(Int32(i), src_doc: in_doc, start_page: 1, end_page: in_doc.getPageCount(), flag: e_ptinsert_none)
51 }
52 new_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_merge_pages.pdf").path, flags: e_ptremove_unused.rawValue)
53 print("Done. Result saved in newsletter_merge_pages.pdf")
54 }
55 } catch let e as NSError {
56 print("\(e)")
57 ret = 1
58 }
59
60 // Sample 3 - Delete every second page
61 do {
62 try PTPDFNet.catchException {
63 print("_______________________________________________")
64 print("Sample 3 - Delete every second page...")
65 print("Opening the input pdf...")
66 let in_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
67 in_doc.initSecurityHandler()
68
69 var page_num = in_doc.getPageCount()
70 while page_num >= 1 {
71 let itr: PTPageIterator = in_doc.getPageIterator(UInt32(page_num))
72 in_doc.pageRemove(itr)
73 page_num -= 2
74 }
75
76 in_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_page_remove.pdf").path, flags: 0)
77 print("Done. Result saved in newsletter_page_remove.pdf...")
78 }
79 } catch let e as NSError {
80 print("\(e)")
81 ret = 1
82 }
83
84 // Sample 4 - Inserts a page from one document at different
85 // locations within another document
86 do {
87 try PTPDFNet.catchException {
88 print("_______________________________________________")
89 print("Sample 4 - Insert a page at different locations...")
90 print("Opening the input pdf...")
91
92 let in1_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
93 in1_doc.initSecurityHandler()
94
95 let in2_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "fish", ofType: "pdf"))
96 in2_doc.initSecurityHandler()
97
98 let src_page: PTPageIterator = in2_doc.getPageIterator(1)
99 let dst_page: PTPageIterator = in1_doc.getPageIterator(1)
100 var page_num: Int = 1
101 while dst_page.hasNext() {
102 if (page_num % 3) == 0 {
103 in1_doc.pageInsert(dst_page, page: src_page.current())
104 }
105 page_num += 1
106 dst_page.next()
107 }
108
109 in1_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_page_insert.pdf").path, flags: 0)
110 print("Done. Result saved in newsletter_page_insert.pdf...")
111 }
112 } catch let e as NSError {
113 print("\(e)")
114 ret = 1
115 }
116
117 // Sample 5 - Replicate pages within a single document
118 do {
119 try PTPDFNet.catchException {
120 print("_______________________________________________")
121 print("Sample 5 - Replicate pages within a single document...")
122 print("Opening the input pdf...")
123 let doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
124 doc.initSecurityHandler()
125
126 // Replicate the cover page three times (copy page #1 and place it before the
127 // seventh page in the document page sequence)
128 let cover: PTPage = doc.getPage(1)
129 let p7: PTPageIterator = doc.getPageIterator(7)
130 doc.pageInsert(p7, page: cover)
131 doc.pageInsert(p7, page: cover)
132 doc.pageInsert(p7, page: cover)
133
134 // Replicate the cover page two more times by placing it before and after
135 // existing pages.
136 doc.pagePushFront(cover)
137 doc.pagePushBack(cover)
138
139 doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_page_clone.pdf").path, flags: 0)
140 print("Done. Result saved in newsletter_page_clone.pdf...")
141 }
142 } catch let e as NSError {
143 print("\(e)")
144 ret = 1
145 }
146
147 // Sample 6 - Use ImportPages() in order to copy multiple pages at once
148 // in order to preserve shared resources between pages (e.g. images, fonts,
149 // colorspaces, etc.)
150 do {
151 try PTPDFNet.catchException {
152 print("_______________________________________________")
153 print("Sample 6 - Preserving shared resources using ImportPages...")
154 print("Opening the input pdf...")
155 let in_doc: PTPDFDoc = PTPDFDoc(filepath: Bundle.main.path(forResource: "newsletter", ofType: "pdf"))
156 in_doc.initSecurityHandler()
157
158 let new_doc: PTPDFDoc = PTPDFDoc()
159
160 let copy_pages: PTVectorPage = PTVectorPage()
161 let itr: PTPageIterator = in_doc.getPageIterator(1)
162 while itr.hasNext() {
163 copy_pages.add(itr.current())
164 itr.next()
165 }
166
167 let imported_pages: PTVectorPage = new_doc.importPages(copy_pages, import_bookmarks: false)
168 for i in 0..<imported_pages.size() {
169 new_doc.pagePushFront(imported_pages.get(Int32(i)))
170 // Order pages in reverse order.
171 // Use PagePushBack() if you would like to preserve the same order.
172 }
173
174 new_doc.save(toFile: URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_import_pages.pdf").path, flags: 0)
175 print("Done. Result saved in newsletter_import_pages.pdf...")
176
177 print("Note that the output file size is less than half the size of the file produced using individual page copy operations between two documents")
178 }
179 } catch let e as NSError {
180 print("\(e)")
181 ret = 1
182 }
183
184 return ret
185 }
186}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales