//--------------------------------------------------------------------------------------- // 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 code shows how to edit the page display list and how to modify graphics state // attributes on existing Elements. In particular the sample program strips all images from // the page and changes text color to blue. //--------------------------------------------------------------------------------------- public class ElementEditTest { public static void main(String[] args) { PDFNet.initialize(); PDFNet.setResourcesPath("../../../resources"); // Relative path to the folder containing test files. String input_path = "../../TestFiles/"; String output_path = "../../TestFiles/Output/"; try { PDFDoc doc=new PDFDoc((input_path + "newsletter.pdf")); doc.initSecurityHandler(); int num_pages = doc.getPageCount(); ElementWriter writer=new ElementWriter(); ElementReader reader=new ElementReader(); Element element; for(int i=1; i<=num_pages; ++i) { PageIterator itr = doc.getPageIterator(i); Page page=(Page)(itr.next()); reader.begin(page); Page new_page = doc.pageCreate(); PageIterator next_page = itr; doc.pageInsert(next_page, new_page ); writer.begin(new_page); while ((element = reader.next())!=null) // Read page contents { if (element.getType() == Element.e_text) { // Set all text to blue color. GState gs = element.getGState(); gs.setFillColorSpace(ColorSpace.createDeviceRGB()); gs.setFillColor(new ColorPt(0, 0, 1)); } else if (element.getType() == Element.e_image) { // remove all images continue; } writer.writeElement(element); } writer.end(); reader.end(); new_page.setMediaBox(page.getCropBox()); doc.pageRemove(doc.getPageIterator(i)); } doc.save((output_path + "newsletter_edited.pdf"), SDFDoc.e_remove_unused , null); // doc.Save((output_path + "newsletter_edited.pdf").c_str(), Doc::e_linearized , 0); System.out.println("Done. Result saved in newsletter_edited.pdf..."); } catch(Exception e) { e.printStackTrace(); } PDFNet.terminate(); } }