Home > Back-end >  PdfTextFormField values is not getting populated with iText 7.2.0
PdfTextFormField values is not getting populated with iText 7.2.0

Time:02-19

In my .Net project, I am using

    <PackageReference Include="itext7" Version="7.2.0" />

In c# class,

PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);

PdfTextFormField borrowerField = PdfFormField.CreateText(pdf);

borrowerField.SetFieldName("Borrowers_Name");
borrowerField.SetValue("Name");
form.AddField(borrowerField);

I can see field name as "Borrowers_Name" but value is iText.Forms.Fields.PdfTextFormField instead of Name. Hence in my PDF, I see value as blank.

When I try to get value using

form.GetField("Borrowers_Name").GetValueAsString();

I can see value which is set. But when I open up the pdf, it's empty.

CodePudding user response:

You create a form field but you don't define its area on page. Thus, the field is created as invisible field.

The easiest way to define an on-page area for your field is in the creation method, e.g. by replacing

PdfTextFormField borrowerField = PdfFormField.CreateText(pdf);

by

PdfTextFormField borrowerField = PdfFormField.CreateText(pdf, new Rectangle(100, 600, 200, 20));

With this you get a visual representation of your field like this:

Screen shot of field

  • Related