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.

1public void setCustomImage(Context context, Annot annot, PDFDoc doc, int imageRes) throws PDFNetException {
2 // Initialize a new ElementWriter and ElementBuilder
3 ElementWriter writer = new ElementWriter();
4 ElementBuilder builder = new ElementBuilder();
5 writer.begin(doc.getSDFDoc(), true);
6 File imageFile = Utils.copyResourceToLocal(context, imageRes, "image", ".png");
7 // Initialize the new image
8 Image image = Image.create(doc.getSDFDoc(), imageFile.getAbsolutePath());
9 int w = image.getImageWidth();
10 int h = image.getImageHeight();
11 // Initialize a new image element
12 Element element = builder.createImage(image, 0, 0, w, h);
13 // Write the element
14 writer.writePlacedElement(element);
15 // Get the bounding box of the new element
16 Rect bbox = element.getBBox();
17 // Configure the appearance stream that will be written to the annotation
18 Obj new_appearance_stream = writer.end();
19 // Set the bounding box to be the rect of the new element
20 new_appearance_stream.putRect(
21 "BBox",
22 bbox.getX1(),
23 bbox.getY1(),
24 bbox.getX2(),
25 bbox.getY2());
26 // Overwrite the annotation's appearance with the new appearance stream
27 annot.setAppearance(new_appearance_stream);
28}

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.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales