0

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:

enter image description here

This is after I try to replace them, and only the first gets replaced:

enter image description here

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:

enter image description here

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.

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
A.J Alhorr
  • 527
  • 7
  • 27

1 Answers1

1

You

saw that we can set form.GetNewPushbuttonFromField(string field, int order), where order should be the index of the field with that field name

which is the correct direction to look. But you still used the form.ReplacePushbuttonField overload without an order parameter:

PushbuttonField logo2 = form.GetNewPushbuttonFromField("1", 1);
...
form.ReplacePushbuttonField("1", logo2.Field);

This creates a PushbuttonField from the second form field named "1" (including the position of the second button) and uses its information to replace the first field named "1". Thus, it

gets placed in the location of the second pushbutton

So, to fix this you also have to use the form.ReplacePushbuttonField overload with an order parameter, if you use the form.GetNewPushbuttonFromField overload with an order parameter, and the values should match:

PushbuttonField logo2 = form.GetNewPushbuttonFromField("1", 1);
...
form.ReplacePushbuttonField("1", logo2.Field, 1);
mkl
  • 90,588
  • 15
  • 125
  • 265