Combine PDFs with Page Imposition - Python Sample Code

Sample code for using Apryse SDK to combine (impose) 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. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14#-----------------------------------------------------------------------------------
15# The sample illustrates how multiple pages can be combined/imposed
16# using PDFNet. Page imposition can be used to arrange/order pages
17# prior to printing or to assemble a 'master' page from several 'source'
18# pages. Using PDFNet API it is possible to write applications that can
19# re-order the pages such that they will display in the correct order
20# when the hard copy pages are compiled and folded correctly.
21#-----------------------------------------------------------------------------------
22
23def main(args):
24 PDFNet.Initialize(LicenseKey)
25
26 resource_path = args[3] if len(args) > 3 else "../../../resources"
27
28 # Relative path to the folder containing the test files.
29 input_path = "../../TestFiles/newsletter.pdf"
30 output_path = "../../TestFiles/Output/newsletter_booklet.pdf"
31
32 print("-------------------------------------------------")
33 print("Opening the input pdf...")
34
35 filein = args[1] if len(args)>1 else input_path
36 fileout = args[2] if len(args)>2 else output_path
37
38 in_doc = PDFDoc(filein)
39 in_doc.InitSecurityHandler()
40
41 # Create a list of pages to import from one PDF document to another
42 import_pages = VectorPage()
43 itr = in_doc.GetPageIterator()
44 while itr.HasNext():
45 import_pages.append(itr.Current())
46 itr.Next()
47
48 new_doc = PDFDoc()
49 imported_pages = new_doc.ImportPages(import_pages)
50
51 # Paper dimension for A3 format in points. Because one inch has
52 # 72 points, 11.69 inch 72 = 841.69 points
53 media_box = Rect(0, 0, 1190.88, 841.69)
54 mid_point = media_box.Width()/2
55
56 builder = ElementBuilder()
57 writer = ElementWriter()
58
59 i = 0
60 while i < len(imported_pages):
61 # Create a blank new A3 page and place on it two pages from the input document.
62 new_page = new_doc.PageCreate(media_box)
63 writer.Begin(new_page)
64
65 # Place the first page
66 src_page = imported_pages[i]
67
68 element = builder.CreateForm(imported_pages[i])
69 sc_x = mid_point / src_page.GetPageWidth()
70 sc_y = media_box.Height() / src_page.GetPageHeight()
71 scale = sc_x if sc_x < sc_y else sc_y # min(sc_x, sc_y)
72 element.GetGState().SetTransform(scale, 0, 0, scale, 0, 0)
73 writer.WritePlacedElement(element)
74
75 # Place the second page
76 i = i + 1
77 if i < len(imported_pages):
78 src_page = imported_pages[i]
79 element = builder.CreateForm(src_page)
80 sc_x = mid_point / src_page.GetPageWidth()
81 sc_y = media_box.Height() / src_page.GetPageHeight()
82 scale = sc_x if sc_x < sc_y else sc_y # min(sc_x, sc_y)
83 element.GetGState().SetTransform(scale, 0, 0, scale, mid_point, 0)
84 writer.WritePlacedElement(element)
85
86 writer.End()
87 new_doc.PagePushBack(new_page)
88 i = i + 1
89
90 new_doc.Save(fileout, SDFDoc.e_linearized)
91 PDFNet.Terminate()
92 print("Done. Result saved in newsletter_booklet.pdf...")
93
94if __name__ == '__main__':
95 args = []
96 main(args)

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales