Itext 7.0读取PDF Acroforms字段为空问题求助
iText 7读取特定PDF表单字段返回空值的解决方法
- 存在一个已填充字段的PDF表单(dnd-character.pdf),使用iText 7.0读取其AcroFields时返回空值,但该PDF中的字段可正常修改并保存值
- 现有代码能正常读取其他PDF表单(如PDF2),但读取此目标PDF时完全失效
- 尝试旧方案
PdfArray fields = form.GetAsArray(PdfName.Fields);触发NullReferenceException,已确认目标PDF非XFA表单 - 通过RUPS对比PDF层级结构,发现目标PDF的表单字段组织方式与正常PDF不同,现寻求读取该PDF表单字段值(如/V对应内容)的解决方法
测试代码如下:
namespace Tutorial { public class FillForm { public const String SRC = "../../../resources/pdf/dnd-character.pdf"; public const String DEST = "../../../results/fill_dnd.pdf"; public static void Main(String[] args) { FileInfo file = new FileInfo(DEST); file.Directory.Create(); new FillForm().ManipulatePdf(SRC, DEST); } public virtual void ManipulatePdf(String src, String dest) { PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); PdfAcroForm form = PdfFormCreator.GetAcroForm(pdf, true); IDictionary<String, PdfFormField> fields = form.GetAllFormFields(); Console.WriteLine(fields.Count); foreach(KeyValuePair<string, PdfFormField> entry in fields) { Console.WriteLine(entry.Key); } pdf.Close(); } } }
内容的提问来源于stack exchange,提问作者Jesse Hamlin-Navias
相关产品推荐
相关产品推荐

