//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- import pdftron.PDF.*; import pdftron.SDF.SDFDoc; //----------------------------------------------------------------------------------- // The sample illustrates how multiple pages can be combined/imposed // using PDFNet. Page imposition can be used to arrange/order pages // prior to printing or to assemble a 'master' page from several 'source' // pages. Using PDFNet API it is 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. //----------------------------------------------------------------------------------- public class ImpositionTest { public static void main(String[] args) { PDFNet.initialize(); String resource_path = args.length>3 ? args[3] : "../../../resources"; PDFNet.setResourcesPath(resource_path); // Relative path to the folder containing test files. String input_path = "../../TestFiles/newsletter.pdf"; String output_path = "../../TestFiles/Output/newsletter_booklet.pdf"; try { System.out.println("-------------------------------------------------"); System.out.println("Opening the input pdf..."); String filein = args.length>1 ? args[1] : input_path; String fileout = args.length>2 ? args[2] : output_path; PDFDoc in_doc=new PDFDoc(filein); in_doc.initSecurityHandler(); PDFDoc new_doc=new PDFDoc(); // Paper dimension for A3 format in points. Because one inch has // 72 points, 11.69 inch 72 = 841.69 points Rect media_box=new Rect(0, 0, 1190.88, 841.69); double mid_point = media_box.getWidth()/2; ElementBuilder builder=new ElementBuilder(); ElementWriter writer=new ElementWriter(); PageIterator itr = in_doc.getPageIterator(); while (itr.hasNext()) { // Create a blank new A3 page and place on it two pages from the input document. Page new_page = new_doc.pageCreate(media_box); writer.begin(new_page); // Place the first page Page src_page = (Page)(itr.next()); Element element = builder.createForm(src_page, new_doc); double sc_x = mid_point / src_page.getPageWidth(); double sc_y = media_box.getHeight() / src_page.getPageHeight(); double scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y) element.getGState().setTransform(scale, 0, 0, scale, 0, 0); writer.writePlacedElement(element); // Place the second page if (itr.hasNext()) { src_page = (Page)(itr.next()); element = builder.createForm(src_page, new_doc); sc_x = mid_point / src_page.getPageWidth(); sc_y = media_box.getHeight() / src_page.getPageHeight(); scale = sc_x < sc_y ? sc_x : sc_y; // min(sc_x, sc_y) element.getGState().setTransform(scale, 0, 0, scale, mid_point, 0); writer.writePlacedElement(element); } writer.end(); new_doc.pagePushBack(new_page); } new_doc.save(fileout, SDFDoc.e_linearized, null); System.out.println("Done. Result saved in newsletter_booklet.pdf..."); } catch(Exception e) { e.printStackTrace(); } PDFNet.terminate(); } }