使用itext框架填充pdf模板

更新时间:2023-07-31 06:28:21 阅读: 评论:0

使⽤itext框架填充pdf模板
所需jar的pom坐标
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>dia</groupId>
<artifactId>jai_codec</artifactId>
<version>1.1-mr</version>
<scope>system</scope>
<systemPath>${project.badir}/src/main/resources/jar/jai_codec-1.1-mr.jar</systemPath>
</dependency>
<dependency>
<groupId&dia</groupId>
<artifactId>jai_core</artifactId>
<version>1.1-mr</version>
<scope>system</scope>
<systemPath>${project.badir}/src/main/resources/jar/jai_core-1.1-mr.jar</systemPath>
</dependency>
<dependency>
<groupId>dialib</groupId>
<artifactId>mlibwrapper_jar</artifactId>
<version>1.1</version>
<scope>system</scope>
<systemPath>${project.badir}/src/main/resources/jar/mlibwrapper_jar-1.1.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.0-RC2</version>
</dependency>
/
**
* pdf填充⽂字信息
*
* @param targetPath  新⽣成的pdf路径
* @param templatePath pdf模板路径
* @param dataMap      key--value
*/
public static void fillPdfWord(String targetPath, String templatePath, Map<String, String> dataMap){
PdfReader reader = null;
ByteArrayOutputStream bos = null;
PdfStamper ps = null;
FileOutputStream fos = null;
try{
//设置字体
BaFont bf = ateFont("c://windows//fonts//,1", BaFont.IDENTITY_H, BaFont.EMBEDDED);
//创建⼀个pdf读取对象,读取pdf模板
reader =new PdfReader(templatePath);
//创建⼀个输出流
bos =new ByteArrayOutputStream();
//创建pdf模板,参数reader  bos
ps =new PdfStamper(reader, bos);
//封装数据,取出模板中的所有字段数据,读取⽂本域
AcroFields form = ps.getAcroFields();
//必须要调⽤这个,否则⽂档不会⽣成的
ps.tFormFlattening(true);
form.addSubstitutionFont(bf);
for(String key : dataMap.keySet()){
String value = (key);
//为指定域,设置字体⼤⼩
form.tFieldProperty(key,"textsize",7f, null);
//为指定域,填充数据
form.tField(key, value);
}
ps.clo();
//指定⽣成的新路径
File file =new File(targetPath);
//创建⽂件
//使⽣成的⽂件file⽣效,这个必须有
//创建⽂件输出流
fos =new FileOutputStream(file);
fos.ByteArray());//写⼊数据
}catch(DocumentException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(fos != null) fos.clo();
if(bos != null) bos.clo();
if(reader != null) reader.clo();
}catch(IOException e){
e.printStackTrace();
}
}
}
/**
* pdf填充图⽚信息
*
* @param targetPath  新⽣成的pdf路径
* @param templatePath pdf模板路径
* @param imgMap      key--value
*/
家用笔记本
public static void fillPdfImage(String targetPath, String templatePath, Map<String, String> imgMap){
PdfReader reader = null;
ByteArrayOutputStream bos = null;
PdfStamper ps = null;
FileOutputStream fos = null;
try{
//设置字体
BaFont bf = ateFont("c://windows//fonts//,1", BaFont.IDENTITY_H, BaFont.EMBEDDED);
//创建⼀个pdf读取对象,读取pdf模板
reader =new PdfReader(templatePath);
//创建⼀个输出流
bos =new ByteArrayOutputStream();
//创建pdf模板,参数reader  bos
ps =new PdfStamper(reader, bos);
//封装数据,取出模板中的所有字段数据,读取⽂本域
放弃反义词AcroFields form = ps.getAcroFields();
//必须要调⽤这个,否则⽂档不会⽣成的
ps.tFormFlattening(true);
form.addSubstitutionFont(bf);
for(String key : imgMap.keySet()){
String imgPath = (key);
int pageNo = FieldPositions(key).get(0).page;
Rectangle signRect = FieldPositions(key).get(0).position;
float x = Left();
float y = Bottom();
/
/ 根据路径读取图⽚
Image image = null;
image = Instance(imgPath);
// 获取图⽚页⾯
PdfContentByte under = ps.getOverContent(pageNo);
// 图⽚⼤⼩⾃适应
image.Width(), Height());
// 添加图⽚
image.tAbsolutePosition(x, y);
under.addImage(image);
}
法律知识宣传
ps.clo();
//指定⽣成的新路径
地球画法
File file =new File(targetPath);
//创建⽂件
//使⽣成的⽂件file⽣效,这个必须有
//创建⽂件输出流
fos =new FileOutputStream(file);
fos.ByteArray());//写⼊数据
}catch(DocumentException e){
笔记本闪屏e.printStackTrace();
关于母亲的故事
}catch(IOException e){
e.printStackTrace();
混沌的包法
}finally{
try{
if(fos != null) fos.clo();外汇什么意思
if(bos != null) bos.clo();
if(reader != null) reader.clo();
}catch(IOException e){
e.printStackTrace();
}
}
}

本文发布于:2023-07-31 06:28:21,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1102792.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:读取   模板   创建   笔记本
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图