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}
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.*
13import com.pdftron.sdf.SDFDoc
14import java.util.*
15
16//-----------------------------------------------------------------------------------
17//The sample illustrates how multiple pages can be combined/imposed
18//using PDFNet. Page imposition can be used to arrange/order pages
19//prior to printing or to assemble a 'master' page from several 'source'
20//pages. Using PDFNet API it is possible to write applications that can
21//re-order the pages such that they will display in the correct order
22//when the hard copy pages are compiled and folded correctly.
23//-----------------------------------------------------------------------------------
24
25class ImpositionTest : PDFNetSample() {
26 init {
27 setTitle(R.string.sample_imposition_title)
28 setDescription(R.string.sample_imposition_description)
29 }
30
31 override fun run(outputListener: OutputListener?) {
32 super.run(outputListener)
33 mOutputListener = outputListener
34 mFileList.clear()
35 printHeader(outputListener!!)
36
37 val filein = Utils.getAssetTempFile(PDFNetSample.INPUT_PATH + "newsletter.pdf")!!.absolutePath
38 val fileout = Utils.createExternalFile("newsletter_booklet.pdf", mFileList).absolutePath
39
40 mOutputListener!!.println("-------------------------------------------------")
41 mOutputListener!!.println("Opening the input pdf...")
42 try {
43 PDFDoc(filein).use { in_doc ->
44 in_doc.initSecurityHandler()
45
46 // Create a list of pages to import from one PDF document to another.
47 val copy_pages = arrayOfNulls<Page>(in_doc.pageCount)
48 var j = 0
49 val itr = in_doc.pageIterator
50 while (itr.hasNext()) {
51 copy_pages[j] = itr.next()
52 j++
53 }
54
55 PDFDoc().use { new_doc ->
56 val imported_pages = new_doc.importPages(copy_pages)
57
58 // Paper dimension for A3 format in points. Because one inch has
59 // 72 points, 11.69 inch 72 = 841.69 points
60 val media_box = Rect(0.0, 0.0, 1190.88, 841.69)
61 val mid_point = media_box.width / 2
62
63 val builder = ElementBuilder()
64 val writer = ElementWriter()
65
66 var i = 0
67 while (i < imported_pages.size) {
68 // Create a blank new A3 page and place on it two pages from the input document.
69 val new_page = new_doc.pageCreate(media_box)
70 writer.begin(new_page)
71
72 // Place the first page
73 var src_page = imported_pages[i]
74 var element = builder.createForm(src_page)
75
76 var sc_x = mid_point / src_page.pageWidth
77 var sc_y = media_box.height / src_page.pageHeight
78 var scale = if (sc_x < sc_y) sc_x else sc_y // min(sc_x, sc_y)
79 element.gState.setTransform(scale, 0.0, 0.0, scale, 0.0, 0.0)
80 writer.writePlacedElement(element)
81
82 // Place the second page
83 ++i
84 if (i < imported_pages.size) {
85 src_page = imported_pages[i]
86 element = builder.createForm(src_page)
87 sc_x = mid_point / src_page.pageWidth
88 sc_y = media_box.height / src_page.pageHeight
89 scale = if (sc_x < sc_y) sc_x else sc_y // min(sc_x, sc_y)
90 element.gState.setTransform(scale, 0.0, 0.0, scale, mid_point, 0.0)
91 writer.writePlacedElement(element)
92 }
93
94 writer.end()
95 new_doc.pagePushBack(new_page)
96 ++i
97 }
98
99 new_doc.save(fileout, SDFDoc.SaveMode.LINEARIZED, null)
100 mOutputListener!!.println("Done. Result saved in newsletter_booklet.pdf...")
101 }
102 }
103 } catch (e: Exception) {
104 mOutputListener!!.printError(e.stackTrace)
105 }
106
107 for (file in mFileList) {
108 addToFileList(file)
109 }
110 printFooter(outputListener)
111 }
112
113 companion object {
114
115 private var mOutputListener: OutputListener? = null
116
117 private val mFileList = ArrayList<String>()
118 }
119
120}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales