//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2012 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- import pdftron.Common.PDFNetException; import pdftron.PDF.*; import pdftron.PDF.Annot.BorderStyle; import pdftron.SDF.SDFDoc; import pdftron.SDF.Obj; public class AnnotationTest { // Relative path to the folder containing test files. public static final String input_path = "../../TestFiles/"; static void AnnotationHighLevelAPI(PDFDoc doc) throws PDFNetException { // The following code snippet traverses all annotations in the document System.out.println("Traversing all annotations in the document..."); int page_num=1; for (PageIterator itr = doc.getPageIterator(); itr.hasNext(); ) { System.out.println("Page " + (page_num++) + ": "); Page page = (Page)(itr.next()); int num_annots = page.getNumAnnots(); for (int i = 0; i < num_annots; ++i) { Annot annot = page.getAnnot(i); if (annot.isValid() == false) continue; System.out.println("Annot Type: " + annot.getSDFObj().get("Subtype").value().getName()); double[] bbox = annot.getRect().get(); System.out.println(" Position: " + ", " + bbox[0] + ", " + bbox[1] + ", " + bbox[2] + ", " + bbox[3]); switch (annot.getType()) { case Annot.e_Link: { pdftron.PDF.Annots.Link link = new pdftron.PDF.Annots.Link(annot); Action action = link.getAction(); if (action.isValid() == false) continue; if (action.getType() == Action.e_GoTo) { Destination dest = action.getDest(); if (dest.isValid() == false) { System.out.println(" Destination is not valid."); } else { int page_link = dest.getPage().getIndex(); System.out.println(" Links to: page number " + page_link + " in this document"); } } else if (action.getType() == Action.e_URI) { String uri = action.getSDFObj().get("URI").value().getAsPDFText(); System.out.println(" Links to: " + uri); } // ... } break; case Annot.e_Widget: break; case Annot.e_FileAttachment: break; // ... default: break; } } } // Use the high-level API to create new annotations. Page first_page = doc.getPage(1); // Create a hyperlink... pdftron.PDF.Annots.Link hyperlink = pdftron.PDF.Annots.Link.create(doc, new Rect(85, 570, 503, 524), Action.createURI(doc, "http://www.pdftron.com")); first_page.annotPushBack(hyperlink); // Create an intra-document link... Action goto_page_3 = Action.createGoto(Destination.createFitH((Page)(doc.getPage(3)), 0)); pdftron.PDF.Annots.Link link = pdftron.PDF.Annots.Link.create(doc.getSDFDoc(), new Rect(85, 458, 503, 502), Action.createURI(doc, "http://www.pdftron.com"));//goto_page_3 // Set the annotation border width to 3 points... BorderStyle border_style = new Annot.BorderStyle(Annot.BorderStyle.e_solid, 10, 0, 0); link.setBorderStyle(border_style); link.setColor(new ColorPt(1, 0, 0), 3); // Add the new annotation to the first page first_page.annotPushBack(link); // Create a stamp annotation ... pdftron.PDF.Annots.RubberStamp stamp = pdftron.PDF.Annots.RubberStamp.create(doc, new Rect(30, 30, 300, 200)); stamp.setIcon("Draft"); first_page.annotPushBack(stamp); // Create a file attachment annotation (embed the 'peppers.jpg'). pdftron.PDF.Annots.FileAttachment file_attach = pdftron.PDF.Annots.FileAttachment.create(doc, new Rect(80, 280, 200, 320), (input_path + "peppers.jpg")); first_page.annotPushBack(file_attach); pdftron.PDF.Annots.Circle circle = pdftron.PDF.Annots.Circle.create(doc, new Rect(10, 110, 100, 200)); circle.setInteriorColor(new ColorPt(0, 0.5, 1), 3); circle.setTitle("This is a title for the circle"); circle.setColor(new ColorPt(0, 1, 0), 3); circle.setInteriorColor(new ColorPt(0, 0, 1), 3); circle.setContentRect(new Rect(12, 112, 98, 198)); circle.setOpacity(0.5); first_page.annotPushBack(circle); pdftron.PDF.Annots.Ink ink = pdftron.PDF.Annots.Ink.create(doc, new Rect(110, 10, 300, 200)); Point pt3 = new Point(110, 10); //pt3.x = 110; pt3.y = 10; ink.setPoint(0, 0, pt3); pt3.x = 150; pt3.y = 50; ink.setPoint(0, 1, pt3); pt3.x = 190; pt3.y = 60; ink.setPoint(0, 2, pt3); pt3.x = 180; pt3.y = 90; ink.setPoint(1, 0, pt3); pt3.x = 190; pt3.y = 95; ink.setPoint(1, 1, pt3); pt3.x = 200; pt3.y = 100; ink.setPoint(1, 2, pt3); pt3.x = 166; pt3.y = 86; ink.setPoint(2, 0, pt3); pt3.x = 196; pt3.y = 96; ink.setPoint(2, 1, pt3); pt3.x = 221; pt3.y = 121; ink.setPoint(2, 2, pt3); pt3.x = 288; pt3.y = 188; ink.setPoint(2, 3, pt3); ink.setColor(new ColorPt(0, 1, 1), 3); first_page.annotPushBack(ink); // ... } static void AnnotationLowLevelAPI(PDFDoc doc) throws PDFNetException { Page page = (Page)(doc.getPageIterator().next()); Obj annots = page.getAnnots(); if (annots==null) { // If there are no annotations, create a new annotation // array for the page. annots = doc.createIndirectArray(); page.getSDFObj().put("Annots", annots); } // Create a Text annotation Obj annot = doc.createIndirectDict(); annot.putName("Subtype", "Text"); annot.putBool("Open", true); annot.putString("Contents", "The quick brown fox ate the lazy mouse."); annot.putRect("Rect", 266, 116, 430, 204); // Insert the annotation in the page annotation array annots.pushBack(annot); // Create a Link annotation Obj link1 = doc.createIndirectDict(); link1.putName("Subtype", "Link"); Destination dest = Destination.createFit((Page)(doc.getPage(2))); link1.put("Dest", dest.getSDFObj()); link1.putRect("Rect", 85, 705, 503, 661); annots.pushBack(link1); // Create another Link annotation Obj link2 = doc.createIndirectDict(); link2.putName("Subtype", "Link"); Destination dest2 = Destination.createFit((Page)(doc.getPage(3))); link2.put("Dest", dest2.getSDFObj()); link2.putRect("Rect", 85, 638, 503, 594); annots.pushBack(link2); // Note that PDFNet APi can be used to modify existing annotations. // In the following example we will modify the second link annotation // (link2) so that it points to the 10th page. We also use a different // destination page fit type. // link2 = annots.GetAt(annots.Size()-1); link2.put("Dest", Destination.createXYZ((Page)(doc.getPage(10)), 100, 792-70, 10).getSDFObj()); // Create a third link annotation with a hyperlink action (all other // annotation types can be created in a similar way) Obj link3 = doc.createIndirectDict(); link3.putName("Subtype", "Link"); link3.putRect("Rect", 85, 570, 503, 524); // Create a URI action Obj action = link3.putDict("A"); action.putName("S", "URI"); action.putString("URI", "http://www.pdftron.com"); annots.pushBack(link3); } public static void main(String[] args) { PDFNet.initialize(); String output_path = "../../TestFiles/Output/"; try { PDFDoc doc=new PDFDoc((input_path + "numbered.pdf")); doc.initSecurityHandler(); // An example of using SDF/Cos API to add any type of annotations. AnnotationLowLevelAPI(doc); doc.save((output_path + "annotation_test1.pdf"), SDFDoc.e_linearized, null); // An example of using the high-level PDFNet API to read existing annotations, // to edit existing annotations, and to create new annotation from scratch. AnnotationHighLevelAPI(doc); doc.save((output_path + "annotation_test2.pdf"), SDFDoc.e_linearized, null); doc.close(); System.out.println("Saved annotation_test.pdf\nDone. "); } catch(Exception e) { System.out.println(e); } PDFNet.terminate(); } }