Java如何使用itext向PDF插入數(shù)據(jù)和圖片
一、下載Adobe Acrobat DC

二、制作模板
1、準(zhǔn)備一個(gè)word模板,并轉(zhuǎn)換成PDF格式

2、使用Adobe Acrobat DC打開PDF文檔,并在右側(cè)搜索框搜索表單,點(diǎn)擊準(zhǔn)備表單

3、點(diǎn)擊開始,制作PDF表單

4、掃描完成后如下圖,藍(lán)白色框就是可編輯表單

5、點(diǎn)擊表單編輯表單名稱以及插入時(shí)的字體大小樣式等,如果沒有設(shè)置字體大小,數(shù)據(jù)寫入時(shí)會(huì)將字體會(huì)根據(jù)表單的高度自適應(yīng)大小


6、編輯完成后保存PDF模板
三、在Java中使用
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import java.io.*;
import java.nio.file.Files;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
public class PDFUtil {
private static void insertImage(AcroFields form, PdfStamper stamper, String filedName, String url) throws IOException, DocumentException {
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);
// 根據(jù)域的大小縮放圖片
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加圖片
image.setAbsolutePosition(x, y);
under.addImage(image);
}
public static Boolean pdfTemplateInsert(String templateUrl, String outputFileUrl, Map<String, Object> templateValueMap, Map<String, String> templateImageMap) {
boolean success = true;
OutputStream os = null;
PdfStamper ps = null;
PdfReader reader = null;
try {
os = Files.newOutputStream(new File(outputFileUrl).toPath());
//讀取pdf表單
reader = new PdfReader(templateUrl);
//根據(jù)表單生成一個(gè)新的pdf文件
ps = new PdfStamper(reader, os);
//獲取pdf表單
AcroFields form = ps.getAcroFields();
//給表單中添加中文字體
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
form.addSubstitutionFont(bf);
if (null != templateValueMap && !templateValueMap.isEmpty()) {
for (String key : templateValueMap.keySet()) {
form.setField(key, String.valueOf(templateValueMap.get(key)));
}
}
if (null != templateImageMap && !templateImageMap.isEmpty()) {
for (String key : templateImageMap.keySet()) {
insertImage(form, ps, key, templateImageMap.get(key));
}
}
ps.setFormFlattening(true);
} catch (Exception e) {
success = false;
} finally {
try {
ps.close();
reader.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return success;
}
public static void main(String[] args) {
//模板路徑
String inputUrl = "E:/Desktop/test/demo/itext-demo.pdf";
//生成的文件路徑
String outputUrl = "E:/Desktop/test/demo/itext-demo-output.pdf";
Map<String, Object> data = new HashMap<>();
data.put("name", "張山");
data.put("year", "2000年10月");
data.put("phone", "13032451234");
data.put("gangwei", "Java開發(fā)");
data.put("introduceOneself", "熟練使用Java常用框架:Spring 、mybatis等");
//圖片地址
String imageUrl = "E:/Desktop/test/demo/1.jpeg";
Map<String, String> templateImageMap = new HashMap<>();
templateImageMap.put("image", imageUrl);
PDFUtil.pdfTemplateInsert(inputUrl, outputUrl, data, templateImageMap);
}
}
執(zhí)行結(jié)果

總結(jié)
到此這篇關(guān)于Java如何使用itext向PDF插入數(shù)據(jù)和圖片的文章就介紹到這了,更多相關(guān)Java向PDF插入數(shù)據(jù)圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot加載配值文件的實(shí)現(xiàn)步驟
本文主要介紹了springboot加載配值文件的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
mybatis/mybatis-plus模糊查詢語句特殊字符轉(zhuǎn)義攔截器的實(shí)現(xiàn)
在開發(fā)中,我們通常會(huì)遇到這樣的情況。用戶在錄入信息是錄入了‘%’,而在查詢時(shí)無法精確匹配‘%’。究其原因,‘%’是MySQL的關(guān)鍵字,如果我們想要精確匹配‘%’,那么需要對其進(jìn)行轉(zhuǎn)義,本文就詳細(xì)的介紹一下2021-11-11
詳解Java程序啟動(dòng)時(shí)-D指定參數(shù)是什么
java服務(wù)啟動(dòng)的時(shí)候,都會(huì)指定一些參數(shù),下面這篇文章主要給大家介紹了關(guān)于Java程序啟動(dòng)時(shí)-D指定參數(shù)是什么的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Java中system.exit(0) 和 system.exit(1)區(qū)別
本文主要介紹了Java中system.exit(0) 和 system.exit(1)區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05
MyBatis批量插入(insert)數(shù)據(jù)操作
本文給大家分享MyBatis批量插入(insert)數(shù)據(jù)操作知識,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06
ElasticSearch學(xué)習(xí)之ES Mapping實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了ElasticSearch學(xué)習(xí)之ES Mapping實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
SpringCloud輪詢拉取注冊表與服務(wù)發(fā)現(xiàn)流程詳解
這篇文章主要介紹了SpringCloud輪詢拉取注冊表與服務(wù)發(fā)現(xiàn),現(xiàn)在很多創(chuàng)業(yè)公司都開始往springcloud靠了,可能是由于文檔和組件比較豐富的原因吧,畢竟是一款目前來說比較完善的微服務(wù)架構(gòu)2022-11-11
MyBatisPlus唯一索引批量新增或修改的實(shí)現(xiàn)方法
本文主要介紹了MyBatisPlus唯一索引批量新增或修改的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03

