Reputation: 55
I am fairly new to iText. I downloaded a free 30 day trial and tried the following on .NET MVC: 1. Extract fields from PDF form:
string src = "mypdf.pdf";
string dest = "mypdfRES.pdf";
PdfReader newReader = new PdfReader(src);
newReader.SetUnethicalReading(true);
PdfDocument pdf = new PdfDocument(newReader, new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
After that I set a value to a specific field
PdfFormField toSet;
fields.TryGetValue("form1[0].#subform[0].Line1_FamilyName[0]", out toSet);
toSet.SetValue("Test familyname");
pdf.Close();
Now when I open up the newly saved PDF document mypdfRES.pdf all of fields are blank.
Please suggest why is iText automatically setting all my form fields to read-only.
PS. Link to the pdf document used in this test https://www.uscis.gov/system/files_force/files/form/i-765.pdf?download=1
Upvotes: 1
Views: 1344
Reputation: 96064
The PDF in question has three properties which make filling in more difficult:
Usually one can fill in such forms without encryption (item 3) by creating an incremental update, i.e. by working in append mode using new StampingProperties().useAppendMode()
. This keeps the usage rights signature valid and everything works fine.
Unfortunately for this encrypted file new StampingProperties().useAppendMode()
and even new StampingProperties().preserveEncryption().useAppendMode()
results in broken files. This might be related to the fact that newReader.SetUnethicalReading(true)
is used instead of supplying the owner password; but supplying the owner password hardly can be expected while filling in a government form... ;*)
A way to proceed with this form anyways is to
The result is a PDF with a pure AcroForm form which you can handle as you wish.
Your code as is already implicitly removes the encryption. To do the other two steps, simply add
form.RemoveXfaForm();
pdf.GetCatalog().Remove(PdfName.Perms);
before the pdf.Close()
.
(Tested with iText for .Net version 7.0.2.2 and iText for Java version 7.0.3-SNAPSHOT)
Upvotes: 2