在已有內容的PDF上添加空白數字簽名域
如果想根據PDF模板生成PDF請跳轉
廢話不多說,上代碼,親測可用。
pom.xml
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
整體代碼(裏面的添加內容請參考上述鏈接):
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
/**
* 已有的PDF中添加空白數字簽名域
* @author
*
*/
public class Demo {
public static void main(String[] args){
Map<String, Object> map=fillTemplate();
System.out.println(map.get("pdfs"));
}
public static Map<String, Object> fillTemplate() {
PdfReader reader;
FileOutputStream out;
PdfStamper ps;
Map<String,String> map = new HashMap();
map.put("Text1","HT188888888888");
map.put("Text2","李四");//甲
map.put("Text3","111111111111111111");//乙
map.put("Text4","188888888888");
map.put("Text5","王五");//甲
map.put("Text6","11111111111111111");//乙
map.put("Text7","188888888888");
map.put("Text8","111");//甲
map.put("Text9","2018-11-11");//乙
map.put("Text10","2018-11-18");
map.put("Text11","0.05");//甲
map.put("Text13","阿拉蕾");//乙
map.put("Text14","888.67");
map.put("Text15","李四");//甲
map.put("Text16","王五");
map.put("Text17","2018-11-11");//甲
Map<String,Object> os=new HashMap();
os.put("datemap",map);
Map<String,Object> map2=Model.fillTemplate(os);//添加內容完成了,但是簽名域沒了。
String src="C:/pdf/demo.pdf";//添加空白域完成的PDF
try {
out = new FileOutputStream(src);
reader = new PdfReader(map2.get("pdf")+"");//源文件
ps = new PdfStamper(reader, out);
// 創建數組簽名域 (因爲添加兩個,所以搞了兩個座標系)
int x = 210, y = 437, width = 60, height = 60; // 座標系遠點位於頁面左下角,左下角到右下角爲 x 軸,左下角到左上角爲 y 軸
Rectangle areaSignatureRect = new Rectangle(// 簽名域區域,由兩個對角點構成的矩形區域
x, // 點1 x座標 左邊距
y, // 點1 y座標 上邊距
x+width,// 點2 x座標, 這個最好是左邊距+寬,好調點。
y+height // 點2 y座標, 同樣這個最好是上邊距+高,好調點。(其實我也懵懵懂.)
);
int xs = 210, ys = 290, widths = 60, heights = 60;
Rectangle areaSignatureRect2 = new Rectangle(// 簽名域區域,由兩個對角點構成的矩形區域
xs, // 點1 x座標
ys, // 點1 y座標
xs+widths,// 點2 x座標
ys+heights // 點2 y座標
);
int pageNo = 5; // PDF 文件的頁碼從 1 開始,而不是 0。(這個就是你要在哪個頁面添加簽名域,我的是第五頁)
PdfFormField pdfFormField = PdfFormField.createSignature(ps.getWriter());
pdfFormField.setFieldName("AREA_SIGNATURE"); // 簽名域標識
pdfFormField.setPage(pageNo);
pdfFormField.setWidget(areaSignatureRect, PdfAnnotation.HIGHLIGHT_OUTLINE); // 高亮顯示
PdfFormField pdfFormField2 = PdfFormField.createSignature(ps.getWriter());//創建了兩個(我也不懂,但是這樣就可以)
pdfFormField2.setFieldName("AREA_SIGNATURE2"); // 簽名域標識
pdfFormField2.setPage(pageNo);
pdfFormField2.setWidget(areaSignatureRect2, PdfAnnotation.HIGHLIGHT_OUTLINE); // 高亮顯示
ps.setFormFlattening(true);// 如果爲false,生成的PDF文件可以編輯,如果爲true,生成的PDF文件不可以編輯
ps.addAnnotation(pdfFormField, pageNo);
ps.addAnnotation(pdfFormField2, pageNo);
System.err.println("生成pdf文件完成~~~~~~~~~~");
ps.close();//必須要關閉,要不然生成的PDF會是0KB(此代碼大部分都是網上摘抄的,以爲能用來着。沒寫這個,讓我找大半天)
Map<String, Object> map3=new HashMap<String,Object>();
map3.put("pdfs",src);
return map3;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}