Imposition

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}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales