You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2016/05/07 12:09:59 UTC

svn commit: r1742703 - /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java

Author: tilman
Date: Sat May  7 12:09:59 2016
New Revision: 1742703

URL: http://svn.apache.org/viewvc?rev=1742703&view=rev
Log:
PDFBOX-3342: jump to a local page

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java?rev=1742703&r1=1742702&r2=1742703&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java Sat May  7 12:09:59 2016
@@ -26,6 +26,7 @@ import org.apache.pdfbox.pdmodel.font.PD
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
 import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
+import org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo;
 import org.apache.pdfbox.pdmodel.interactive.action.PDActionURI;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLine;
@@ -33,6 +34,8 @@ import org.apache.pdfbox.pdmodel.interac
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitWidthDestination;
 
 /**
  * Add annotations to pages of a PDF document.
@@ -56,9 +59,13 @@ public final class AddAnnotations
         PDDocument document = new PDDocument();
         try
         {
-            PDPage page = new PDPage();
-            document.addPage(page);
-            List<PDAnnotation> annotations = page.getAnnotations();
+            PDPage page1 = new PDPage();
+            PDPage page2 = new PDPage();
+            PDPage page3 = new PDPage();
+            document.addPage(page1);
+            document.addPage(page2);
+            document.addPage(page3);
+            List<PDAnnotation> annotations = page1.getAnnotations();
 
             // Some basic reusable objects/constants
             // Annotations themselves can only be used once!
@@ -76,18 +83,20 @@ public final class AddAnnotations
             borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
             borderULine.setWidth(INCH / 72); // 1 point
             
-            float pw = page.getMediaBox().getUpperRightX();
-            float ph = page.getMediaBox().getUpperRightY();
+            float pw = page1.getMediaBox().getUpperRightX();
+            float ph = page1.getMediaBox().getUpperRightY();
             
             // First add some text, two lines we'll add some annotations to this later
             PDFont font = PDType1Font.HELVETICA_BOLD;
-            PDPageContentStream contents = new PDPageContentStream(document, page);
+            PDPageContentStream contents = new PDPageContentStream(document, page1);
             contents.beginText();
             contents.setFont(font, 18);
             contents.newLineAtOffset(INCH, ph - INCH - 18);
             contents.showText("PDFBox");
             contents.newLineAtOffset(0, -(INCH / 2));
-            contents.showText("Click Here");
+            contents.showText("External URL");
+            contents.newLineAtOffset(0, -(INCH / 2));
+            contents.showText("Jump to page three");
             contents.endText();
             contents.close();
 
@@ -102,7 +111,7 @@ public final class AddAnnotations
             PDRectangle position = new PDRectangle();
             position.setLowerLeftX(INCH);
             position.setLowerLeftY(ph - INCH - 18);
-            position.setUpperRightX(72 + textWidth);
+            position.setUpperRightX(INCH + textWidth);
             position.setUpperRightY(ph - INCH);
             txtMark.setRectangle(position);
 
@@ -124,16 +133,16 @@ public final class AddAnnotations
             txtMark.setContents("Highlighted since it's important");
             annotations.add(txtMark);
 
-            // Now add the link annotation, so the clickme works
+            // Now add the link annotation, so the click on "External URL" works
             PDAnnotationLink txtLink = new PDAnnotationLink();
             txtLink.setBorderStyle(borderULine);
 
             // Set the rectangle containing the link
-            textWidth = font.getStringWidth("Click Here") / 1000 * 18;
+            textWidth = font.getStringWidth("External URL") / 1000 * 18;
             position = new PDRectangle();
             position.setLowerLeftX(INCH);
             position.setLowerLeftY(ph - 1.5f * INCH -20);  // down a couple of points
-            position.setUpperRightX(72 + textWidth);
+            position.setUpperRightX(INCH + textWidth);
             position.setUpperRightY(ph - 1.5f * INCH);
             txtLink.setRectangle(position);
 
@@ -168,7 +177,7 @@ public final class AddAnnotations
             aSquare.setColor(red);  // Outline in red, not setting a fill
             aSquare.setBorderStyle(borderThick);
 
-            // Place the annotation on the page, we'll make this 1" (72points) square
+            // Place the annotation on the page, we'll make this 1" (72 points) square
             // 3.5" down, 1" in from the right on the page
             position = new PDRectangle(); // Reuse the variable, but note it's a new object!
             position.setLowerLeftX(pw - 2 * INCH);  // 1" in from right, 1" wide
@@ -203,7 +212,36 @@ public final class AddAnnotations
             aLine.setBorderStyle(borderThick);
             aLine.setColor(black);
             annotations.add(aLine);
+            
+            
+            // Now add the link annotation, so the click on "Jump to page three" works
+            PDAnnotationLink pageLink = new PDAnnotationLink();
+            pageLink.setBorderStyle(borderULine);
 
+            // Set the rectangle containing the link
+            textWidth = font.getStringWidth("Jump to page three") / 1000 * 18;
+            position = new PDRectangle();
+            position.setLowerLeftX(INCH);
+            position.setLowerLeftY(ph - 2 * INCH - 20);  // down a couple of points
+            position.setUpperRightX(INCH + textWidth);
+            position.setUpperRightY(ph - 2 * INCH);
+            pageLink.setRectangle(position);
+
+            // add the GoTo action
+            PDActionGoTo actionGoto = new PDActionGoTo();
+            // see javadoc for other types of PDPageDestination
+            PDPageDestination dest = new PDPageFitWidthDestination();
+            // do not use setPageNumber(), this is for external destinations only
+            dest.setPage(page3);
+            actionGoto.setDestination(dest);
+            pageLink.setAction(actionGoto);
+            annotations.add(pageLink);      
+            
+
+            showPageNo(document, page1, "Page 1");
+            showPageNo(document, page2, "Page 2");
+            showPageNo(document, page3, "Page 3");
+            
             // save the PDF
             document.save(args[0]);
         }
@@ -212,4 +250,23 @@ public final class AddAnnotations
             document.close();
         }
     }
+
+    private static void showPageNo(PDDocument document, PDPage page, String pageText)
+            throws IOException
+    {
+        int fontSize = 10;
+
+        PDPageContentStream contents = 
+                new PDPageContentStream(document, page, PDPageContentStream.AppendMode.PREPEND, true);
+        float pageWidth = page.getMediaBox().getWidth();
+        float pageHeight = page.getMediaBox().getHeight();
+        PDFont font = PDType1Font.HELVETICA;
+        contents.setFont(font, fontSize);
+        float textWidth = font.getStringWidth(pageText) / 1000 * fontSize;
+        contents.beginText();
+        contents.newLineAtOffset(pageWidth / 2 - textWidth / 2, pageHeight - INCH / 2);
+        contents.showText(pageText);
+        contents.endText();
+        contents.close();
+    }
 }