1.下载adobe9.0.0
2.选择“文档”-->“安全设置”
3.选择添加添加身份证
代码:
1.添加依赖:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.54</version>
</dependency>
2.代码:
public static void makeSignature(String src, String dest) {
//签名文件位置
String KEYSTORE = "d:\\zwh.pfx";
//密码
String PASSWORD = "123456";
//数字签名
String signName = "";
//签名原因
String reason = "签名防伪防修改";
//位置
String location = "广东省";
//联系方式
String contact = "1388888888";
//数字签名位置x,距左
float x = 0L;
//数字签名位置y,距底
float y = 0L;
//数字签名宽
float width = 10L;
//数字签名高
float height = 10L;
//签名页码
int page = 1;
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
KeyStore ks = null;
String alias = null;
PrivateKey pk = null;
Certificate[] chain = null;
try {
ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream(KEYSTORE), PASSWORD.toCharArray());
} catch (Exception e) {
e.printStackTrace();
}
try {
alias = (String)ks.aliases().nextElement();
pk = (PrivateKey) ks.getKey(alias, PASSWORD.toCharArray());
chain = ks.getCertificateChain(alias);
} catch (Exception e) {
e.printStackTrace();
}
try {
PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
//PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true);
//在一份PDF文档中嵌入多个数字证书请参考上面一行注释的代码
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
appearance.setLayer2Font(new Font(bfChinese, 8, Font.NORMAL));
appearance.setLayer2Text(signName);
appearance.setReason(reason);
appearance.setLocation(location); //添加位置信息,可为空
appearance.setContact(contact);
appearance.setVisibleSignature(new Rectangle(x, y, width, height),page, "sig");
ExternalDigest digest = new BouncyCastleDigest();
ExternalSignature signature = new PrivateKeySignature(pk, DigestAlgorithms.SHA256, provider.getName());
MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, MakeSignature.CryptoStandard.CMS);
} catch (Exception e) {
e.printStackTrace();
}
}
3.调用:
@Test
public void test(){
String src = "d:\\MODBUS介绍.pdf";
String test="d:\\MODBUS介绍1.pdf";
makeSignature(src,test);
}
效果:用adobe9打开