itextsharp - Cant Set Checkbox when PdfStamper in Append Mode

蹲街弑〆低调 提交于 2019-12-12 04:59:46

问题


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();

回答1:


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");
}


来源:https://stackoverflow.com/questions/46200640/itextsharp-cant-set-checkbox-when-pdfstamper-in-append-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!