iTextSharp SetField用于在不同页面上具有相同名称的字段 [英] iTextSharp SetField for fields with same name on different pages

查看:84
本文介绍了iTextSharp SetField用于在不同页面上具有相同名称的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用acroFields.GetTranslatedFieldName()从pdf中获取下一个字段名称:

I used acroFields.GetTranslatedFieldName() to get the next fieldnames from the pdf:

topmostSubform[0].Page1[0].CheckBox2A[0]
topmostSubform[0].Page1[0].CheckBox2A[1]
topmostSubform[0].Page2[0].CheckBox2A[0]
topmostSubform[0].Page2[0].CheckBox2A[1] 
topmostSubform[0].Page3[0].CheckBox2A[0]
topmostSubform[0].Page3[0].CheckBox2A[1] 

我使用下一行代码填充第二页上的CheckBox2A [0].

I use the next line of code to fill CheckBox2A[0] on the second page.

fields.SetField("topmostSubform[0].Page2[0].CheckBox2A[0]", "1")

而不是第二页上的CheckBox2A [0],而是被选中了第一页上的CheckBox2A [0].

Instead of CheckBox2A[0] on the second page, CheckBox2A[0] on the first page gets checked.

推荐答案

iTextSharp(以及iText for Java,已通过

iTextSharp (and also iText for Java, tested here) does not correctly associate the reference derived from the XFA template structure

topmostSubform[0].Page2[0].CheckBox2A[0]

在一个XFA数据集中具有匹配条目:

with the matching entry in the one XFA dataset:

<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" >
  <xfa:data >
    <topmostSubform >
<!-- vvv the incorrect match by iText -->
      <CheckBox2A >0</CheckBox2A >
<!-- ^^^ the incorrect match by iText -->
      <CheckBox2A >0</CheckBox2A >
      <CheckBox2B >0</CheckBox2B >
      <CheckBox2B >0</CheckBox2B >
      <Voorletters />
      <Achternaam />
      <CheckBox2C >0</CheckBox2C >
      <CheckBox2C >0</CheckBox2C >
      <DatumDag />
      <DatumMaand />
      <DatumJaar />
      <TextField1 />
      <HuisNrTekst5 />
      <HuisNrNummer5 />
      <Straat />
      <PostcodeNr1c />
      <PostcodeAlpha1c />
      <Plaats />
      <HuisNrTekst5 />
      <HuisNrNummer5 />
      <TextField1 />
      <PostcodeNr1c />
      <PostcodeAlpha1c />
      <Plaats />
      <TextField1 />
      <CheckBox2D >0</CheckBox2D >
      <CheckBox2D >0</CheckBox2D >
      <IBANREKC_1 />
      <IBANREKB_1 />
      <IBANREKA_1 />
      <IBAN_1 />
      <BurgerserviceNr />
      <Voorletters />
      <Achternaam />
      <CheckBox2E >0</CheckBox2E >
      <CheckBox2E >0</CheckBox2E >
      <HuisNrTekst5 />
      <HuisNrNummer5 />
      <Straat />
      <PostcodeNr1c />
      <PostcodeAlpha1c />
      <Plaats />
      <DatumDag />
      <DatumMaand />
      <DatumJaar />
      <IBANREKC_2 />
      <IBANREKB_2 />
      <IBANREKA_2 />
      <IBAN_2 />
      <Telefoon />
<!-- vvv the correct match -->
      <CheckBox2A >0</CheckBox2A >
<!-- ^^^ the correct match -->
      <CheckBox2A >0</CheckBox2A >
      <CheckBox2B >0</CheckBox2B >
      <CheckBox2B >0</CheckBox2B >
      <IBANREKC_1 />
      <IBANREKB_1 />
      <IBANREKA_1 />
      <IBAN_1 />
      <CheckBox2C >0</CheckBox2C >
      <CheckBox2C >0</CheckBox2C >
      <TextField1 />
      <TextField1 />
      <CheckBox2D >0</CheckBox2D >
      <CheckBox2D >0</CheckBox2D >
      <Telefoon />
      <CheckBox2E >0</CheckBox2E >
      <CheckBox2E >0</CheckBox2E >
      <CheckBox2F >0</CheckBox2F >
      <CheckBox2F >0</CheckBox2F >
      <TextField1 />
      <TextField1 />
      <CheckBox2G >0</CheckBox2G >
      <CheckBox2G >0</CheckBox2G >
      <CheckBox3B >0</CheckBox3B >
      <CheckBox3B >0</CheckBox3B >
      <CheckBox3B >0</CheckBox3B >
      <IBAN_1E_01 />
      <IBAN_1D_01 />
      <IBAN_1C_01 />
      <IBAN_1A_01 />
      <IBAN_1B_01 />
      <IBANREKC_1 />
      <IBANREKB_1 />
      <IBANREKA_1 />
      <IBAN_1 />
      <IBAN_1E_02 />
      <IBAN_1D_02 />
      <IBAN_1C_02 />
      <IBAN_1B_02 />
      <IBAN_1A_02 />
      <CheckBox3D >0</CheckBox3D >
      <CheckBox3D >0</CheckBox3D >
      <CheckBox3D >0</CheckBox3D >
      <CheckBox3A >0</CheckBox3A >
      <CheckBox3A >0</CheckBox3A >
      <CheckBox3A >0</CheckBox3A >
      <CheckBox2A >0</CheckBox2A >
      <CheckBox2A >0</CheckBox2A >
      <CheckBox3B >0</CheckBox3B >
      <CheckBox3B >0</CheckBox3B >
      <CheckBox3B >0</CheckBox3B >
      <DatumDag />
      <DatumMaand />
      <DatumJaar />
      <TextField1 />
      <DatumDag />
      <DatumMaand />
      <DatumJaar />
      <TextField1 />
      <Telefoon />
    </topmostSubform >
  </xfa:data >
</xfa:datasets >

因此,错误的数据集元素被更改.恐怕iText开发必须考虑到这一点,我手头没有解决方法.

Thus, the wrong dataset element is changed. I'm afraid this has to be looked into by iText development, I have no fix at hand.

但是,您可能很幸运:文档中的表单实际上是AcroForm和XFA表示的混合形式.在您的fields.SetField调用期间,iText尝试在两种表示形式中设置值,实际上,在AcroForm表示形式中,它设置了正确的值.

You might be in luck, though: The form in your document actually is a hybrid with both an AcroForm and an XFA representation. During your fields.SetField call iText attempts to set the value in both representations, and indeed, in the AcroForm representation it sets the correct one.

因此,如果不再需要结果PDF带有该XFA结构,则只需删除XFA结构即可:

Thus, if the result PDF is not required to carry that XFA structure anymore, you can simply drop the XFA structure:

using (var pdfReader = new PdfReader(file))
using (FileStream output = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
using (PdfStamper pdfStamper = new PdfStamper(pdfReader, output))
{
    AcroFields fields = pdfStamper.AcroFields;
    fields.SetField("topmostSubform[0].Page2[0].CheckBox2A[0]", "1");
    fields.RemoveXfa();
}

这样您就在第2页上得到了勾号:

Doing that you get the tick on page 2:

这篇关于iTextSharp SetField用于在不同页面上具有相同名称的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆