
成果

使用word,製作一個模板

使用pdf編輯器,生成pdf模板。
1.從做好的word匯入來建立pdf


2.插入文字域


3.插入選項域


4.插入圖片域,預插入簽名和公章

注意:外觀這裡的邊框和填充都選無色,否則圖片會被填充色蓋住;

注意:選項這裡我們選擇只顯示標籤。

5.模板做好了,我們進行儲存。

java 實現
引入依賴
<!-- pdf -->
<dependency>
<groupId>
com.itextpdf
</groupId>
<artifactId>
itextpdf
</artifactId>
<version>
5.5.13.2
</version>
</dependency>
<!--中文字型-->
<dependency>
<groupId>
com.itextpdf
</groupId>
<artifactId>
itext-asian
</artifactId>
<version>
5.2.0
</version>
</dependency>
<!--html xml 轉為pdf-->
<dependency>
<groupId>
com.itextpdf.tool
</groupId>
<artifactId>
xmlworker
</artifactId>
<version>
5.5.11
</version>
</dependency>
程式碼實現
package
com.tencent.qcloud.roomservice.webrtc.utils;
import
com.alibaba.fastjson.JSONObject;
import
com.itextpdf.text.Image;
import
com.itextpdf.text.Rectangle;
import
com.itextpdf.text.pdf.*;
import
java.io.ByteArrayOutputStream;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.util.Map;
/**
*
@author
dume
*
@ClassName
PdfTest
*
@description
: pdf模板操作
*
@date
2024年07月26日
*
@version
: 1.0
*/
publicclassPdfTest
{
publicstaticvoidmain(String[] args)
{
//測試執行
FillTemplate(
"C:\\test\\申請表.pdf"
,
"C:\\test\\新申請表.pdf"
,
"杜小七"
,
"遼寧大連"
,
"跑步"
,
"Yes"
,
"Yes"
,
"Yes"
,
"C:\\test\\電子簽名.png"
,
"C:\\test\\公章.png"
);
}
/**
* 根據模板生成pdf
*
@param
sourcesPath 原檔案路徑
*
@param
targetPath 生成檔案路徑
*
@param
name 引數
*
@param
address 引數
*
@param
hobby 引數
*
@param
select_1 選項
*
@param
select_2 選項
*
@param
select_3 選項
*
@param
signPath 簽名圖片路徑
*
@param
gongzhangPath 公章圖片路徑
*/
publicstaticvoidFillTemplate
(
String sourcesPath,
String targetPath,
String name,
String address,
String hobby,
String select_1,
String select_2,
String select_3,
String signPath,
String gongzhangPath
)
{
//設定引數
JSONObject jsonObject =
new
JSONObject();
jsonObject.put(
"name"
,name);
jsonObject.put(
"address"
,address);
jsonObject.put(
"hobby"
,hobby);
jsonObject.put(
"select_1"
,select_1);
jsonObject.put(
"select_2"
,select_2);
jsonObject.put(
"select_3"
,select_3);
// 填充建立pdf
PdfReader reader =
null
;
PdfStamper stamp =
null
;
ByteArrayOutputStream baos =
null
;
try
{
reader =
new
PdfReader( sourcesPath);
File deskFile =
new
File(targetPath);
stamp =
new
PdfStamper(reader,
new
FileOutputStream(deskFile));
// 取出報表模板中的所有欄位
AcroFields form = stamp.getAcroFields();
System.out.println(form.getFields().keySet());
//設定宋體
BaseFont song =BaseFont.createFont(
"STSong-Light"
,
"UniGB-UCS2-H"
, BaseFont.NOT_EMBEDDED);
if
(jsonObject !=
null
) {
for
(Map.Entry<String, Object> entry : jsonObject.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().toString();
//儲存選項
if
(key.startsWith(
"select"
)) {
form.setField(key, value,
true
);
//儲存文字
}
else
{
form.setFieldProperty(key,
"textfont"
, song,
null
);
form.setField(key, value);
}
}
}
//插入簽名
insertImage(form,stamp,
"sign"
,signPath);
//插入公章
insertImage(form,stamp,
"gongzhang"
,gongzhangPath);
//儲存修改
stamp.setFormFlattening(
true
);
}
catch
(Exception e){
e.printStackTrace();
}
finally
{
if
(stamp !=
null
) {
try
{
stamp.close();
}
catch
(Exception e){
e.printStackTrace();
}
}
if
(reader !=
null
) {
try
{
reader.close();
}
catch
(Exception e){
e.printStackTrace();
}
}
if
(baos !=
null
) {
try
{
baos.close();
}
catch
(Exception e){
e.printStackTrace();
}
}
}
}
/**
* pdf模板插入圖片
*
@param
form
*
@param
stamper
*
@param
filedName
*
@param
url
*
@return
*/
publicstaticbooleaninsertImage(AcroFields form, PdfStamper stamper, String filedName, String url)
{
try
{
int
pageNo = form.getFieldPositions(filedName).get(
0
).page;
Rectangle signRect = form.getFieldPositions(filedName).get(
0
).position;
float
x = signRect.getLeft();
float
y = signRect.getBottom();
Image image = Image.getInstance(url);
// 獲取操作的頁面
PdfContentByte under = stamper.getOverContent(pageNo);
// 根據域的大小縮放圖片
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 新增圖片
image.setAbsolutePosition(x, y);
under.addImage(image);
}
catch
(Exception e){
returnfalse
;
}
returntrue
;
}
}
執行測試

