Some test text!

menu
search
Custom appearanceskeyboard_arrow_down

Customize an annotation's appearance

It's possible to modify an annotation's appearance directly using PDFNet.

The sample code below demonstrates how to replace an annotation's appearance with a custom image.

public void setCustomImage(Context context, Annot annot, PDFDoc doc, int imageRes) throws PDFNetException {
    // Initialize a new ElementWriter and ElementBuilder
    ElementWriter writer = new ElementWriter();
    ElementBuilder builder = new ElementBuilder();

    writer.begin(doc.getSDFDoc(), true);

    File imageFile = Utils.copyResourceToLocal(context, imageRes, "image", ".png");

    // Initialize the new image
    Image image = Image.create(doc.getSDFDoc(), imageFile.getAbsolutePath());
    int w = image.getImageWidth();
    int h = image.getImageHeight();

    // Initialize a new image element
    Element element = builder.createImage(image, 0, 0, w, h);

    // Write the element
    writer.writePlacedElement(element);

    // Get the bounding box of the new element
    Rect bbox = element.getBBox();

    // Configure the appearance stream that will be written to the annotation
    Obj new_appearance_stream = writer.end();

    // Set the bounding box to be the rect of the new element
    new_appearance_stream.putRect(
        "BBox",
        bbox.getX1(),
        bbox.getY1(),
        bbox.getX2(),
        bbox.getY2());

    // Overwrite the annotation's appearance with the new appearance stream
    annot.setAppearance(new_appearance_stream);
}

In addition to images, an annotation's appearance can be created using (nearly) everything the PDF specification allows, including vector content and text. For an example of how to create PDF vector and text content, see the ElementBuilder sample.

Warning
If you call RefreshAppearance after modifying the appearance of a `PTAnnot`, then the annotation's default appearance will be restored and any changes you have made will be lost.

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.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close