itextword加公章 java_新手如何使用itext7生成pdf盖章
一開始,我使用的是itext2.1.7,但是會出現(xiàn)中文不能顯示問題,解決方案:下載另一個包extrajars-2.3.zip,此包中的itext-asian.jar可以幫助解決中文亂碼問題,若再遇到STSong-Light' with 'UniGB-UCS2-H' is not recognized問題,可以點(diǎn)擊鏈接進(jìn)行解決。如若再無解決,請放棄itext2.1.7直接用itext7吧,itext7中自帶解決中文顯示問題。具體如下:
一、使用的工具
1.編譯環(huán)境:eclipse mars2
2.運(yùn)行環(huán)境:jdk1.7
3.itext包 :itext7.0.2
3.相關(guān)jar包:bcprov-jdk15on-157.jar,bcpkix-jdk15on-157.jar?(這兩個一定要有,不然會報錯,其他包看情況而定,參考鏈接里面JDK 1.5 - JDK 1.8)
二、生成test.keystore文件
如下圖(名字可以隨便取):
三、代碼前準(zhǔn)備
1.先把生成好的 test.keystore文件放入 jdk1.7\bin目錄下
2.把 bcprov-jdk15on-157.jar放入 jdk1.7\jre\lib\ext目錄下
3.配置安全屬性文件:在 jdk1.7\jre\lib\security目錄下有個java.security文件,用它定義了當(dāng)前可以使用的加密提供者。如看到下面的語句:
security.provider.1=sun.security.provider.Sun
security.provider.2=sun.security.rsa.SunRsaSign
它表明本虛擬機(jī)有兩個加密提供者以及他們的優(yōu)先級和訪問時使用的名稱。當(dāng)需要用到一個加密算法時,虛擬機(jī)會依次訪問這里列出的提供者,尋找想要的算法,
并按這里的優(yōu)先級順序使用第一個找到的算法。我們應(yīng)該在文件中插入新的提供者例如:
security.provider.3=org.bouncycastle.jce.provider.BouncyCastleProvider
例如下圖:
4.先創(chuàng)建一個java工程,導(dǎo)入jar包,如下圖(紅色框內(nèi)為后面運(yùn)行時報出異常后添加的包)
四、代碼編寫
importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.security.GeneralSecurityException;importjava.security.KeyStore;importjava.security.PrivateKey;importjava.security.cert.Certificate;importjavax.swing.JOptionPane;importorg.bouncycastle.jce.provider.BouncyCastleProvider;importcom.itextpdf.io.image.ImageData;importcom.itextpdf.io.image.ImageDataFactory;importcom.itextpdf.kernel.geom.PageSize;importcom.itextpdf.kernel.geom.Rectangle;importcom.itextpdf.kernel.pdf.PdfDocument;importcom.itextpdf.kernel.pdf.PdfReader;importcom.itextpdf.layout.element.Image;importcom.itextpdf.signatures.BouncyCastleDigest;importcom.itextpdf.signatures.DigestAlgorithms;importcom.itextpdf.signatures.IExternalDigest;importcom.itextpdf.signatures.IExternalSignature;importcom.itextpdf.signatures.PdfSignatureAppearance;importcom.itextpdf.signatures.PdfSignatureAppearance.RenderingMode;importcom.itextpdf.signatures.PdfSigner;importcom.itextpdf.signatures.PdfSigner.CryptoStandard;importcom.itextpdf.signatures.PrivateKeySignature;public classSignPDF {public static final String KEYSTORE = "E:\\Program Files\\jdk1.7\\bin\\test.keystore";//keystore文件路徑
public static final char[] PASSWORD = "123456".toCharArray(); //keystore密碼
public static final String SRC = "C:\\Users\\xxx\\Desktop\\歡迎信.pdf";//需要蓋章的pdf文件路徑
public static final String DEST = "C:\\Users\\xxx\\Desktop\\歡迎信2.pdf";//蓋章后生產(chǎn)的pdf文件路徑
public static final String stamperSrc = "C:\\Users\\xxx\\Desktop\\self_study\\stamper\\stamper7.png";//印章路徑
public void sign(String src //需要簽章的pdf文件路徑
, String dest //簽完章的pdf文件路徑
, Certificate[] chain //證書鏈
, PrivateKey pk //簽名私鑰
, String digestAlgorithm //摘要算法名稱,例如SHA-1
, String provider //密鑰算法提供者,可以為null
, CryptoStandard subfilter //數(shù)字簽名格式,itext有2種
, String reason //簽名的原因,顯示在pdf簽名屬性中,隨便填
, String location) //簽名的地點(diǎn),顯示在pdf簽名屬性中,隨便填
throwsGeneralSecurityException, IOException {//下邊的步驟都是固定的,照著寫就行了,沒啥要解釋的
PdfReader reader = newPdfReader(src);
PdfDocument document= newPdfDocument(reader);
document.setDefaultPageSize(PageSize.TABLOID);//目標(biāo)文件輸出流
FileOutputStream os = newFileOutputStream(dest);//創(chuàng)建簽章工具PdfSigner ,最后一個boolean參數(shù)//false的話,pdf文件只允許被簽名一次,多次簽名,最后一次有效//true的話,pdf可以被追加簽名,驗(yàn)簽工具可以識別出每次簽名之后文檔是否被修改
PdfSigner stamper = new PdfSigner(reader, os, true);//獲取數(shù)字簽章屬性對象,設(shè)定數(shù)字簽章的屬性
PdfSignatureAppearance appearance =stamper.getSignatureAppearance();
appearance.setReason(reason);
appearance.setLocation(location);
ImageData img=ImageDataFactory.create(stamperSrc);//讀取圖章圖片,這個image是itext包的image
Image image = newImage(img);float height =image.getImageHeight();float width =image.getImageWidth();//設(shè)置簽名的位置,頁碼,簽名域名稱,多次追加簽名的時候,簽名與名稱不能一樣//簽名的位置,是圖章相對于pdf頁面的位置坐標(biāo),原點(diǎn)為pdf頁面左下角//四個參數(shù)的分別是,圖章左下角x,圖章左下角y,圖章寬度,圖章高度
appearance.setPageNumber(1);
appearance.setPageRect(new Rectangle(350, 100, width, height));//插入蓋章圖片
appearance.setSignatureGraphic(img);//設(shè)置圖章的顯示方式,如下選擇的是只顯示圖章(還有其他的模式,可以圖章和簽名描述一同顯示)
appearance.setRenderingMode(RenderingMode.GRAPHIC);//這里的itext提供了2個用于簽名的接口,可以自己實(shí)現(xiàn),后邊著重說這個實(shí)現(xiàn)//摘要算法
IExternalDigest digest = newBouncyCastleDigest();//簽名算法
IExternalSignature signature = newPrivateKeySignature(pk, digestAlgorithm, BouncyCastleProvider.PROVIDER_NAME);//調(diào)用itext簽名方法完成pdf簽章
stamper.setCertificationLevel(1);
stamper.signDetached(digest,signature, chain,null, null, null, 0, PdfSigner.CryptoStandard.CADES);
}public static voidmain(String[] args) {try{//讀取keystore ,獲得私鑰和證書鏈 jks
KeyStore ks = KeyStore.getInstance("jks");
ks.load(newFileInputStream(KEYSTORE), PASSWORD);
String alias=(String) ks.aliases().nextElement();
PrivateKey pk=(PrivateKey) ks.getKey(alias, PASSWORD);
Certificate[] chain=ks.getCertificateChain(alias);//new一個上邊自定義的方法對象,調(diào)用簽名方法
SignPDF app = newSignPDF();
app.sign(SRC, String.format(DEST,1), chain, pk, DigestAlgorithms.SHA256, null, CryptoStandard.CADES, "Test 1","Ghent");
}catch(Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
e.printStackTrace();
}
}
}
五,生成后的結(jié)果如下圖所示
要蓋章的文檔必須是通過itext已經(jīng)生成完后的文檔,或者蓋章的同時生成文檔,不然章蓋不上去!!
如下圖:
總結(jié)
以上是生活随笔為你收集整理的itextword加公章 java_新手如何使用itext7生成pdf盖章的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OPC介绍
- 下一篇: VML实例-拖动效果