2013-02-12 1280 views
0

我在嘗試對PDF文檔進行數字簽名時出現此錯誤。 傳遞兩個pdf SOURCEPDF名稱和DESTINATIONPDF名稱(數字SIGNED)pdf名稱。 在SOURCEPDF首次數字簽名後,我得到DESTINATIONPDF。 第二次數字簽名使用DESTINATIONPDF作爲源pdf以及目的地pdf。com.itextpdf.text.exceptions.InvalidPdfException:找不到PDF頭標籤

這裏是我的代碼

try 
{ 
    for(int i=1;i<=signature_Count;i++) 
    { 
     if(i==1) 
     { 
      tmpPdfSource=sourcePdfPath; 
     }else{ 
      this.tmpPdfSource=destinationPdfPath; 
     } 

     int pageNo=Integer.parseInt(ad.readXML(xmlString, rootName,"PageNo-"+i)); 
     String imageSource=ad.readXML(xmlString, rootName,"ImageSource-"+i); 
     float llx=Float.parseFloat(ad.readXML(xmlString, rootName,"llx-"+i)); 
     float lly=Float.parseFloat(ad.readXML(xmlString, rootName,"lly-"+i)); 
     float urx=Float.parseFloat(ad.readXML(xmlString, rootName,"urx-"+i)); 
     float ury=Float.parseFloat(ad.readXML(xmlString, rootName,"ury-"+i)); 
     String signature=ad.readXML(xmlString, rootName,"SignatureName-"+i); 

     File dest = new File(destinationPdfPath); 
     KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 
     ks.load(new Fil eInputStream(certificatePath), keystore_password.toCharArray()); 
     String alias = (String) ks.aliases().nextElement(); 
     PrivateKey pk = (PrivateKey) ks.getKey(alias,key_password.toCharArray()); 
     java.security.cert.Certificate[] chain = ks.getCertificateChain(alias); 
     PdfReader reader = new PdfReader(tmpPdfSource); 
     stamper = PdfStamper.createSignature(reader,new FileOutputStream(dest), '\0', null, true); 
     PdfSignatureAppearance appearance = stamper.getSignatureAppearance(); 

     appearance.setCrypto(pk, chain, null,PdfSignatureAppearance.SELF_SIGNED); 

     if (true) 
     { 
      appearance.setAcro6Layers(true); 
      Image img=Image.getInstance(imageSource); 
      appearance.setImage(img); 
      appearance.setVisibleSignature(new com.itextpdf.text.Rectangle(llx, lly, urx, ury), pageNo, signature); 
     } 
    }//for 
    stamper.close(); 
} catch (Exception e) { 
    GenericLog gl=new GenericLog(); 
    gl.writeWarning("Error Occured in SignPdfDocument "); 
    gl.writeError(e); 
    e.printStackTrace(); 
} 

請幫我解決這個錯誤。

回答

0

我看到方法setCrypto()這意味着你沒有使用最新版本的iText;我也看到選項PdfSignatureAppearance.SELF_SIGNED這意味着你正在創建一個不符合當今標準的簽名。

請你幫個忙,看看documentation

另外:您使用的是源文件和目標文件?這不可能。您至少需要創建一個臨時文件或在內存中創建該文件,然後覆蓋現有文件。

+0

謝謝布魯諾非常幫助完整的文檔。 – naveed 2013-02-14 08:21:03

1

已經重新格式化您的代碼,使其可讀,問題就變得appearant:

for(int i=1;i<=signature_Count;i++) 
{ 
    if(i==1) 
    { 
     tmpPdfSource=sourcePdfPath; 
    }else{ 
     this.tmpPdfSource=destinationPdfPath; 
    } 
    [...] 
    File dest = new File(destinationPdfPath); 
    [...] 
    PdfReader reader = new PdfReader(tmpPdfSource); 
    stamper = PdfStamper.createSignature(reader,new FileOutputStream(dest), '\0', null, true); 
    [...] 
}//for 
stamper.close(); 

從第二次迭代你看在以前的迭代由PdfStamper生成的文件,你必須關閉stamper在迭代結束時,不應超出for循環:

stamper = PdfStamper.createSignature(reader,new FileOutputStream(dest), '\0', null, true); 
    [...] 
    stamper.close(); 
}//for 

而且你最好把你的new FileOutputStream(dest)到一個變量,並明確將其關閉,同樣,關閉stamper:

FileOutputStream fout = new FileOutputStream(dest); 
    stamper = PdfStamper.createSignature(reader, fout, '\0', null, true); 
    [...] 
    stamper.close(); 
    fout.close(); 
}//for 

,當然還有後右拐,沿着布魯諾的建議,讀他的PDF簽名白紙,更新您的簽名創建代碼來生成非過時類型的簽名。