Sample Obj-C code for using Apryse SDK to impose (combine) multiple PDF pages. Page imposition can be used to arrange/order pages prior to printing or for document assembly (assemble a 'master' page from several 'source' pages). It is also possible to write applications that can re-order the pages such that they will display in the correct order when the hard copy pages are compiled and folded correctly. 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
9//-----------------------------------------------------------------------------------
10// The sample illustrates how multiple pages can be combined/imposed
11// using PDFNet. Page imposition can be used to arrange/order pages
12// prior to printing or to assemble a 'master' page from several 'source'
13// pages. Using PDFNet API it is possible to write applications that can
14// re-order the pages such that they will display in the correct order
15// when the hard copy pages are compiled and folded correctly.
16//-----------------------------------------------------------------------------------
17
18int main(int argc, char *argv[])
19{
20 @autoreleasepool {
21 int ret = 0;
22 [PTPDFNet Initialize: 0];
23
24 // Relative path to the folder containing test files.
25 NSString *input_path = @"../../TestFiles/newsletter.pdf";
26 NSString *output_path = @"../../TestFiles/Output/newsletter_booklet.pdf";
27
28 @try
29 {
30 NSLog(@"-------------------------------------------------");
31 NSLog(@"Opening the input pdf...");
32
33 NSString *filein = input_path;
34 NSString *fileout = output_path;
35
36 PTPDFDoc *in_doc = [[PTPDFDoc alloc] initWithFilepath: filein];
37 [in_doc InitSecurityHandler];
38
39 // Create a list of pages to import from one PDF document to another.
40 PTVectorPage *import_pages = [[PTVectorPage alloc] init];
41 PTPageIterator *itr;
42 for (itr=[in_doc GetPageIterator: 1]; [itr HasNext]; [itr Next]) {
43 [import_pages add: [itr Current]];
44 }
45
46 PTPDFDoc *new_doc = [[PTPDFDoc alloc] init];
47 PTVectorPage *imported_pages = [new_doc ImportPages: import_pages import_bookmarks: NO];
48
49 // Paper dimension for A3 format in points. Because one inch has
50 // 72 points, 11.69 inch 72 = 841.69 points
51 PTPDFRect * media_box = [[PTPDFRect alloc] initWithX1: 0 y1: 0 x2: 1190.88 y2: 841.69];
52 double mid_point = [media_box Width]/2;
53
54 PTElementBuilder *builder = [[PTElementBuilder alloc] init];
55 PTElementWriter *writer = [[PTElementWriter alloc] init];
56
57 size_t i;
58 for (i=0; i<[imported_pages size]; ++i)
59 {
60 // Create a blank new A3 page and place on it two pages from the input document.
61 PTPage *new_page = [new_doc PageCreate: media_box];
62 [writer WriterBeginWithPage: new_page placement: e_ptoverlay page_coord_sys: YES compress: YES resources: NULL];
63
64 // Place the first page
65 PTPage *src_page = [imported_pages get:(int)i];
66 PTElement *element = [builder CreateFormWithPage: src_page];
67
68 double sc_x = mid_point / [src_page GetPageWidth: e_ptcrop];
69 double sc_y = [media_box Height] / [src_page GetPageHeight: e_ptcrop];
70 double scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
71 [[element GetGState] SetTransform: scale b: 0 c: 0 d: scale h: 0 v: 0];
72 [writer WritePlacedElement: element];
73
74 // Place the second page
75 ++i;
76 if (i<[imported_pages size]) {
77 src_page = [imported_pages get:(int)i];
78 element = [builder CreateFormWithPage: src_page];
79 sc_x = mid_point / [src_page GetPageWidth: e_ptcrop];
80 sc_y = [media_box Height] / [src_page GetPageHeight: e_ptcrop];
81 scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
82 [[element GetGState] SetTransform: scale b: 0 c: 0 d: scale h: mid_point v: 0];
83 [writer WritePlacedElement: element];
84 }
85
86 [writer End];
87 [new_doc PagePushBack: new_page];
88 }
89
90 [new_doc SaveToFile: fileout flags: e_ptlinearized ];
91 NSLog(@"Done. Result saved in newsletter_booklet.pdf...");
92 }
93 @catch(NSException *e)
94 {
95 NSLog(@"%@", e.reason);
96 ret = 1;
97 }
98 [PTPDFNet Terminate: 0];
99 return ret;
100 }
101}
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 sample illustrates how multiple pages can be combined/imposed
11// using PDFNet. Page imposition can be used to arrange/order pages
12// prior to printing or to assemble a 'master' page from several 'source'
13// pages. Using PDFNet API it is possible to write applications that can
14// re-order the pages such that they will display in the correct order
15// when the hard copy pages are compiled and folded correctly.
16//-----------------------------------------------------------------------------------
17func runImpositionTest() -> Int {
18 return autoreleasepool {
19 var ret: Int = 0
20
21
22 // Relative path to the folder containing test files.
23 let input_path: String? = Bundle.main.path(forResource: "newsletter", ofType: "pdf")
24 let output_path: String = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]).appendingPathComponent("newsletter_booklet.pdf").path
25
26 do {
27 try PTPDFNet.catchException {
28 print("-------------------------------------------------")
29 print("Opening the input pdf...")
30
31 let filein: String? = input_path
32 let fileout: String = output_path
33
34 let in_doc: PTPDFDoc = PTPDFDoc(filepath: filein)
35 in_doc.initSecurityHandler()
36
37 // Create a list of pages to import from one PDF document to another.
38 let import_pages: PTVectorPage = PTVectorPage()
39 let itr: PTPageIterator = in_doc.getPageIterator(1)
40 while itr.hasNext() {
41 import_pages.add(itr.current())
42 itr.next()
43 }
44
45 let new_doc: PTPDFDoc = PTPDFDoc()
46 let imported_pages: PTVectorPage = new_doc.importPages(import_pages, import_bookmarks: false)
47
48 // Paper dimension for A3 format in points. Because one inch has
49 // 72 points, 11.69 inch 72 = 841.69 points
50 let media_box: PTPDFRect = PTPDFRect(x1: 0, y1: 0, x2: 1190.88, y2: 841.69)
51 let mid_point: Double = media_box.width() / 2
52
53 let builder: PTElementBuilder = PTElementBuilder()
54 let writer: PTElementWriter = PTElementWriter()
55
56 var i: Int32 = 0
57 while i < imported_pages.size() {
58 // Create a blank new A3 page and place on it two pages from the input document.
59 let new_page: PTPage = new_doc.pageCreate(media_box)
60
61 writer.writerBegin(with: new_page, placement: e_ptoverlay, page_coord_sys: true, compress: true, resources: nil)
62
63 // Place the first page
64 var src_page: PTPage = imported_pages.get(i)
65 var element: PTElement = builder.createForm(with: src_page)
66
67 var sc_x: Double = mid_point / src_page.getWidth(e_ptcrop)
68 var sc_y: Double = media_box.height() / src_page.getHeight(e_ptcrop)
69 var scale: Double = sc_x < sc_y ? sc_x : sc_y // min(sc_x, sc_y)
70 element.getGState().setTransform(scale, b: 0, c: 0, d: scale, h: 0, v: 0)
71 writer.writePlacedElement(element)
72
73 // Place the second page
74 i += 1
75 if i < imported_pages.size() {
76 src_page = imported_pages.get(i)
77 element = builder.createForm(with: src_page)
78 sc_x = mid_point / src_page.getWidth(e_ptcrop)
79 sc_y = media_box.height() / src_page.getHeight(e_ptcrop)
80 scale = sc_x < sc_y ? sc_x : sc_y
81 // min(sc_x, sc_y)
82 element.getGState().setTransform(scale, b: 0, c: 0, d: scale, h: mid_point, v: 0)
83 writer.writePlacedElement(element)
84 }
85
86 writer.end()
87 new_doc.pagePushBack(new_page)
88 i += 1
89 }
90
91 new_doc.save(toFile: fileout, flags: e_ptlinearized.rawValue)
92 print("Done. Result saved in newsletter_booklet.pdf...")
93 }
94 } catch let e as NSError {
95 print("\(e)")
96 ret = 1
97 }
98
99 print("Done.")
100 return ret
101 }
102}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales