Annotation style

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: changing tool.

With PDFTron 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().

public static void changeAnnotThickness(PDFViewCtrl mPdfViewCtrl, Annot annot, double thickness) {
    boolean shouldUnlock = false;
    try {
        // Locks the document first as accessing annotation/doc
        // information isn't thread safe.
        mPdfViewCtrl.docLock(true);
        shouldUnlock = true;
        Annot.BorderStyle borderStyle = annot.getBorderStyle();
        borderStyle.setWidth(thickness);

        annot.refreshAppearance();
        mPdfViewCtrl.update();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (shouldUnlock) {
            mPdfViewCtrl.docUnlock();
        }
    }
}
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).

public static void changeAnnotBorderColor(PDFViewCtrl mPdfViewCtrl, Annot annot, @ColorInt int color) {
    boolean shouldUnlock = false;
    try {
        // Locks the document first as accessing annotation/doc
        // information isn't thread safe.
        mPdfViewCtrl.docLock(true);
        shouldUnlock = true;
        ColorPt colorPt = Utils.color2ColorPt(color);
        // if color is transparent, then color component number is 0.
        int colorCompNum = color == Color.TRANSPARENT ? 0 : 3;
        // if the annotation is a FreeText annotation
        if (annot.getType() == Annot.e_FreeText) {
            FreeText freeText = new FreeText(annot);
            freeText.setLineColor(colorPt, colorCompNum);
        } else {
            annot.setColor(colorPt, colorCompNum);
        }

        annot.refreshAppearance();
        mPdfViewCtrl.update();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (shouldUnlock) {
            mPdfViewCtrl.docUnlock();
        }
    }
}
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):

public static void changeAnnotOpacity(PDFViewCtrl mPdfViewCtrl, Annot annot, double opacity) {
    boolean shouldUnlock = false;
    try {
        // Locks the document first as accessing annotation/doc
        // information isn't thread safe.
        mPdfViewCtrl.docLock(true);
        shouldUnlock = true;
        // if the annotation is a markup annotation,
        // set opacity to the annotation
        if (annot.isMarkup()) {
          Markup markup = new Markup(annot);
          markup.setOpacity(opacity);
        }
        
        annot.refreshAppearance();
        mPdfViewCtrl.update();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (shouldUnlock) {
            mPdfViewCtrl.docUnlock();
        }
    }
}

Changing an annotation fill color

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

public static void changeAnnotInteriorColor(PDFViewCtrl mPdfViewCtrl, Annot annot, @ColorInt int color) {
    boolean shouldUnlock = false;
    try {
        // Locks the document first as accessing annotation/doc
        // information isn't thread safe.
        mPdfViewCtrl.docLock(true);
        shouldUnlock = true;
        ColorPt colorPt = Utils.color2ColorPt(color);
        // if color is transparent, then color component number is 0.
        int colorCompNum = color == Color.TRANSPARENT ? 0 : 3;
        // if the annotation is a Markup annotation,
        // set interior color to the annotation.
        if (annot.isMarkup()) {
            Markup markup = new Markup(annot);
            markup.setInteriorColor(colorPt, colorCompNum);
        }
        // refresh the annotation appearch
        annot.refreshAppearance();
        mPdfViewCtrl.update();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (shouldUnlock) {
            mPdfViewCtrl.docUnlock();
        }
    }
}

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.
public static void changeAnnotInteriorColor(PDFViewCtrl mPdfViewCtrl, Annot annot, String icon) {
    boolean shouldUnlock = false;
    try {
        // Locks the document first as accessing annotation/doc
        // information isn't thread safe.
        mPdfViewCtrl.docLock(true);
        shouldUnlock = true;
        
        Text text = new Text(annot);
        text.setIcon(icon);
        // refresh the annotation appearch
        AnnotUtils.refreshStickyNoteAppearance(annot, pdfViewCtrl);
        mPdfViewCtrl.update();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (shouldUnlock) {
            mPdfViewCtrl.docUnlock();
        }
    }
}

Changing a FreeText annotation text color

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

public static void changeFreeTextColor(PDFViewCtrl mPdfViewCtrl, Annot annot, @ColorInt int color) {
  boolean shouldUnlock = false;
    try {
        // Locks the document first as accessing annotation/doc
        // information isn't thread safe.
        mPdfViewCtrl.docLock(true);
        shouldUnlock = true;
        ColorPt colorPt = Utils.color2ColorPt(color);
        // set FreeText annotation text color
        FreeText freeText = new FreeText(annot);
        freeText.setTextColor(colorPt, 3);

        annot.refreshAppearance();
        mPdfViewCtrl.update();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (shouldUnlock) {
            mPdfViewCtrl.docUnlock();
        }
    }
}

Changing a FreeText annotation text size

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

public static void changeFreeTextSize(PDFViewCtrl mPdfViewCtrl, Annot annot, double textSize) {
  boolean shouldUnlock = false;
    try {
        // Locks the document first as accessing annotation/doc
        // information isn't thread safe.
        mPdfViewCtrl.docLock(true);
        shouldUnlock = true;
        // set FreeText annotation text color
        FreeText freeText = new FreeText(annot);
        freeText.setFontSize(textSize);
        // refresh the annotation appearance
        annot.refreshAppearance();
        mPdfViewCtrl.update();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (shouldUnlock) {
            mPdfViewCtrl.docUnlock();
        }
    }
}

Changing a FreeText annotation font

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

public static void changeFreeTextFont(PDFViewCtrl mPdfViewCtrl, Annot annot, String font) {
  boolean shouldUnlock = false;
    try {
        // Locks the document first as accessing annotation/doc
        // information isn't thread safe.
        mPdfViewCtrl.docLock(true);
        shouldUnlock = true;
        // set FreeText annotation text color
        FreeText textAnnot = new FreeText(annot);
        String fontDRName = "F0";

        // Create a DR entry for embedding the font
        Obj annotObj = textAnnot.getSDFObj();
        Obj drDict = annotObj.putDict("DR");
        Obj fontDict = drDict.putDict("Font");

        // Embed the font
        Font font = Font.create(mPdfViewCtrl.getDoc(), font, textAnnot.getContents());
        fontDict.put(fontDRName, font.GetSDFObj());
        String fontName = font.getName();

        // Set DA string
        String DA = textAnnot.getDefaultAppearance();
        int slashPosition = DA.indexOf("/", 0);

        // if DR string contains '/' which it always should.
        if (slashPosition > 0) {
            String beforeSlash = DA.substring(0, slashPosition);
            String afterSlash = DA.substring(slashPosition);
            String afterFont = afterSlash.substring(afterSlash.indexOf(" "));
            String updatedDA = beforeSlash + "/" + fontDRName + afterFont;

            textAnnot.setDefaultAppearance(updatedDA);

            // refresh text annotation appearance
            textAnnot.refreshAppearance();
            mPdfViewCtrl.update();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (shouldUnlock) {
            mPdfViewCtrl.docUnlock();
        }
    }
}