//--------------------------------------------------------------------------------------- // 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.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 processElements(ElementWriter writer, ElementReader reader) { Element element; try { while ((element = reader.next()) != null) { switch (element.getType()) { case Element.e_path: break; case Element.e_image: case Element.e_inline_image: continue; case Element.e_text: GState gs = element.getGState(); gs.setFillColorSpace(ColorSpace.createDeviceRGB()); gs.setFillColor(new ColorPt(0, 0, 1)); break; case Element.e_form: reader.formBegin(); processElements(writer, reader); reader.end(); break; } writer.writeElement(element); } } catch(Exception e) { e.printStackTrace(); } } 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/"; String input_filename = "newsletter.pdf"; String output_filename = "newsletter_edited.pdf"; try { PDFDoc doc=new PDFDoc((input_path + input_filename)); doc.initSecurityHandler(); ElementWriter writer=new ElementWriter(); ElementReader reader=new ElementReader(); Element element; PageIterator itr = doc.getPageIterator(); while (itr.hasNext()) { Page page = (Page)itr.next(); reader.begin(page); writer.begin(page, ElementWriter.e_replacement, false); ElementEditTest.processElements(writer, reader); writer.end(); reader.end(); } doc.save((output_path + output_filename), SDFDoc.e_remove_unused , null); doc.close(); // doc.Save((output_path + "newsletter_edited.pdf").c_str(), Doc::e_linearized , 0); System.out.println("Done. Result saved in " + output_filename +"..."); } catch(Exception e) { e.printStackTrace(); } PDFNet.terminate(); } }