The PDF Manipulator’s addAnnotation() method accepts instances of the classes in org.jpedal.annotation. FreeText and Link are covered with worked examples in the PDF Manipulator tutorial. This page covers the constructors for the remaining annotation types, plus the flags, caption, line ending, and icon values they accept.
Each annotation type has several constructors of increasing detail. The examples below use the simplest constructor for each type. The more detailed constructors also let you set:
- Annotation flags - see Annotation flags below.
- Transparency - separate
strokingAlpha(line/border transparency) andnonStrokingAlpha(fill transparency) values, from0.0(fully transparent) to1.0(fully opaque). - A title bar - the
titletext shown in the annotation’s pop-up window (commonly used for the author’s name).
See the Annotation Javadoc for the full constructor list of each type.
Features
- PDF Annotation Types in Java
- Annotation flags
- Caret
- Circle
- Highlight
- Ink
- Line
- PolyLine
- Polygon
- Square
- Squiggly
- Stamp
- StrikeOut
- Text
- Underline
Annotation flags
Several constructors accept an int flags parameter, a bitmask of the PDF annotation flags described in the PDF specification. Use the static helper Annotation.getFlagsValue() to build this value from booleans rather than calculating the bitmask yourself:
final int flags = Annotation.getFlagsValue(
false, // invisible - don't render an unknown annotation type, and don't print it even if Print is set
false, // hidden - don't render or allow interaction with the annotation at all
true, // print - print the annotation when the page is printed, unless Hidden is also set
false, // noZoom - don't scale the annotation's appearance to match the page's zoom level
false, // noRotate - don't rotate the annotation's appearance to match the page's rotation
false, // noView - don't render on screen or allow interaction, but still print it
false, // readOnly - don't allow the user to interact with the annotation
false, // locked - don't allow the annotation to be deleted, moved, or resized by the user
false, // toggleNoView - invert the NoView flag while the mouse hovers over, or the annotation is selected
false // lockedContents - don't allow the annotation's contents to be modified by the user
);
If you do not need to set flags, use one of the shorter constructors instead which omit the
flagsparameter.
Caret
Marks a specific location in the text, most often used to mark a point where text has been inserted.
final Caret caret = new Caret(new float[] {X1, Y1, X2, Y2}, new float[] {0.0f, 0.0f, 0.0f});
Circle
An ellipse inscribed within the annotation’s bounding box.
final Circle circle = new Circle(new float[] {X1, Y1, X2, Y2},
new float[] {0.0f, 0.0f, 0.0f}, // lineColor
new float[] {1.0f, 1.0f, 0.0f}, // fillColor
1.0f, // strokingAlpha
1.0f, // nonStrokingAlpha
1.0f); // lineWidth
Highlight
Highlights one or more ranges of text, most commonly rendered by making the highlighted text appear as if marked with a highlighter pen.
quadPoints is an array of 8 x n numbers specifying the coordinates of n quadrilaterals in default user space. Each quadrilateral encompasses a word or group of contiguous words in the text underlying the annotation. The coordinates for each quadrilateral are given in the order x1 y1 x2 y2 x3 y3 x4 y4, specifying the quadrilateral’s four vertices in counterclockwise order.
final Highlight highlight = new Highlight(new float[] {X1, Y1, X2, Y2},
new float[] {1.0f, 1.0f, 0.0f}, // color
new float[] {X1, Y1, X2, Y1, X1, Y2, X2, Y2}); // quadPoints
Ink
A freehand “scribble” composed of one or more disjoint paths, most commonly used to represent freehand annotations made with a pointing device.
inkList is an array of n arrays, each representing a stroked path. Each inner array is a series of alternating horizontal and vertical coordinates in default user space, specifying points along the path.
final Ink ink = new Ink(new float[] {X1, Y1, X2, Y2},
new float[] {0.0f, 0.0f, 0.0f}, // color
new float[][] , // inkList, one path with three points
1.0f); // lineWidth
Line
A single straight line, optionally with a caption and different styles of line ending.
line is an array of four numbers, [x1 y1 x2 y2], specifying the starting and ending coordinates of the line in default user space.
final Line line = new Line(new float[] {X1, Y1, X2, Y2},
"A caption", // contents, or null
new float[] {0.0f, 0.0f, 0.0f}, // color
new float[] {X1, Y1, X2, Y2}); // line
Line also has a constructor which lets you set different ending styles for the start and end of the line, and where the caption is positioned, using the LineEndingStyle and CaptionPosition enums:
final Line arrowLine = new Line(new float[] {X1, Y1, X2, Y2},
"A caption", // contents, or null
new float[] {0.0f, 0.0f, 0.0f}, // color
new float[] {X1, Y1, X2, Y2}, // line
LineEndingStyle.NONE, // startStyle
LineEndingStyle.OPEN_ARROW, // endStyle
new float[] {0.0f, 0.0f, 0.0f}, // endingColor
CaptionPosition.TOP); // captionPosition
LineEndingStyle accepts the following values:
| Value | Description |
|---|---|
SQUARE | A square line ending. |
CIRCLE | A circular line ending. |
DIAMOND | A diamond-shaped line ending. |
OPEN_ARROW | An open arrowhead line ending. |
CLOSED_ARROW | A closed (filled) arrowhead line ending. |
NONE | No line ending. |
BUTT | A flat cap at the end of the line (also known as a “butt cap”). |
REVERSE_OPEN_ARROW | A reversed open arrowhead line ending (the arrow points toward the start of the line). |
REVERSE_CLOSED_ARROW | A reversed closed (filled) arrowhead line ending. |
SLASH | A short diagonal line resembling a slash at the end of the line. |
CaptionPosition accepts the following values:
| Value | Description |
|---|---|
INLINE | The caption is centred inside the line. |
TOP | The caption is on top of the line. |
The most detailed Line constructor also lets you set leaderLength (the length of the leader lines extending from each endpoint) and leaderExtensionLength (a non-negative length for leader line extensions that continue 180 degrees beyond the leader lines).
PolyLine
A series of connected straight line segments, unlike Polygon the first and last vertex are not connected.
vertices is an array of numbers specifying the alternating horizontal and vertical coordinates of each vertex.
final PolyLine polyLine = new PolyLine(new float[] {X1, Y1, X2, Y2},
new float[] {0.0f, 0.0f, 0.0f}, // lineColor
new float[] {1.0f, 1.0f, 0.0f}, // fillColor
new float[] {X1, Y1, X2, Y2, X3, Y3}, // vertices
1.0f); // lineWidth
PolyLine also has a constructor which lets you set different ending styles for the start and end of the line, using the LineEndingStyle enum described above:
final PolyLine arrowPolyLine = new PolyLine(new float[] {X1, Y1, X2, Y2},
new float[] {0.0f, 0.0f, 0.0f}, // lineColor
new float[] {1.0f, 1.0f, 0.0f}, // fillColor
new float[] {X1, Y1, X2, Y2, X3, Y3}, // vertices
LineEndingStyle.NONE, // startStyle
LineEndingStyle.OPEN_ARROW, // endStyle
1.0f); // lineWidth
Polygon
A series of connected straight line segments which form a closed shape.
vertices is an array of numbers specifying the alternating horizontal and vertical coordinates of each vertex.
final Polygon polygon = new Polygon(new float[] {X1, Y1, X2, Y2},
new float[] {0.0f, 0.0f, 0.0f}, // lineColor
new float[] {1.0f, 1.0f, 0.0f}, // fillColor
new float[] {X1, Y1, X2, Y2, X3, Y3}, // vertices
1.0f); // lineWidth
Square
A rectangle inscribed within the annotation’s bounding box.
final Square square = new Square(new float[] {X1, Y1, X2, Y2},
new float[] {0.0f, 0.0f, 0.0f}, // lineColor
new float[] {1.0f, 1.0f, 0.0f}, // fillColor
1.0f, // strokingAlpha
1.0f, // nonStrokingAlpha
1.0f); // lineWidth
Squiggly
A wavy underline beneath one or more ranges of text.
quadPoints follows the same format described under Highlight above.
final Squiggly squiggly = new Squiggly(new float[] {X1, Y1, X2, Y2},
new float[] {1.0f, 0.0f, 0.0f}, // color
new float[] {X1, Y1, X2, Y1, X1, Y2, X2, Y2}); // quadPoints
Stamp
Displays a named rubber stamp icon, or an image or text if you set a custom appearance stream (see Add an annotation).
icon must be one of the values from AnnotationIcons:
final Stamp stamp = new Stamp(new float[] {X1, Y1, X2, Y2},
new float[] {1.0f, 0.0f, 0.0f}, // color
AnnotationIcons.APPROVED); // icon
AnnotationIcons provides the following rubber stamp icon names: APPROVED, EXPERIMENTAL, NOT_APPROVED, AS_IS, EXPIRED, NOT_FOR_PUBLIC_RELEASE, CONFIDENTIAL, FINAL, SOLD, DEPARTMENTAL, FOR_COMMENT, TOP_SECRET, DRAFT, and FOR_PUBLIC_RELEASE.
AnnotationIcons also provides icon names for the Text sticky note icon (COMMENT, KEY, NOTE, HELP, NEW_PARAGRAPH, PARAGRAPH, INSERT), and for the FileAttachment icon used by Attach a file (GRAPH, PAPER_CLIP, PUSH_PIN, TAG), and a “Sound icon” pair (SPEAKER, MIC) documented against a Sound annotation type, which is not currently one of the types accepted by addAnnotation().
StrikeOut
Strikes a line through one or more ranges of text.
quadPoints follows the same format described under Highlight above.
final StrikeOut strikeOut = new StrikeOut(new float[] {X1, Y1, X2, Y2},
new float[] {1.0f, 0.0f, 0.0f}, // color
new float[] {X1, Y1, X2, Y1, X1, Y2, X2, Y2}); // quadPoints
Text
A “sticky note” icon which displays a pop-up window containing text when opened.
icon must be one of the sticky note values from AnnotationIcons, listed under Stamp above.
final Text text = new Text(new float[] {X1, Y1, X2, Y2},
"Some notes", // contents, or null
new float[] {1.0f, 1.0f, 0.0f}, // color
AnnotationIcons.COMMENT); // icon
There is also a constructor which lets you set open, whether the pop-up window should initially be displayed open:
final Text openText = new Text(new float[] {X1, Y1, X2, Y2},
"Some notes", // contents, or null
new float[] {1.0f, 1.0f, 0.0f}, // color
true, // open
AnnotationIcons.COMMENT); // icon
Underline
An underline beneath one or more ranges of text.
quadPoints follows the same format described under Highlight above.
final Underline underline = new Underline(new float[] {X1, Y1, X2, Y2},
new float[] {0.0f, 0.0f, 1.0f}, // color
new float[] {X1, Y1, X2, Y1, X1, Y2, X2, Y2}); // quadPoints