正如Bruno Lowagie先生Here所建议的那样,我一直在使用按钮字段在自定义模板中填充图像,例如:
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);
}
问题是,如果我有多个具有字段名称1的按钮,而我想用相同的图像替换所有按钮,那么这只会替换第一个按钮。
以下是替换其图标之前的按钮字段:
这是在我尝试替换它们之后,只有第一个被替换了:
我还看到我们可以设置form.GetNewPushbuttonFromField(string field, int order)
,其中顺序应该是具有该字段名称的字段的索引?
因此,我尝试进行测试:
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);
}
但是,由于某些原因,第一个按钮图标不会更改,而是放置在第二个按钮的位置,而第二个按钮的图标已更改,例如:
我需要能够使用该名称字段列出所有按钮字段,并用同一张图片替换所有按钮字段,我该怎么做?
谢谢
编辑
根据要求Here是查看pdf的链接
编辑2
如果有人在意,这就是我使用mkl的答案来循环并填充所有图像的方式:(我知道它做得非常好,但是可以,是的,我暂时保留了它)
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);
}
因为我找不到找到具有相同名称的按钮数量的方法。
答案 0 :(得分:1)
您
看到我们可以设置
form.GetNewPushbuttonFromField(string field, int order)
,其中order应该是具有该字段名称的字段的索引
这是正确的方向。但是您仍然使用form.ReplacePushbuttonField
重载而没有使用order
参数:
PushbuttonField logo2 = form.GetNewPushbuttonFromField("1", 1);
...
form.ReplacePushbuttonField("1", logo2.Field);
这将从名为“ 1”的第二个表单字段(包括第二个按钮的位置)创建一个PushbuttonField
,并使用其信息替换名为“ 1”的第一个字段。因此,
放置在第二个按钮的位置
因此,要解决此问题,还必须将form.ReplacePushbuttonField
重载和order
参数一起使用,如果您将form.GetNewPushbuttonFromField
重载和order
参数一起使用,并且值应匹配:
PushbuttonField logo2 = form.GetNewPushbuttonFromField("1", 1);
...
form.ReplacePushbuttonField("1", logo2.Field, 1);