How to change the icon of a push button field?
I'am currently trying to fill out a predefined Form with itextsharp. All works well except adding an image. I was able to do this using a different library by changing the icon of a button in a predefined form. That scaled the image correctly. Unfortunatly I couldn't find any method to do this with iTextSharp.
This is what I've tried so far. It adds the image at an absolute position:
PdfContentByte pdfContentByte = pdfStamper.GetOverContent(1); iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(picfile); iTextSharp.text.Rectangle imageRect = new iTextSharp.text.Rectangle(338f, 65f, 250f, 200f, 270); img.ScaleToFit(imageRect.Width, imageRect.Height); img.SetAbsolutePosition(65, 250); pdfContentByte.AddImage(img);
Unfortunately, the image isn't sized correctly. I think there must be a better way to do this.
Posted on StackOverflow on Oct 23, 2012 by xXx
Your code sample simply adds an image to the content of a page. As you indicate, that's not what you want. You want to replace the icon in a button field. This can be done in iText 7 using this code snippet:
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src),new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
Document doc = new Document(pdfDoc);
CustomButton ad = new CustomButton((PdfButtonFormField) form.getField("advertisement"));
ad.setImage(ImageDataFactory.create(resource));
form.removeField("advertisement");
doc.add(new Paragraph().add(ad));
pdfDoc.close();
Take a look at the ReplaceIcon example to learn the CustomButton
class realization. You'll find that we need an extension of AbstractRenderer
, where we'll override layout()
and draw()
methods.
Click How to change the icon of a push button field? if you want to see how to answer this question in iText 5.