How to add an image to an AcroForm field?

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

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.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now