Reputation: 63
I know there are many similar questions on here, i've tried several of them but have not managed to solve the problem(and many of them are still unanswered after several years).
My problem is that i can not set the value of the two fields at the top of the second page named "form1[0].sida2[0].flt_datSidhuvud[0]" and "form1[0].sida2[0].flt_txtPersonNrBrukare[0]", they both have a field with same name but different prefix on the first page and from the research i've done this might be causing the problem but the suggested solutions have not worked for me.
If i fill in the form manually with for example Acrobat Reader the values that are input in the fields on page one automatically appear in the fields on page two and the other way around.
Here is an example of the code i use to try and fill in the two fields
MemoryStream output = new MemoryStream();
FileStream fs = File.Open(pdfTemplatePath, FileMode.Open, FileAccess.Read);
PdfReader reader = new PdfReader(fs);
fs.Close();
PdfStamper stamper = new PdfStamper(reader, output);
var formFields = stamper.AcroFields;
formFields.SetField("form1[0].sida2[0].flt_datSidhuvud[0]", "2016-12"));
formFields.SetField("form1[0].sida2[0].flt_txtPersonNrBrukare[0]", data.SocialSecurityNumber);//data.SocialSecurityNumber is a string
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
The result is that it fills in the value of the fields on the first page only.
From my research this question here on SO was the most promising but the suggested solution(to remove XFA) doesn't seem to work in my case.
Upvotes: 0
Views: 877
Reputation: 95963
If (AcroForm) form fields have different full names, they are separate fields.
A behavior as you describe (filling one field in Adobe Reader upon losing focus automatically fills another one, too) can be achieved using JavaScript actions. But filling in these fields using iText does not trigger any JavaScript events. Thus, in general you have to fill in both (AcroForm) fields.
Your case is slightly different, though:
flt_datSidhuvud
and flt_txtPersonNrBrukare
fields on page 2 (which you do not explicitly fill) are empty and don't even have an appearance stream.flt_datSidhuvud
form fields are backed by a single data element, and so are both flt_txtPersonNrBrukare
form fields.Furthermore, your case is special because you flatten the form. If you did not flatten id, only the values in the fields of the AcroForm fields on the first page and the XFA data fields would be set, not the AcroForm fields on the second page.
Form flattening has also been substantially improved during the 5.x versions.
While flattening your hybrid form, the fields on page two get their values:
While flattening the still empty flt_datSidhuvud
and flt_txtPersonNrBrukare
AcroForm fields on page two, iText determines that they do not have any appearance streams yet to flatten into the page content and, therefore, tries to create appearance streams for them.
To create these appearances, iText first retrieves the value of each field. As mentioned above, this means that the value in the XFA form is looked up first which is the same for both the fields on page one and two. Thus, here the value you set for the field on the first page is retrieved and used for building the appearance on the second page!
In comments you said that you mostly used the older 4.1.6.0 iTextSharp version, not the current 5.5.11.0 one. As explained above, the automatic fill-in on your second page depends on the iText XFA support and form flattening improvements both of which were introduced during the 5.x versions.
Thus, your initial attempts to run the code from your question did not result in filled-in fields on page 2 because the older iTextSharp versions simply did not implement support for that.
Upvotes: 1