As suggested by Mr Bruno Lowagie Here, I've been using the push button field to fill images in custom templates, as such:
AcroFields form = stamper.AcroFields;
PushbuttonField logo = form.GetNewPushbuttonFromField("1");
if(logo != null)
{
logo.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
logo.ProportionalIcon = true;
logo.Image = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/Image.jpg"));
form.ReplacePushbuttonField("1", logo.Field);
}
The problem is if I have multiple pushbuttons with field names 1, and I want to replace all of them with that same image, this only replaces the first one.
These are the push button fields before replacing their icon:
This is after I try to replace them, and only the first gets replaced:
I also saw that we can set form.GetNewPushbuttonFromField(string field, int order)
, where order should be the index of the field with that field name?
So for testing I tried:
PushbuttonField logo = form.GetNewPushbuttonFromField("1", 0);
if(logo != null)
{
logo.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
logo.ProportionalIcon = true;
logo.Image = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/Image.jpg"));
form.ReplacePushbuttonField("1", logo.Field);
}
PushbuttonField logo2 = form.GetNewPushbuttonFromField("1", 1);
if (CDlogo2 != null)
{
logo2.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
logo2.ProportionalIcon = true;
logo2.Image = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/Image.jpg"));
form.ReplacePushbuttonField("1", logo2.Field);
}
However, for some reason the first pushbutton icon does not change and gets placed in the location of the second pushbutton that gets its icon changed, as such:
I need to be able to list through all of the pushbutton fields with that name field and replace all of them with that same image, how would I do that?
Thank you,
EDIT
As requested Here is a link to view the pdfs
EDIT 2
If anyone cares this is how I'm using mkl's answer to loop and fill all images: (I know it's done terribly, but it works, yes, I'm keeping it for the time being)
int n= 0;
PushbuttonField logo = form.GetNewPushbuttonFromField("1", n);
while (logo != null)
{
n++;
logo = form.GetNewPushbuttonFromField("1", n);
}
PushbuttonField logo2;
for (int z = 0; z < n; z++)
{
logo2 = form.GetNewPushbuttonFromField("1", z);
logo2.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
logo2.ProportionalIcon = true;
logo2.Image = iTextSharp.text.Image.GetInstance(Server.MapPath(imagePath));
form.ReplacePushbuttonField("1", logo2.Field, z);
}
Because I couldn't find a method to get the number of pushbuttons with the same name.