I want to do multiple signatures in a pdf document with iText7 , but the problem is when I have once sign , it works . If I sign twice , the first signature is invalid . It looks like this :
unsigned PDF
signed PDF
and here is my code:
@Test
public void testMutiSign() {
iTextSignerUtil1.SignMultPDF(getBytes(unsignedPath), destPath1);
iTextSignerUtil2.SignMultPDF(getBytes(destPath1), destPath2);
}
IExternalSignatureContainer externalP7DetachSignatureContainer = new IExternalSignatureContainer() {
@Override
public byte[] sign(InputStream data) throws GeneralSecurityException {
//byte[] hashData = HashUtil.hash(data , "SHA256");
byte signData = null;
signData = signUtil.signP7DetachData(data);
return signData;
}
@Override
public void modifySigningDictionary(PdfDictionary signDic) {
signDic.put(PdfName.Filter, PdfName.Adobe_PPKLite);
signDic.put(PdfName.SubFilter, PdfName.Adbe_pkcs7_detached);
}
};
public void SignMultPDF(byte[] pdfFile , String destPath , String name , String fname , String value){
boolean success = false;
int estimatedSize = 300000;
while (!success) {
try {
PdfReader pdfReader = new PdfReader(new ByteArrayInputStream(pdfFile));
PdfSigner pdfSigner = new PdfSigner(pdfReader, new FileOutputStream(destPath), true);
pdfSigner.signExternalContainer(externalP7DetachSignatureContainer, estimatedSize);
success = true;
} catch (IOException e) {
e.printStackTrace();
estimatedSize += 1000;
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}
}
Above is all my sample code .
And one more question , did I miss to create signature field ?