java通過(guò)PDF模板填寫PDF表單
本文實(shí)例為大家分享了java通過(guò)PDF模板填寫PDF表單的具體代碼,包括圖片,供大家參考,具體內(nèi)容如下
需要用到的java包:
itext.jar、iTextAsian.jar的JAR包。這個(gè)包里面定義了與中文輸出相關(guān)的一些文件。
編寫的表單如下:

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
/**
* pdf工具類
* @author MOSHUNWEI
* @since 2018-02-01
*/
public class PDFUtil {
/**
* 根據(jù)模板生成pdf
* @param data Map(String,Object)
* @return
*/
public static boolean createPDF(String path,Map<String, Object> data) {
PdfReader reader = null;
AcroFields s = null;
PdfStamper ps = null;
ByteArrayOutputStream bos = null;
try {
reader = new PdfReader("D:\\test.pdf");
bos = new ByteArrayOutputStream();
ps = new PdfStamper(reader, bos);
s = ps.getAcroFields();
/**
* 使用中文字體 使用 AcroFields填充值的不需要在程序中設(shè)置字體,在模板文件中設(shè)置字體為中文字體 Adobe 宋體 std L
*/
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
/**
* 設(shè)置編碼格式
*/
s.addSubstitutionFont(bfChinese);
// 遍歷data 給pdf表單表格賦值
for (String key : data.keySet()) {
s.setField(key,data.get(key).toString());
}
// 如果為false那么生成的PDF文件還能編輯,一定要設(shè)為true
ps.setFormFlattening(true);
/**
* 添加圖片
*/
String imgpath="D:/n5.jpg";
int pageNo = s.getFieldPositions("img").get(0).page;
Rectangle signRect = s.getFieldPositions("img").get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 讀圖片
Image image = Image.getInstance(imgpath);
// 獲取操作的頁(yè)面
PdfContentByte under = ps.getOverContent(pageNo);
// 根據(jù)域的大小縮放圖片
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加圖片
image.setAbsolutePosition(x, y);
under.addImage(image);
@SuppressWarnings("resource")
FileOutputStream fos = new FileOutputStream("d:\\shouju_fb.pdf");
fos.write(bos.toByteArray());
return true;
} catch (IOException | DocumentException e) {
System.out.println("讀取文件異常");
e.printStackTrace();
return false;
}finally {
try {
bos.close();
ps.close();
reader.close();
} catch (IOException | DocumentException e) {
System.out.println("關(guān)閉流異常");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Map<String, Object> data = new HashMap<String, Object>();
data.put("id", "12312321");
data.put("name", "小帥哥");
data.put("sex", "男");
data.put("age", "21");
PDFUtil.createPDF("D:/n5.jpg",data);
}
}
還有相應(yīng)的編輯pdf表單的工具,默認(rèn)用Adobe Acrobat。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中使用SpringSecurity進(jìn)行權(quán)限控制的示例代碼
本文將詳細(xì)介紹如何在Spring Boot應(yīng)用程序中使用Spring Security進(jìn)行權(quán)限控制,我們將探討Spring Security的基本概念,以及如何使用Spring Security實(shí)現(xiàn)認(rèn)證和授權(quán),需要的朋友可以參考下2024-02-02
如何修改nacos權(quán)重報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了如何修改nacos權(quán)重報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
springboot整合@Retryable實(shí)現(xiàn)重試功能的示例代碼
本文主要介紹了springboot整合@Retryable實(shí)現(xiàn)重試功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Java解析Excel文件并把數(shù)據(jù)存入數(shù)據(jù)庫(kù)
本篇文章主要介紹了Java解析Excel文件并把數(shù)據(jù)存入數(shù)據(jù)庫(kù) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解
今天小編就為大家分享一篇關(guān)于Java中字符數(shù)組和字符串與StringBuilder和字符串轉(zhuǎn)換的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Sentinel熱門詞匯限流的實(shí)現(xiàn)詳解
這篇文章主要介紹了使用Sentinel對(duì)熱門詞匯進(jìn)行限流的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
微信開(kāi)發(fā)準(zhǔn)備第二步 springmvc mybatis項(xiàng)目結(jié)構(gòu)搭建
這篇文章主要為大家詳細(xì)介紹了微信開(kāi)發(fā)準(zhǔn)備第二步,springmvc和mybatis項(xiàng)目結(jié)構(gòu)的搭建,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04

