Some test text!

Hamburger Icon

Android / Guides / Custom appearances

Create custom annotations in Android

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

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

    // Overwrite the annotation's appearance with the 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 .

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: Chat with us