2016-12-15 134 views
0

對於IText 5,添加數字簽名相當容易。 的鏈接,它的文檔是: http://developers.itextpdf.com/examples/security/digital-signatures-white-paper/digital-signatures-chapter-2使用IText將數字簽名添加到PDF 7

有人可以分享的鏈接文檔中iText的7這樣做? 我嘗試過各種方法無濟於事。在線找不到任何鏈接。我可以簽名並檢查簽名,但不能添加它。

+0

你有沒有看看[iText的7 Java範例的GitHub庫(https://github.com/itext/i7js-samples)?在子文件夾[/publications/signatures/](https://github.com/itext/i7js-samples/tree/develop/publications/signatures)中,它包含了移植到iText 7的白皮書示例。 – mkl

+0

非常感謝。如果你可以在下面寫出答案,那麼請將它標記爲:) –

回答

2

數字簽名的端口對於iText 7的白皮書代碼示例可以在iText 7 Java samples github repository子文件夾/publications/signatures/(例如)中找到。從簡單C2_01_SignHelloWorld示例節選:

public void sign(String src, String dest, 
       Certificate[] chain, 
       PrivateKey pk, String digestAlgorithm, String provider, 
       PdfSigner.CryptoStandard subfilter, 
       String reason, String location) 
     throws GeneralSecurityException, IOException { 
    // Creating the reader and the signer 
    PdfReader reader = new PdfReader(src); 
    PdfSigner signer = new PdfSigner(reader, new FileOutputStream(dest), false); 
    // Creating the appearance 
    PdfSignatureAppearance appearance = signer.getSignatureAppearance() 
      .setReason(reason) 
      .setLocation(location) 
      .setReuseAppearance(false); 
    Rectangle rect = new Rectangle(36, 648, 200, 100); 
    appearance 
      .setPageRect(rect) 
      .setPageNumber(1); 
    signer.setFieldName("sig"); 
    // Creating the signature 
    IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, provider); 
    IExternalDigest digest = new BouncyCastleDigest(); 
    signer.signDetached(digest, pks, chain, null, null, null, 0, subfilter); 
}