Customize annotation style properties

AnnotEdit tool class can edit the style of a selected annotation easily by using annotation style dialog . You can set AnnotEdit tool to ToolManager as described here: change tool mode .

With Apryse library, you can change annotation styles such as border color, border thickness, fill color, etc. To change annotation style, you must lock PDF document first. For more information about locking document, see: document locking.

Changing an annotation border thickness

To change an annotation border thickness:

  1. Get the BorderStyle from the annotation by calling annot.getBorderStyle().
  2. Set thickness to the BorderSytle by calling borderStyle.setWidth(double).
  3. Refresh the annotation appearance by calling annot.refreshAppearance().
  4. Update the PDFViewCtrl contents by calling pdfViewCtrl.update().
1void changeAnnotThickness(final PDFViewCtrl pdfViewCtrl, final Annot annot, final double thickness) throws Exception {
2 // Locks the document first as accessing annotation/doc
3 // information isn't thread safe.
4 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
5 @Override
6 public void run() throws Exception {
7 Annot.BorderStyle borderStyle = annot.getBorderStyle();
8 borderStyle.setWidth(thickness);
9
10 annot.refreshAppearance();
11 pdfViewCtrl.update();
12 }
13 });
14}

To make annotation border invisible, call borderStyle.setWidth(0).

Changing an annotation border color.

If there is a FreeText annotation, to set the border color of the annotation, call freeText.setLineColor(ColorPt, int). To set the border color of the other annotation types, call annot.setColor(ColorPt, int).

1void changeAnnotBorderColor(final PDFViewCtrl pdfViewCtrl, final Annot annot, final @ColorInt int color) throws Exception {
2 // Locks the document first as accessing annotation/doc
3 // information isn't thread safe.
4 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
5 @Override
6 public void run() throws Exception {
7 ColorPt colorPt = Utils.color2ColorPt(color);
8 // if color is transparent, then color component number is 0.
9 int colorCompNum = color == Color.TRANSPARENT ? 0 : 3;
10 // if the annotation is a FreeText annotation
11 if (annot.getType() == Annot.e_FreeText) {
12 FreeText freeText = new FreeText(annot);
13 freeText.setLineColor(colorPt, colorCompNum);
14 } else {
15 annot.setColor(colorPt, colorCompNum);
16 }
17
18 annot.refreshAppearance();
19 pdfViewCtrl.update();
20 }
21 });
22}

To set the color of a Text annotation, call annot.setColor(ColorPt, int).

Changing an annotation opacity

To change the opacity of a Markup annotation, callMarkup.setOpacity(double):

1void changeAnnotOpacity(final PDFViewCtrl pdfViewCtrl, final Annot annot, final double opacity) throws Exception {
2 // Locks the document first as accessing annotation/doc
3 // information isn't thread safe.
4 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
5 @Override
6 public void run() throws Exception {
7 // if the annotation is a markup annotation,
8 // set opacity to the annotation
9 if (annot.isMarkup()) {
10 Markup markup = new Markup(annot);
11 markup.setOpacity(opacity);
12 }
13
14 annot.refreshAppearance();
15 pdfViewCtrl.update();
16 }
17 });
18}

Changing an annotation fill color

To set the fill color of a Markup annotation, call Markup.setInteriorColor(ColorPt, int).

1void changeAnnotInteriorColor(final PDFViewCtrl pdfViewCtrl, final Annot annot, @ColorInt final int color) throws Exception {
2 // Locks the document first as accessing annotation/doc
3 // information isn't thread safe.
4 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
5 @Override
6 public void run() throws Exception {
7 ColorPt colorPt = Utils.color2ColorPt(color);
8 // if color is transparent, then color component number is 0.
9 int colorCompNum = color == Color.TRANSPARENT ? 0 : 3;
10 // if the annotation is a Markup annotation,
11 // set interior color to the annotation.
12 if (annot.isMarkup()) {
13 Markup markup = new Markup(annot);
14 markup.setInteriorColor(colorPt, colorCompNum);
15 }
16 // refresh the annotation appearch
17 annot.refreshAppearance();
18 pdfViewCtrl.update();
19 }
20 });
21}

Changing a Text annotation icon

To change a Text annotation icon, call Text.setIcon(String). After changing the icon, you must refresh the annotation appearance by calling AnnotUtils.refreshStickyNoteAppearance(Annot, PDFViewCtrl).

For available icons, see: default icons table .

1void changeAnnotInteriorColor(final PDFViewCtrl pdfViewCtrl, final Annot annot, final String icon) throws Exception {
2 // Locks the document first as accessing annotation/doc
3 // information isn't thread safe.
4 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
5 @Override
6 public void run() throws Exception {
7 Text text = new Text(annot);
8 text.setIcon(icon);
9 // refresh the annotation appearch
10 AnnotUtils.refreshStickyNoteAppearance(annot, pdfViewCtrl);
11 pdfViewCtrl.update();
12 }
13 });
14}

Changing a FreeText annotation text color

To change the text color of a FreeText annotation, call FreeText.setTextColor(ColorPt, int).

1void changeFreeTextColor(final PDFViewCtrl pdfViewCtrl, final Annot annot, @ColorInt final int color) throws Exception {
2
3 // Locks the document first as accessing annotation/doc
4 // information isn't thread safe.
5 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
6 @Override
7 public void run() throws Exception {
8 ColorPt colorPt = Utils.color2ColorPt(color);
9 // set FreeText annotation text color
10 FreeText freeText = new FreeText(annot);
11 freeText.setTextColor(colorPt, 3);
12
13 annot.refreshAppearance();
14 pdfViewCtrl.update();
15 }
16 });
17}

Changing a FreeText annotation text size

To change the text size of a FreeText annotation, call FreeText.setFontSize(double).

1void changeFreeTextSize(final PDFViewCtrl pdfViewCtrl, final Annot annot, final double textSize) throws Exception {
2 // Locks the document first as accessing annotation/doc
3 // information isn't thread safe.
4 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
5 @Override
6 public void run() throws Exception {
7 // set FreeText annotation text color
8 FreeText freeText = new FreeText(annot);
9 freeText.setFontSize(textSize);
10 // refresh the annotation appearance
11 annot.refreshAppearance();
12 pdfViewCtrl.update();
13 }
14 });
15}

Changing a FreeText annotation font

The follow code snippet shows how to set the font to a FreeText annotation:

1void changeFreeTextFont(final PDFViewCtrl pdfViewCtrl, final Annot annot, final String fontStr) throws Exception {
2 // Locks the document first as accessing annotation/doc
3 // information isn't thread safe.
4 pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
5 @Override
6 public void run() throws Exception {
7 // set FreeText annotation text color
8 FreeText textAnnot = new FreeText(annot);
9 String fontDRName = "F0";
10
11 // Create a DR entry for embedding the font
12 Obj annotObj = textAnnot.getSDFObj();
13 Obj drDict = annotObj.putDict("DR");
14 Obj fontDict = drDict.putDict("Font");
15
16 // Embed the font
17 Font font = Font.create(pdfViewCtrl.getDoc(), fontStr, textAnnot.getContents());
18 fontDict.put(fontDRName, font.GetSDFObj());
19 String fontName = font.getName();
20
21 // Set DA string
22 String DA = textAnnot.getDefaultAppearance();
23 int slashPosition = DA.indexOf("/", 0);
24
25 // if DR string contains '/' which it always should.
26 if (slashPosition > 0) {
27 String beforeSlash = DA.substring(0, slashPosition);
28 String afterSlash = DA.substring(slashPosition);
29 String afterFont = afterSlash.substring(afterSlash.indexOf(" "));
30 String updatedDA = beforeSlash + "/" + fontDRName + afterFont;
31
32 textAnnot.setDefaultAppearance(updatedDA);
33
34 // refresh text annotation appearance
35 textAnnot.refreshAppearance();
36 pdfViewCtrl.update();
37 }
38 }
39 });
40}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales