You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2015/01/23 20:39:46 UTC
svn commit: r1654343 - in
/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel:
AddAnnotations.java Annotation.java
Author: jahewson
Date: Fri Jan 23 19:39:46 2015
New Revision: 1654343
URL: http://svn.apache.org/r1654343
Log:
PDFBOX-2589: Clean up Annotation example
Added:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java
- copied, changed from r1654284, pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java
Removed:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java
Copied: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java (from r1654284, pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java)
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java?p2=pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java&p1=pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java&r1=1654284&r2=1654343&rev=1654343&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/Annotation.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddAnnotations.java Fri Jan 23 19:39:46 2015
@@ -16,247 +16,195 @@
*/
package org.apache.pdfbox.examples.pdmodel;
+import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
-import org.apache.pdfbox.pdmodel.graphics.color.PDGamma;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionURI;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLine;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquareCircle;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationTextMarkup;
-import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary;
-
-import java.util.List;
-
/**
- * This is an example on how to add annotations to pages of a PDF document.
- *
- * @author Paul King
- * @version $Revision: 1.2 $
+ * Add annotations to pages of a PDF document.
*/
-public class Annotation
+public class AddAnnotations
{
- private Annotation()
+ static final float INCH = 72;
+
+ public static void main(String[] args) throws Exception
{
- //utility class, should not be instantiated.
- }
-
- /**
- * This will create a document showing various annotations.
- *
- * @param args The command line arguments.
- *
- * @throws Exception If there is an error parsing the document.
- */
- public static void main( String[] args ) throws Exception
- {
- if( args.length != 1 )
+ if (args.length != 1)
{
- usage();
+ System.err.println("Usage: " + AddAnnotations.class.getName() + " <output-pdf>");
+ System.exit(1);
}
- else
+
+ PDDocument document = new PDDocument();
+ try
{
- PDDocument document = new PDDocument();
+ PDPage page = new PDPage();
+ document.addPage(page);
+ List<PDAnnotation> annotations = page.getAnnotations();
+
+ // Some basic reusable objects/constants
+ // Annotations themselves can only be used once!
+ PDColor red = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE);
+ PDColor blue = new PDColor(new float[] { 0, 0, 1 }, PDDeviceRGB.INSTANCE);
+ PDColor black = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE);
+
+ PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
+ borderThick.setWidth(INCH / 12); // 12th inch
+
+ PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary();
+ borderThin.setWidth(INCH / 72); // 1 point
+
+ PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
+ borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
+ borderULine.setWidth(INCH / 72); // 1 point
+
+ float pw = page.getMediaBox().getUpperRightX();
+ float ph = page.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);
+ 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.endText();
+ contents.close();
+
+ // Now add the markup annotation, a highlight to PDFBox text
+ PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(
+ PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
+ txtMark.setColor(blue);
+ txtMark.setConstantOpacity((float)0.2); // 20% transparent
+
+ // Set the rectangle containing the markup
+ float textWidth = font.getStringWidth("PDFBox") / 1000 * 18;
+ PDRectangle position = new PDRectangle();
+ position.setLowerLeftX(INCH);
+ position.setLowerLeftY(ph - INCH - 18);
+ position.setUpperRightX(72 + textWidth);
+ position.setUpperRightY(ph - INCH);
+ txtMark.setRectangle(position);
+
+ // work out the points forming the four corners of the annotations
+ // set out in anti clockwise form (Completely wraps the text)
+ // OK, the below doesn't match that description.
+ // It's what acrobat 7 does and displays properly!
+ float[] quads = new float[8];
+ quads[0] = position.getLowerLeftX(); // x1
+ quads[1] = position.getUpperRightY()-2; // y1
+ quads[2] = position.getUpperRightX(); // x2
+ quads[3] = quads[1]; // y2
+ quads[4] = quads[0]; // x3
+ quads[5] = position.getLowerLeftY()-2; // y3
+ quads[6] = quads[2]; // x4
+ quads[7] = quads[5]; // y5
+
+ txtMark.setQuadPoints(quads);
+ txtMark.setContents("Highlighted since it's important");
+ annotations.add(txtMark);
+
+ // Now add the link annotation, so the clickme works
+ PDAnnotationLink txtLink = new PDAnnotationLink();
+ txtLink.setBorderStyle(borderULine);
+
+ // Set the rectangle containing the link
+ textWidth = font.getStringWidth("Click Here") / 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.setUpperRightY(ph - 1.5f * INCH);
+ txtLink.setRectangle(position);
+
+ // add an action
+ PDActionURI action = new PDActionURI();
+ action.setURI("http://www.pdfbox.org");
+ txtLink.setAction(action);
+ annotations.add(txtLink);
+
+ // Now draw a few more annotations
+ PDAnnotationSquareCircle aCircle = new PDAnnotationSquareCircle(
+ PDAnnotationSquareCircle.SUB_TYPE_CIRCLE);
+ aCircle.setContents("Circle Annotation");
+ aCircle.setInteriorColor(red); // Fill in circle in red
+ aCircle.setColor(blue); // The border itself will be blue
+ aCircle.setBorderStyle(borderThin);
+
+ // Place the annotation on the page, we'll make this 1" round
+ // 3" down, 1" in on the page
+ position = new PDRectangle();
+ position.setLowerLeftX(INCH);
+ position.setLowerLeftY(ph - 3 * INCH -INCH); // 1" height, 3" down
+ position.setUpperRightX(2 * INCH); // 1" in, 1" width
+ position.setUpperRightY(ph - 3*INCH); // 3" down
+ aCircle.setRectangle(position);
+ annotations.add(aCircle);
+
+ // Now a square annotation
+ PDAnnotationSquareCircle aSquare = new PDAnnotationSquareCircle(
+ PDAnnotationSquareCircle.SUB_TYPE_SQUARE);
+ aSquare.setContents("Square Annotation");
+ 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
+ // 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
+ position.setLowerLeftY(ph - 3.5f * INCH - INCH); // 1" height, 3.5" down
+ position.setUpperRightX(pw - INCH); // 1" in from right
+ position.setUpperRightY(ph - 3.5f * INCH); // 3.5" down
+ aSquare.setRectangle(position);
+ annotations.add(aSquare);
+
+ // Now we want to draw a line between the two, one end with an open arrow
+ PDAnnotationLine aLine = new PDAnnotationLine();
+ aLine.setEndPointEndingStyle(PDAnnotationLine.LE_OPEN_ARROW);
+ aLine.setContents("Circle->Square");
+ aLine.setCaption(true); // Make the contents a caption on the line
+
+ // Set the rectangle containing the line
+ position = new PDRectangle(); // Reuse the variable, but note it's a new object!
+ position.setLowerLeftX(2 * INCH); // 1" in + width of circle
+ position.setLowerLeftY(ph - 3.5f * INCH - INCH); // 1" height, 3.5" down
+ position.setUpperRightX(pw - INCH-INCH); // 1" in from right, and width of square
+ position.setUpperRightY(ph - 3 * INCH); // 3" down (top of circle)
+ aLine.setRectangle(position);
+
+ // Now set the line position itself
+ float[] linepos = new float[4];
+ linepos[0] = 2 * INCH; // x1 = rhs of circle
+ linepos[1] = ph - 3.5f * INCH; // y1 halfway down circle
+ linepos[2] = pw- 2 * INCH; // x2 = lhs of square
+ linepos[3] = ph- 4 * INCH; // y2 halfway down square
+ aLine.setLine(linepos);
+
+ aLine.setBorderStyle(borderThick);
+ aLine.setColor(black);
+ annotations.add(aLine);
- try
- {
- PDPage page = new PDPage();
- document.addPage(page);
- List annotations = page.getAnnotations();
-
- // Setup some basic reusable objects/constants
- // Annotations themselves can only be used once!
-
- float inch = 72;
- PDGamma colourRed = new PDGamma();
- colourRed.setR(1);
- PDGamma colourBlue = new PDGamma();
- colourBlue.setB(1);
- PDGamma colourBlack = new PDGamma();
-
- PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
- borderThick.setWidth(inch/12); // 12th inch
- PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary();
- borderThin.setWidth(inch/72); // 1 point
- PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
- borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
- borderULine.setWidth(inch/72); // 1 point
-
-
- float pw = page.getMediaBox().getUpperRightX();
- float ph = page.getMediaBox().getUpperRightY();
-
-
- // First add some text, two lines we'll add some annotations to this later
-
-
- PDFont font = PDType1Font.HELVETICA_BOLD;
-
- PDPageContentStream contentStream = new PDPageContentStream(document, page);
- contentStream.beginText();
- contentStream.setFont( font, 18 );
- contentStream.newLineAtOffset(inch, ph - inch - 18);
- contentStream.showText("PDFBox");
- contentStream.newLineAtOffset(0, -(inch / 2));
- contentStream.showText("Click Here");
- contentStream.endText();
-
- contentStream.close();
-
- // Now add the markup annotation, a highlight to PDFBox text
- PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT);
- txtMark.setColour(colourBlue);
- txtMark.setConstantOpacity((float)0.2); // Make the highlight 20% transparent
-
- // Set the rectangle containing the markup
-
- float textWidth = (font.getStringWidth( "PDFBox" )/1000) * 18;
- PDRectangle position = new PDRectangle();
- position.setLowerLeftX(inch);
- position.setLowerLeftY( ph-inch-18 );
- position.setUpperRightX(72 + textWidth);
- position.setUpperRightY(ph-inch);
- txtMark.setRectangle(position);
-
- // work out the points forming the four corners of the annotations
- // set out in anti clockwise form (Completely wraps the text)
- // OK, the below doesn't match that description.
- // It's what acrobat 7 does and displays properly!
- float[] quads = new float[8];
-
- quads[0] = position.getLowerLeftX(); // x1
- quads[1] = position.getUpperRightY()-2; // y1
- quads[2] = position.getUpperRightX(); // x2
- quads[3] = quads[1]; // y2
- quads[4] = quads[0]; // x3
- quads[5] = position.getLowerLeftY()-2; // y3
- quads[6] = quads[2]; // x4
- quads[7] = quads[5]; // y5
-
- txtMark.setQuadPoints(quads);
- txtMark.setContents("Highlighted since it's important");
-
- annotations.add(txtMark);
-
- // Now add the link annotation, so the clickme works
- PDAnnotationLink txtLink = new PDAnnotationLink();
- txtLink.setBorderStyle(borderULine);
-
- // Set the rectangle containing the link
-
- textWidth = (font.getStringWidth( "Click Here" )/1000) * 18;
- position = new PDRectangle();
- position.setLowerLeftX(inch);
- position.setLowerLeftY( ph-(float)(1.5*inch)-20); // down a couple of points
- position.setUpperRightX(72 + textWidth);
- position.setUpperRightY(ph-(float)(1.5*inch));
- txtLink.setRectangle(position);
-
- // add an action
- PDActionURI action = new PDActionURI();
- action.setURI("http://www.pdfbox.org");
- txtLink.setAction(action);
-
- annotations.add(txtLink);
-
-
- // Now draw a few more annotations
-
- PDAnnotationSquareCircle aCircle =
- new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_CIRCLE);
- aCircle.setContents("Circle Annotation");
- aCircle.setInteriorColour(colourRed); // Fill in circle in red
- aCircle.setColour(colourBlue); // The border itself will be blue
- aCircle.setBorderStyle(borderThin);
-
- // Place the annotation on the page, we'll make this 1" round
- // 3" down, 1" in on the page
-
- position = new PDRectangle();
- position.setLowerLeftX(inch);
- position.setLowerLeftY(ph-(3*inch)-inch); // 1" height, 3" down
- position.setUpperRightX(2*inch); // 1" in, 1" width
- position.setUpperRightY(ph-(3*inch)); // 3" down
- aCircle.setRectangle(position);
-
- // add to the annotations on the page
- annotations.add(aCircle);
-
- // Now a square annotation
-
- PDAnnotationSquareCircle aSquare =
- new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_SQUARE);
- aSquare.setContents("Square Annotation");
- aSquare.setColour(colourRed); // Outline in red, not setting a fill
- aSquare.setBorderStyle(borderThick);
-
- // Place the annotation on the page, we'll make this 1" (72points) 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
- position.setLowerLeftY(ph-(float)(3.5*inch) - inch); // 1" height, 3.5" down
- position.setUpperRightX(pw-inch); // 1" in from right
- position.setUpperRightY(ph-(float)(3.5*inch)); // 3.5" down
- aSquare.setRectangle(position);
-
- // add to the annotations on the page
- annotations.add(aSquare);
-
- // Now we want to draw a line between the two, one end with an open arrow
-
- PDAnnotationLine aLine = new PDAnnotationLine();
-
- aLine.setEndPointEndingStyle( PDAnnotationLine.LE_OPEN_ARROW );
- aLine.setContents("Circle->Square");
- aLine.setCaption(true); // Make the contents a caption on the line
-
- // Set the rectangle containing the line
-
- position = new PDRectangle(); // Reuse the variable, but note it's a new object!
- position.setLowerLeftX(2*inch); // 1" in + width of circle
- position.setLowerLeftY(ph-(float)(3.5*inch)-inch); // 1" height, 3.5" down
- position.setUpperRightX(pw-inch-inch); // 1" in from right, and width of square
- position.setUpperRightY(ph-(3*inch)); // 3" down (top of circle)
- aLine.setRectangle(position);
-
- // Now set the line position itself
- float[] linepos = new float[4];
- linepos[0] = 2*inch; // x1 = rhs of circle
- linepos[1] = ph-(float)(3.5*inch); // y1 halfway down circle
- linepos[2] = pw-(2*inch); // x2 = lhs of square
- linepos[3] = ph-(4*inch); // y2 halfway down square
- aLine.setLine(linepos);
-
- aLine.setBorderStyle(borderThick);
- aLine.setColour(colourBlack);
-
- // add to the annotations on the page
- annotations.add(aLine);
-
-
- // Finally all done
-
-
- document.save( args[0] );
- }
- finally
- {
- document.close();
- }
+ // save the PDF
+ document.save(args[0]);
+ }
+ finally
+ {
+ document.close();
}
- }
-
- /**
- * This will print the usage for this document.
- */
- private static void usage()
- {
- System.err.println( "Usage: java org.apache.pdfbox.examples.pdmodel.Annotation <output-pdf>" );
}
}