iText 7 .NET makes my fields readonly automaticall

2019-08-12 15:35发布

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

标签: .net pdf itext7
1条回答
祖国的老花朵
2楼-- · 2019-08-12 16:39

The PDF in question has three properties which make filling in more difficult:

  1. The form definition in it is a hybrid XFA / AcroForm definition pair.
  2. The file is signed using a usage rights (UR3) signature required by the XFA form for saving.
  3. The file is encrypted.

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

  • remove the encryption,
  • remove the usage rights signature (which has been broken by removing the encryption), and
  • remove the XFA form definition (which after the removal of the usage rights signature does not work properly anymore).

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)

查看更多
登录 后发表回答