【问题标题】:Getting pdf signatures with iText7使用 iText7 获取 pdf 签名
【发布时间】:2018-04-20 20:45:41
【问题描述】:

我有一个场景,我需要使用 iText7 库从 pdf 中获取签名信息。签名可能存在也可能不存在。当我为没有任何数字签名的 PDF 实例化一个新的 SignatureUtil 对象时,我得到了异常

“没有关联的 PdfWriter 用于制作间接。”

。如果有签名,它就可以正常工作。我不确定如何更正此异常。

已更新以包含代码示例

Using reader As New PdfReader(pdfPath),
            pdf As New PdfDocument(reader)

                Dim util As New SignatureUtil(pdf)

                Dim signModel As String = "[Signature: {0} - {1}]"

                For Each signame As String In util.GetSignatureNames()
                    Dim whoisthis As PdfSignature = util.GetSignature(signame)
                    returnVal &= String.Format(
                        signModel,
                        whoisthis.GetName(),
                        whoisthis.GetReason
                        )
                Next

        End Using

【问题讨论】:

  • 请向我们展示您的代码。
  • 已更新以包含我的代码。

标签: itext7


【解决方案1】:

抛出异常是因为文档中没有 AcroForm 并且SignatureUtil 尝试添加它,但是没有关联的PdfWriter

作为一种解决方法,您可以检查文档是否包含 AcroForm:

PdfAcroForm.getAcroForm(document, false) != null

并且仅在存在 AcroForm 时创建SignatureUtil。如果没有 AcroForm,就没有签名字段。

【讨论】:

  • 应该添加到SignatureUtil构造函数JavaDocs中。
  • 我完全同意你的看法@mkl。这个类也将被重构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
相关资源
最近更新 更多