使用 iText7 获取 pdf 签名



我有一个场景,我需要使用 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

引发异常是因为文档中没有 AcroForm,SignatureUtil尝试添加它,但没有关联的PdfWriter

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

PdfAcroForm.getAcroForm(document, false) != null

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

最新更新