Change appearance of an Annotation

Hi, I'm trying put a custom icon on a pdftron.PDF.Annots.Text, but I had no success. Researching on group I've found some suggestions using the property setAppearance, but apparently the component doesn't use the image I passed and loads a default icon. The image I'm trying to load is a png stored in android drawable resources. Follow the code I used in my tests:

    ElementBuilder build = new ElementBuilder();
    ElementWriter writer = new ElementWriter();
    writer.begin(doc);
    Image img = Image.create(doc, BitmapFactory.decodeResource(context.getResources(), R.drawable.my_icon));
    int w = img.getImageWidth(), h = img.getImageHeight();
    Element img_element = build.createImage(img, 0, 0, w, h);
    writer.writePlacedElement(img_element);
    Obj obj = writer.end();
    pdftron.PDF.Annots.Text text = pdftron.PDF.Annots.Text.create(doc, p);
    text.setAppearance(obj);
    text.refreshAppearance();

Am I missing something?

Thanks in advance,

Hector Martins

Hello Hector,

Since RefreshAppearance() overwrites the value set by SetAppearance(), you should not call it. You also need to set the BBox of the appearance stream. Something similar to the following should work:

ElementBuilder build = new ElementBuilder();
ElementWriter writer = new ElementWriter();
writer.Begin(doc);
Image img = Image.Create(doc,input_path + "butterfly.png");
int w = img.GetImageWidth(),h = img.GetImageHeight();
Element img_element = build.CreateImage(img,0,0,w,h);
writer.WritePlacedElement(img_element);
pdftron.PDF.Annots.Text text = pdftron.PDF.Annots.Text.Create(doc,new Rect(161, 388, 180, 406),"Hello World");
Obj appearance_stream = writer.End();
appearance_stream.PutRect("BBox",0,0,w,h);
text.SetAppearance(appearance_stream);
page.AnnotPushBack(text);