【问题标题】:iText Acrofields Are Empty, form.getAsArray(PdfName.FIELDS) is also nulliText Acrofields 为空,form.getAsArray(PdfName.FIELDS) 也为空
【发布时间】:2021-08-20 18:43:21
【问题描述】:

我正在使用 iText7.NET。第三方为 PDF 提供了字段,这些字段存在并且 Adob​​e Acrobat 打开和显示 PDF 似乎没有问题,但在 iText 中,字段集合为空。

我已经在ItextSharp - Acrofields are empty 上看到了答案以及 iText 网站上的相关知识库文章,但在我的情况下该修复不起作用,因为 form.getAsArray(PdfName.FIELDS) 返回 null,所以它不能添加到。

我还检查了 Xfa,但似乎没有出现

XfaForm xfa = form.GetXfaForm();
xfa.IsXfaPresent()  // returns false

是否可以将 PdfName.FIELDS 添加到文档中然后填充?

谢谢

【问题讨论】:

  • 请分享pdf进行分析。
  • 不幸的是它充满了私人数据,当通过删除信息进行编辑时,保存在 Acrobat 中,然后它就可以工作了
  • 确实,该代码不仅应该跳过链接,还应该跳过除小部件之外的所有内容。不过,顺便说一句,您应该将解决方案发布为答案,而不是对您的问题进行编辑。这样,您最终可以将该答案标记为已接受且有效的答案。
  • 感谢@mkl,我更新为作为答案发布,并使过滤更具体到子类型“链接”

标签: c# itext7


【解决方案1】:

所以我想我已经找出了导致问题的原因,并为我的特殊情况提供了短期解决方案。在本文档中,一些字段是子类型“链接”,而不是“小部件”,并且我使用的修复代码(基于上面的链接,很可能来自这里 https://kb.itextsupport.com/home/it7kb/faq/why-are-the-acrofields-in-my-document-empty)将失败。我的解决方法是跳过子类型链接,尽管可能存在更好的解决方案,它不会跳过我不需要的链接。

如果我不跳过链接,当保存的 PDF 再次加载时它会失败

            PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);

在 itext.forms 的较低级别代码中,调用 IterateFields() 并在其中将 formField.GetParent() 作为参数传递给 PdfFormField.MakeFormField,GetParent() 为 Link 字段返回 null,因此存在异常。

下面是导致问题的第一个子类型链接字段的 RUPS 层次结构

因此,目前解决我的特定问题的解决方案是跳过子类型链接。代码如下

            PdfReader reader = new PdfReader(pdf);
            MemoryStream dest = new MemoryStream();
            PdfWriter writer = new PdfWriter(dest);
            PdfDocument pdfDoc = new PdfDocument(reader, writer);
            PdfCatalog root = pdfDoc.GetCatalog();
            PdfDictionary form = root.GetPdfObject().GetAsDictionary(PdfName.AcroForm);
            PdfArray fields = form.GetAsArray(PdfName.Fields);
            if (fields == null)
            {
                form.Put(PdfName.Fields, new PdfArray());
                fields = form.GetAsArray(PdfName.Fields);
            }
            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
            {
                PdfPage page = pdfDoc.GetPage(i);
                var annots = page.GetAnnotations();
                for (int j = 0; j < annots.Count(); j++)
                {
                    PdfObject o = annots[j].GetPdfObject();
                    PdfDictionary m = o as PdfDictionary;
                    string subType = m?.GetAsName(PdfName.Subtype)?.GetValue() ?? "";
                    if (subType != "Link")
                    {
                        fields.Add(o);
                        fields.SetModified();
                    }
                }
            }
            pdfDoc.Close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多