Imposition

Sample Java 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 Android SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.pdf.Element;
13import com.pdftron.pdf.ElementBuilder;
14import com.pdftron.pdf.ElementWriter;
15import com.pdftron.pdf.PDFDoc;
16import com.pdftron.pdf.Page;
17import com.pdftron.pdf.PageIterator;
18import com.pdftron.pdf.Rect;
19import com.pdftron.sdf.SDFDoc;
20
21import java.util.ArrayList;
22
23//-----------------------------------------------------------------------------------
24//The sample illustrates how multiple pages can be combined/imposed
25//using PDFNet. Page imposition can be used to arrange/order pages
26//prior to printing or to assemble a 'master' page from several 'source'
27//pages. Using PDFNet API it is possible to write applications that can
28//re-order the pages such that they will display in the correct order
29//when the hard copy pages are compiled and folded correctly.
30//-----------------------------------------------------------------------------------
31
32public class ImpositionTest extends PDFNetSample {
33
34 private static OutputListener mOutputListener;
35
36 private static ArrayList<String> mFileList = new ArrayList<>();
37
38 public ImpositionTest() {
39 setTitle(R.string.sample_imposition_title);
40 setDescription(R.string.sample_imposition_description);
41 }
42
43 @Override
44 public void run(OutputListener outputListener) {
45 super.run(outputListener);
46 mOutputListener = outputListener;
47 mFileList.clear();
48 printHeader(outputListener);
49
50 String filein = Utils.getAssetTempFile(INPUT_PATH + "newsletter.pdf").getAbsolutePath();
51 String fileout = Utils.createExternalFile("newsletter_booklet.pdf", mFileList).getAbsolutePath();
52
53 mOutputListener.println("-------------------------------------------------");
54 mOutputListener.println("Opening the input pdf...");
55 try (PDFDoc in_doc = new PDFDoc(filein)) {
56 in_doc.initSecurityHandler();
57
58 // Create a list of pages to import from one PDF document to another.
59 Page[] copy_pages = new Page[in_doc.getPageCount()];
60 int j = 0;
61 for (PageIterator itr = in_doc.getPageIterator(); itr.hasNext(); j++) {
62 copy_pages[j] = itr.next();
63 }
64
65 try (PDFDoc new_doc = new PDFDoc()) {
66 Page[] imported_pages = new_doc.importPages(copy_pages);
67
68 // Paper dimension for A3 format in points. Because one inch has
69 // 72 points, 11.69 inch 72 = 841.69 points
70 Rect media_box = new Rect(0, 0, 1190.88, 841.69);
71 double mid_point = media_box.getWidth() / 2;
72
73 ElementBuilder builder = new ElementBuilder();
74 ElementWriter writer = new ElementWriter();
75
76 for (int i = 0; i < imported_pages.length; ++i) {
77 // Create a blank new A3 page and place on it two pages from the input document.
78 Page new_page = new_doc.pageCreate(media_box);
79 writer.begin(new_page);
80
81 // Place the first page
82 Page src_page = imported_pages[i];
83 Element element = builder.createForm(src_page);
84
85 double sc_x = mid_point / src_page.getPageWidth();
86 double sc_y = media_box.getHeight() / src_page.getPageHeight();
87 double scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
88 element.getGState().setTransform(scale, 0, 0, scale, 0, 0);
89 writer.writePlacedElement(element);
90
91 // Place the second page
92 ++i;
93 if (i < imported_pages.length) {
94 src_page = imported_pages[i];
95 element = builder.createForm(src_page);
96 sc_x = mid_point / src_page.getPageWidth();
97 sc_y = media_box.getHeight() / src_page.getPageHeight();
98 scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y)
99 element.getGState().setTransform(scale, 0, 0, scale, mid_point, 0);
100 writer.writePlacedElement(element);
101 }
102
103 writer.end();
104 new_doc.pagePushBack(new_page);
105 }
106
107 new_doc.save(fileout, SDFDoc.SaveMode.LINEARIZED, null);
108 mOutputListener.println("Done. Result saved in newsletter_booklet.pdf...");
109 }
110 } catch (Exception e) {
111 mOutputListener.printError(e.getStackTrace());
112 }
113
114 for (String file : mFileList) {
115 addToFileList(file);
116 }
117 printFooter(outputListener);
118 }
119
120}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales