Some test text!

menu
Style propertieskeyboard_arrow_down

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 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.

linkChanging 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().
void changeAnnotThickness(final PDFViewCtrl pdfViewCtrl, final Annot annot, final double thickness) throws Exception {
    // Locks the document first as accessing annotation/doc
    // information isn't thread safe.
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            Annot.BorderStyle borderStyle = annot.getBorderStyle();
            borderStyle.setWidth(thickness);

            annot.refreshAppearance();
            pdfViewCtrl.update();
        }
    });
}
To make annotation border invisible, call borderStyle.setWidth(0).

linkChanging 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).

void changeAnnotBorderColor(final PDFViewCtrl pdfViewCtrl, final Annot annot, final @ColorInt int color) throws Exception {
    // Locks the document first as accessing annotation/doc
    // information isn't thread safe.
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            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();
            pdfViewCtrl.update();
        }
    });
}
To set the color of a Text annotation, call annot.setColor(ColorPt, int).

linkChanging an annotation opacity

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

void changeAnnotOpacity(final PDFViewCtrl pdfViewCtrl, final Annot annot, final double opacity) throws Exception {
    // Locks the document first as accessing annotation/doc
    // information isn't thread safe.
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            // 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();
            pdfViewCtrl.update();
        }
    });
}

linkChanging an annotation fill color

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

void changeAnnotInteriorColor(final PDFViewCtrl pdfViewCtrl, final Annot annot, @ColorInt final int color) throws Exception {
    // Locks the document first as accessing annotation/doc
    // information isn't thread safe.
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            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();
            pdfViewCtrl.update();
        }
    });
}

linkChanging 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.
void changeAnnotInteriorColor(final PDFViewCtrl pdfViewCtrl, final Annot annot, final String icon) throws Exception {
    // Locks the document first as accessing annotation/doc
    // information isn't thread safe.
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            Text text = new Text(annot);
            text.setIcon(icon);
            // refresh the annotation appearch
            AnnotUtils.refreshStickyNoteAppearance(annot, pdfViewCtrl);
            pdfViewCtrl.update();
        }
    });
}

linkChanging a FreeText annotation text color

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

void changeFreeTextColor(final PDFViewCtrl pdfViewCtrl, final Annot annot, @ColorInt final int color) throws Exception {

    // Locks the document first as accessing annotation/doc
    // information isn't thread safe.
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            ColorPt colorPt = Utils.color2ColorPt(color);
            // set FreeText annotation text color
            FreeText freeText = new FreeText(annot);
            freeText.setTextColor(colorPt, 3);

            annot.refreshAppearance();
            pdfViewCtrl.update();
        }
    });
}

linkChanging a FreeText annotation text size

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

void changeFreeTextSize(final PDFViewCtrl pdfViewCtrl, final Annot annot, final double textSize) throws Exception {
    // Locks the document first as accessing annotation/doc
    // information isn't thread safe.
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            // set FreeText annotation text color
            FreeText freeText = new FreeText(annot);
            freeText.setFontSize(textSize);
            // refresh the annotation appearance
            annot.refreshAppearance();
            pdfViewCtrl.update();
        }
    });
}

linkChanging a FreeText annotation font

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

void changeFreeTextFont(final PDFViewCtrl pdfViewCtrl, final Annot annot, final String fontStr) throws Exception {
    // Locks the document first as accessing annotation/doc
    // information isn't thread safe.
    pdfViewCtrl.docLock(true, new PDFViewCtrl.LockRunnable() {
        @Override
        public void run() throws Exception {
            // 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(pdfViewCtrl.getDoc(), fontStr, 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();
                pdfViewCtrl.update();
            }
        }
    });
}

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

Join our live demo to learn about use cases & capabilities for WebViewer

Learn more
close