我正在为现有的pdf添加数字签名,我需要建议如何让矩形坐标将签名附加到从最后一页的左边0开始的新行,如果页面已满,则添加新页面。 我可以确定最后一页是否有签名空间吗?这样我就可以决定是添加新页面还是添加到最后一页。 我的pdf将由多个用户在不同时间签名,因此我的计划是在最后一页的底部添加第一个签名,然后在他们进入时添加新签名。 我使用Itext进行数字签名,需要建议将签名附加到pdf。
public class Signatures {
static void signPdf(SignatureDTO signatureDTO) throws Exception{
KeyStore ks = KeyStore.getInstance("pkcs12", "BC");
ks.load(new FileInputStream(signatureDTO.pks), signatureDTO.pwd.toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias, signatureDTO.pwd.toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader(signatureDTO.src);
FileOutputStream os = new FileOutputStream(signatureDTO.dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason(signatureDTO.reason);
appearance.setLocation(signatureDTO.location);
appearance.setVisibleSignature(signatureDTO.rectangle, signatureDTO.page, signatureDTO.fieldName);
ExternalSignature es = new PrivateKeySignature(pk, "SHA-256", "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, es, chain, null, null, null, 0, CryptoStandard.CMS);
}
static class SignatureDTO {
String src,dest,pks,pwd, reason,location;
Rectangle rectangle;
int page;
String fieldName;
}
public static void main(String[] args)
throws Exception {
Security.addProvider(new BouncyCastleProvider());
SignatureDTO signatureDTO= new SignatureDTO();
signatureDTO.dest="resources/OCD-Final-signed1.pdf";
signatureDTO.src="resources/OCD-Final.pdf";
signatureDTO.fieldName="sign1";
signatureDTO.location="VA";
signatureDTO.reason="Approval";
signatureDTO.pks="resources/test1.pfx";
signatureDTO.pwd="test123";
signatureDTO.page=5;
signatureDTO.rectangle= new Rectangle(72, 732, 144, 780);
signPdf(signatureDTO);
signatureDTO.src=signatureDTO.dest;
signatureDTO.dest="resources/OCD-Final-signed2.pdf";
signatureDTO.pks="resources/test5657.pfx";
signatureDTO.fieldName="sign2";
signatureDTO.rectangle= new Rectangle(160, 732, 232, 780);
signPdf(signatureDTO);
}
}
我试过这个,找到与我要找的文字匹配的TextRenderInfo对象,但是TextRenderInfo没有任何坐标
PdfReader reader = new PdfReader(signatureDTO.src);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
TextMarginFinder finder;
finder= parser.processContent(5, new TextMarginFinder(){
@Override
public void renderText(TextRenderInfo renderInfo) {
super.renderText(renderInfo);
if(renderInfo.getText().equals("Approving Official")){
textRenderInfos.add(renderInfo);
}
}
});
请建议我从TextRenderInfo
获取坐标基于文本
查找坐标的代码PdfReader reader = new PdfReader(signatureDTO.src);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
parser.processContent(5, new TextMarginFinder(){
@Override
public void renderText(TextRenderInfo renderInfo) {
super.renderText(renderInfo);
if(mathStr.contains(renderInfo.getText())){
textRenderInfos.put(renderInfo.getText(),renderInfo);
System.out.println(renderInfo.getBaseline().getStartPoint().get(Vector.I1));
System.out.println(renderInfo.getBaseline().getEndPoint().get(Vector.I2));
System.out.println(renderInfo.getBaseline().getEndPoint().get(Vector.I3));
}
}