//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- import pdftron.PDF.*; import pdftron.SDF.*; import pdftron.Common.PDFNetException; public class PDFPageTest { public static void main(String[] args) { PDFNet.initialize(); // Relative path to the folder containing test files. String input_path = "../../TestFiles/"; String output_path = "../../TestFiles/Output/"; // Sample 1 - Split a PDF document into multiple pages try { System.out.println("_______________________________________________"); System.out.println("Sample 1 - Delete every second page..."); System.out.println("Opening the input pdf..."); PDFDoc in_doc=new PDFDoc(input_path + "newsletter.pdf"); in_doc.initSecurityHandler(); int page_num = in_doc.getPageCount(); for (int i=1; i<=page_num; ++i) { PDFDoc new_doc=new PDFDoc(); new_doc.insertPages(0, in_doc, i, i, PDFDoc.e_none, null); new_doc.save(output_path + "newsletter_split_page_" + i + ".pdf", SDFDoc.e_remove_unused, null); System.out.println("Done. Result saved in newsletter_split_page_" + i + ".pdf"); new_doc.close(); } in_doc.close(); } catch (Exception e2) { System.out.println(e2); } // Sample 2 - Merge several PDF documents into one try { System.out.println("_______________________________________________"); System.out.println("Sample 2 - Merge several PDF documents into one..."); PDFDoc new_doc=new PDFDoc(); new_doc.initSecurityHandler(); int page_num = 15; for (int i=1; i<=page_num; ++i) { System.out.println("Opening newsletter_split_page_" + i + ".pdf"); PDFDoc in_doc=new PDFDoc(output_path + "newsletter_split_page_" + i + ".pdf"); new_doc.insertPages(i, in_doc, 1, in_doc.getPageCount(), PDFDoc.e_none, null); in_doc.close(); } new_doc.save(output_path + "newsletter_merge_pages.pdf", SDFDoc.e_remove_unused, null); System.out.println("Done. Result saved in newsletter_merge_pages.pdf..."); new_doc.close(); } catch (Exception e2) { System.out.println(e2); } // Sample 3 - Delete every second page try { System.out.println("_______________________________________________"); System.out.println("Sample 3 - Delete every second page..."); System.out.println("Opening the input pdf..."); PDFDoc in_doc=new PDFDoc(input_path + "newsletter.pdf"); in_doc.initSecurityHandler(); int page_num = in_doc.getPageCount(); while (page_num>=1) { PageIterator itr = in_doc.getPageIterator(page_num); in_doc.pageRemove(itr); page_num -= 2; } in_doc.save((output_path + "newsletter_page_remove.pdf"), 0, null); System.out.println("Done. Result saved in newsletter_page_remove.pdf..."); //Close the open document to free up document //memory sooner than waiting for the //garbage collector in_doc.close(); } catch(Exception e2) { System.out.println(e2); } // Sample 4 - Inserts a page from one document at different // locations within another document try { System.out.println("_______________________________________________"); System.out.println("Sample 4 - Insert a page at different locations..."); System.out.println("Opening the input pdf..."); PDFDoc in1_doc=new PDFDoc((input_path + "newsletter.pdf")); in1_doc.initSecurityHandler(); PDFDoc in2_doc=new PDFDoc((input_path + "fish.pdf")); in2_doc.initSecurityHandler(); PageIterator src_page_itr = in2_doc.getPageIterator(); int page_num = in1_doc.getPageCount(); Page src_page=(Page)(src_page_itr.next()); for (int i=1; i