Some test text!

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

Trial setup questions? Ask experts on Discord
Need other help? Contact Support
Pricing or product questions? Contact Sales