How to add an image to an AcroForm field?

Tags: formsimagesimage fieldpushbutton fieldiText 7

I'm trying to fill out a PDF form using the AcroFields class. I'm able to add text data perfectly, but I'm having issues adding images. How is this done?

Posted on StackOverflow on Apr 17, 2013 by Anil M

The "official" way to do this, is to have a button field as placeholder for the image, and to replace the "icon" of the button. In iText7 you can write your own CustomButton class that extends AbstractElement class, override draw(DrawContext) method and create an instance like this:

CustomButton ad = new CustomButton((PdfButtonFormField) form.getField("advertisement"));
ad.setImage(ImageDataFactory.create(resource));
form.removeField("advertisement");
doc.add(new Paragraph().add(ad));

See ReplaceIcon.java for the full code sample.

Click this link if you want to see how to answer this question in iText 5.