简体   繁体   English

PDFTron 数字签名

[英]PDFTron Digital Signature

I'm currently working on a Digital Signature project in Python which can create digital signature on PDF documents and verify them.我目前正在从事 Python 中的数字签名项目,该项目可以在 PDF 文档上创建数字签名并验证它们。 I was able to add a single digital signature to PDF Files using the code below:我能够使用以下代码向 PDF 文件添加单个数字签名:

PDFNet.Initialize()
doc = PDFDoc(pdf_path)
page1 = doc.GetPage(1)

# Create signature field
sign_field = doc.CreateDigitalSignatureField("Signature")
widgetAnnot = SignatureWidget.Create(doc, Rect(0, 0, 100, 100), sign_field)
page1.AnnotPushBack(widgetAnnot)
sign_field.SignOnNextSave(pkcs12_path, "")
# Save file
output_path = get_only_path(pdf_path) + "output.pdf"
doc.Save(output_path, SDFDoc.e_incremental)

PDFNet.Terminate()

I was also able to verify the document with the code below:我还能够使用以下代码验证文档:

    # Get certificate
    dsfield_iter = doc.GetDigitalSignatureFieldIterator()
    while dsfield_iter.HasNext():
        sign_field = dsfield_iter.Current()
        cert = sign_field.GetSignerCertFromCMS()
        dsfield_iter.Next()
    
    # Save certificate for verification
    cert_path = get_only_path(pdf_path) + "certificate.cer"
    with open(cert_path, "wb") as cer:
        # cert.GetData() return DER format
        cer.write(cert.GetData())

    # Verifying signature
    opts = VerificationOptions(VerificationOptions.e_compatibility_and_archiving)
    opts.AddTrustedCertificate(cert_path)
    result = doc.VerifySignedDigitalSignatures(opts)

However, I have a problem.但是,我有一个问题。 I want multiple people to be able to sign on the same document with their self signed certificate in different signature fields and also able to verify the document.我希望多人能够使用他们在不同签名字段中的自签名证书在同一个文档上签名,并且还能够验证文档。 I used my code for the same process but was unable to verify the signature after more than one people sign on it.我将我的代码用于相同的过程,但在多人签名后无法验证签名。 I would like to get help on this, please kindly share your thoughts:)我想得到这方面的帮助,请分享你的想法:)

Yes you can add multiple Signature Fields and Sign them using PDFTron SDK.是的,您可以添加多个签名字段并使用 PDFTron SDK 对其进行签名。 Just repeat the SignOnNextSave code you had above, and always make sure to save using e_incremental .只需重复上面的SignOnNextSave代码,并始终确保使用e_incremental进行保存。

Note though that currently PDFTron Validation would fail once two or more signatures added, as this would strictly speaking violate what the PDF specification describes for default behavior.请注意,当前 PDFTron 验证将在添加两个或更多签名后失败,因为严格来说这将违反 PDF 规范描述的默认行为。 Though I checked with Adobe Acrobat at least and it was fine with 2+ signatures, so at least Adobe appears to be doing something proprietary and non-standard.尽管我至少检查了 Adobe Acrobat 并且有 2+ 个签名很好,但至少 Adobe 似乎在做一些专有和非标准的事情。 I cannot say about other vendors, if that is important for you.如果这对您很重要,我不能说其他供应商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM