2013-02-12 68 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签名白纸,更新您的签名创建代码来生成非过时类型的签名。