2013-03-05 52 views
1

我想檢查給定的PDF文件簽名是否存在時間戳。 到目前爲止,我來到這個代碼:使用iText檢查時間戳2.1.7

RandomAccessFileOrArray random = 
    new RandomAccessFileOrArray(new File("temp.pdf").getAbsolutePath()); 

PdfReader reader = new PdfReader(random, null); 
AcroFields af = reader.getAcroFields(); 
ArrayList<?> names = af.getSignatureNames(); 

//this are the signatures? 
for (Object o : names){ 

    AcroFields.Item item = (Item) af.getFields().get((String)o); 

    //this is the class for verifying the signature, 
    //how do I get it from the item? 
    PdfPKCS7 pdfPKCS7 = null; //XYZ ??? 

    Calendar signingDate = pdfPKCS7.getTimeStampDate(); 
} 

我顯然已取得了簽名,但我應該得到PdfPKCS7類驗證簽名。有誰知道我怎麼去那裏?

回答

1

您應該使用AcroFields方法verifySignature(String name),它將返回一個PdfPKCS7對象以繼續驗證。

該方法的顯示的JavaDoc其使用的一個示例:

KeyStore kall = PdfPKCS7.loadCacertsKeyStore(); 
PdfReader reader = new PdfReader("my_signed_doc.pdf"); 
AcroFields af = reader.getAcroFields(); 
ArrayList names = af.getSignatureNames(); 
for (int k = 0; k < names.size(); ++k) { 
    String name = (String)names.get(k); 
    System.out.println("Signature name: " + name); 
    System.out.println("Signature covers whole document: " + af.signatureCoversWholeDocument(name)); 
    PdfPKCS7 pk = af.verifySignature(name); 
    Calendar cal = pk.getSignDate(); 
    Certificate pkc[] = pk.getCertificates(); 
    System.out.println("Subject: " + PdfPKCS7.getSubjectFields(pk.getSigningCertificate())); 
    System.out.println("Document modified: " + !pk.verify()); 
    Object fails[] = PdfPKCS7.verifyCertificates(pkc, kall, null, cal); 
    if (fails == null) 
     System.out.println("Certificates verified against the KeyStore"); 
    else 
     System.out.println("Certificate failed: " + fails[1]); 
} 

在這裏可以使用PdfPKCS7實例很容易地添加額外的代碼。

Ceterum censeo ...除非你被綁定到古老的iText版本(例如,由於兼容性或許可證問題),但是,你應該考慮更新到當前版本。

+0

我以前有過這個解決方案,但它不會編譯。我下載了itext版本,發現af.verifySignature(name)起作用。這是我修改過的第三方版本。謝謝。 – mrcaramori 2013-03-06 14:12:40