古詩詞大全網 - 個性簽名 - 如何使用itext對PDF生成疊加簽名

如何使用itext對PDF生成疊加簽名

我有完整代碼

下面是方法,直接調用

public static void sign(String beforePdfpath,String afterPdfpath,String pngpath) throws Exception {

// KeyStore ks = KeyStore.getInstance("pkcs12");

// ks.load(new FileInputStream("demo.p12"),"123456".toCharArray()); //123456為私鑰密碼

// String alias = (String) ks.aliases().nextElement();

// PrivateKey key = (PrivateKey) ks.getKey(alias, "123456".toCharArray());

// Certificate[] chain = ks.getCertificateChain(alias);

Log.d("pdfd", "do-1");

PdfReader reader = new PdfReader(beforePdfpath); //源文件

Log.d("pdfd", "do-1-01");

FileOutputStream fout = new FileOutputStream(afterPdfpath);

Log.d("pdfd", "do-1-1");

PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');

PdfSignatureAppearance sap = stp.getSignatureAppearance();

// sap.setCrypto(key, chain, null, PdfSignatureAppearance.VERISIGN_SIGNED);

Log.d("pdfd", "do-2");

sap.setReason("");

sap.setLocation("");

sap.setContact("/");

Image image = Image.getInstance(pngpath); //使用png格式透明圖片

Log.d("pdfd", "do-3");

sap.setSignatureGraphic(image);

sap.setAcro6Layers(true);

sap.setRenderingMode(RenderingMode.GRAPHIC);

sap.setVisibleSignature(new Rectangle(300, 600, 400, 675), 1, null); //300和600 是對應x軸和y軸坐標

stp.getWriter().setCompressionLevel(5);

Log.d("pdfd", "do-4");

if (stp != null) {

Log.d("pdfd", "do-4-1");

stp.close();

Log.d("pdfd", "do-4-2");

}

Log.d("pdfd", "do-5");

if (fout != null) {

fout.close();

}

Log.d("pdfd", "do-6");

if (reader != null) {

reader.close();

}

Log.d("pdfd", "do-7");

}