Combine PDFs with Page Imposition - Java 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-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.pdf.*;
7import com.pdftron.sdf.SDFDoc;
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//-----------------------------------------------------------------------------------
17public class ImpositionTest {
18
19 public static void main(String[] args) {
20 PDFNet.initialize(PDFTronLicense.Key());
21
22 // Relative path to the folder containing test files.
23 String input_path = "../../TestFiles/";
24 String output_path = "../../TestFiles/Output/";
25
26 String filein = input_path + "newsletter.pdf";
27 String fileout = output_path + "newsletter_booklet.pdf";
28
29 System.out.println("-------------------------------------------------");
30 System.out.println("Opening the input pdf...");
31 try (PDFDoc in_doc = new PDFDoc(filein)) {
32 in_doc.initSecurityHandler();
33
34 // Create a list of pages to import from one PDF document to another.
35 Page[] copy_pages = new Page[in_doc.getPageCount()];
36 int j = 0;
37 for (PageIterator itr = in_doc.getPageIterator(); itr.hasNext(); j++) {
38 copy_pages[j] = itr.next();
39 }
40
41 try (PDFDoc new_doc = new PDFDoc()) {
42 Page[] imported_pages = new_doc.importPages(copy_pages);
43
44 // Paper dimension for A3 format in points. Because one inch has
45 // 72 points, 11.69 inch 72 = 841.69 points
46 Rect media_box = new Rect(0, 0, 1190.88, 841.69);
47 double mid_point = media_box.getWidth() / 2;
48
49 ElementBuilder builder = new ElementBuilder();
50 ElementWriter writer = new ElementWriter();
51
52 for (int i = 0; i < imported_pages.length; ++i) {
53 // Create a blank new A3 page and place on it two pages from the input document.
54 Page new_page = new_doc.pageCreate(media_box);
55 writer.begin(new_page);
56
57 // Place the first page
58 Page src_page = imported_pages[i];
59 Element element = builder.createForm(src_page);
60
61 double sc_x = mid_point / src_page.getPageWidth();
62 double sc_y = media_box.getHeight() / src_page.getPageHeight();
63 double scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
64 element.getGState().setTransform(scale, 0, 0, scale, 0, 0);
65 writer.writePlacedElement(element);
66
67 // Place the second page
68 ++i;
69 if (i < imported_pages.length) {
70 src_page = imported_pages[i];
71 element = builder.createForm(src_page);
72 sc_x = mid_point / src_page.getPageWidth();
73 sc_y = media_box.getHeight() / src_page.getPageHeight();
74 scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
75 element.getGState().setTransform(scale, 0, 0, scale, mid_point, 0);
76 writer.writePlacedElement(element);
77 }
78
79 writer.end();
80 new_doc.pagePushBack(new_page);
81 }
82
83 new_doc.save(fileout, SDFDoc.SaveMode.LINEARIZED, null);
84 System.out.println("Done. Result saved in newsletter_booklet.pdf...");
85 }
86 } catch (Exception e) {
87 e.printStackTrace();
88 }
89
90 PDFNet.terminate();
91 }
92}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales