0

I want to set some fields in a pdf but leave them open to be modified after they have been populated. My issue is that when I edit the pdf in append mode the checkboxes aren't getting set. The text fields work fine.

string pdfTemplate = @"c:\test\fw4.pdf";
string newFile = @"c:\test\completed_fw4.pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
//PdfStamper pdfStamper = new PdfStamper(pdfReader, new System.IO.FileStream(newFile, System.IO.FileMode.Create), '\0', true); //doesn't set field - leaves editable
PdfStamper pdfStamper = new PdfStamper(pdfReader, new System.IO.FileStream(newFile, System.IO.FileMode.Create)); //sets field - leaves locked
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("topmostSubform[0].Page1[0].c1_01[1]", "2");
pdfStamper.Close();
  • Please share the pdf in question – mkl Sep 13 '17 at 18:23
  • [2017 W4](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwiFtZSU7KLWAhWEw4MKHXCyCoUQFggoMAA&url=https%3A%2F%2Fwww.irs.gov%2Fpub%2Firs-pdf%2Ffw4.pdf&usg=AFQjCNH6XrXINhUDXXX1C3llG2hdmsZL5g) – just4atwork Sep 13 '17 at 19:00

1 Answers1

0

The PDF form in question is a hybrid AcroForm/XFA form. iText(Sharp) 5 has only a limited support for XFA forms, and it looks like it cannot handle the checkboxes in question in the XFA form representation while it can handle it in the AcroForm representation.

This explains the observations:

  • On the one hand

    PdfStamper pdfStamper = new PdfStamper(pdfReader, new System.IO.FileStream(newFile, System.IO.FileMode.Create), '\0', true);
    //doesn't set field - leaves editable
    

    You work in append mode, so the usage rights signature remains valid and the PDF remains Reader enabled. Thus, Adobe Reader displays the XFA form and allows editing. As iText did not properly update the XFA form, the box is not checked.

  • On the other hand

    PdfStamper pdfStamper = new PdfStamper(pdfReader, new System.IO.FileStream(newFile, System.IO.FileMode.Create));
    //sets field - leaves locked
    

    You don't work in append mode, so the usage rights signature becomes invalid and the Reader enabling is broken, a situation in which Adobe Reader has even less features as without usage rights signature/Reader enabling. Thus, the Reader only displays the AcroForm and does not allow editing. But as iText did properly update the AcroForm form, the box is checked.

The best you can do with iText(Sharp) 5 in this situation is to remove both the XFA form and the usage rights signature. This leaves you with a pure AcroForm form and no limitations from an invalid usage rights signature:

using (PdfReader reader = new PdfReader(@"fw4.pdf"))
using (FileStream stream = new FileStream(@"fw4-SetCheckBox.pdf", FileMode.Create))
using (PdfStamper stamper = new PdfStamper(reader, stream))
{
    reader.Catalog.Remove(PdfName.PERMS);
    reader.Catalog.GetAsDict(PdfName.ACROFORM).Remove(PdfName.XFA);
    AcroFields pdfFormFields = stamper.AcroFields;
    pdfFormFields.SetField("topmostSubform[0].Page1[0].c1_01[1]", "2");
}
mkl
  • 90,588
  • 15
  • 125
  • 265