Java使用itext根据已有的PDF模板进行填充并生成新PDF

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

Java使⽤itext根据已有的PDF模板进⾏填充并⽣成新PDF 使⽤itext 填充已有的PDF模板⽣成新的PDF
1. POM⽂件添加
<!-- itextpdf 依赖包-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
2. 新建PdfUtils⼯具类
import Document;
import pdf.*;
import s.io.FileUtils;
import s.io.IOUtils;
import s.lang3.StringUtils;
import java.io.*;
import java.util.*;
public class PdfUtils {
/
**
* @param map 需要填充的字段
* @param sourceFile  原⽂件路径
* @param targetFile  ⽬标⽂件路径
* @throws IOException
*/
public static void genPdf(HashMap map, String sourceFile, String targetFile)throws IOException {
照片的故事
File templateFile =new File(sourceFile);
fillParam(map, adFileToByteArray(templateFile), targetFile);
}
/**
* 使⽤map中的参数填充pdf,map中的key和pdf表单中的field对应
*/
用望组词public static void fillParam(Map<String, String> fieldValueMap,byte[] file, String contractFileName){
FileOutputStream fos = null;
try{
fos =new FileOutputStream(contractFileName);5号病
PdfReader reader = null;
PdfStamper stamper = null;
BaFont ba = null;
try{
reader =new PdfReader(file);
stamper =new PdfStamper(reader, fos);
stamper.tFormFlattening(true);
//ba = ateFont("STSong-Light", "UniGB-UCS2-H", BaFont.NOT_EMBEDDED); //简体中⽂字体
ba = ateFont("MHei-Medium","UniCNS-UCS2-H", BaFont.NOT_EMBEDDED);//繁体中⽂字体
AcroFields acroFields = AcroFields();
for(String key : Fields().keySet()){
acroFields.tFieldProperty(key,"textfont", ba, null);
acroFields.tFieldProperty(key,"textsize",new Float(9), null);//字体⼤⼩
}
if(fieldValueMap != null){
for(String fieldName : fieldValueMap.keySet()){
if(StringUtils.(fieldName))){
//获取map中key对应的Value是否为On,若是则勾选复选框
(fieldName).equals("On")|| (fieldName)=="On"){
acroFields.tField(fieldName, (fieldName),true);
}el{
期中目标
acroFields.tField(fieldName, (fieldName));
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(stamper != null){
try{
stamper.clo();
}catch(Exception e){
e.printStackTrace();
}
}
if(reader != null){
reader.clo();
}
}
}catch(Exception e){
System.out.println("填充参数异常");
e.printStackTrace();
}finally{
IOUtils.cloQuietly(fos);
}
}
/**
天天燥
* 获取pdf表单中的fieldNames
*/
public static Set<String>getTemplateFileFieldNames(String pdfFileName){
Set<String> fieldNames =new TreeSet<String>();
PdfReader reader = null;
try{
reader =new PdfReader(pdfFileName);
Set<String> keys = AcroFields().getFields().keySet();
for(String key : keys){
int lastIndexOf = key.lastIndexOf(".");
int lastIndexOf2 = key.lastIndexOf("[");
fieldNames.add(key.substring(lastIndexOf !=-1? lastIndexOf +1:0, lastIndexOf2 !=-1? lastIndexOf2 : key.length()));
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(reader != null){
reader.clo();
}
}
return fieldNames;
}
/**
* 读取⽂件数组
*/
public static byte[]fileBuff(String filePath)throws IOException {
File file =new File(filePath);
long fileSize = file.length();
long fileSize = file.length();
if(fileSize > Integer.MAX_VALUE){
//System.out.println("file ");
return null;
}
FileInputStream fi =new FileInputStream(file);
byte[] file_buff =new byte[(int) fileSize];
int offt =0;
int numRead =0;
while(offt < file_buff.length &&(numRead = fi.read(file_buff, offt, file_buff.length - offt))>=0){            offt += numRead;
}
// 确保所有数据均被读取
if(offt != file_buff.length){
throw new IOException("Could not completely read file "+ Name());
}
fi.clo();
return file_buff;
}
/**
* 合并pdf
*/
public static void mergePdfFiles(String[] files, String savepath){
Document document = null;
try{
document =new Document();//默认A4⼤⼩
PdfCopy copy =new PdfCopy(document,new FileOutputStream(savepath));
document.open();
for(int i =0; i < files.length; i++){
PdfReader reader = null;
旁轴相机try{
reader =new PdfReader(files[i]);
沈红霞
int n = NumberOfPages();
for(int j =1; j <= n; j++){
PdfImportedPage page = ImportedPage(reader, j);
聊天记录导出copy.addPage(page);
}
}finally{
if(reader != null){
reader.clo();
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
//关闭PDF⽂档流,OutputStream⽂件输出流也将在PDF⽂档流关闭⽅法内部关闭
if(document != null){
document.clo();
}
}
}
}
3. 实现代码
public static void main(String[] args)throws IOException {
//Map中Key对应PDF表单中的fieldNames,Value则是你想填充的值        HashMap map =new HashMap<String, String>();
map.put("name","杨杰");
map.put("xMan","On");//Value为On 则勾选这个复选框
map.put("age","27");
map.put("phone","15521331");
map.put("email","");
String sourceFile ="D:\\proposal.pdf";//原⽂件路径
String targetFile ="D:\\test.pdf";//⽬标⽂件路径
}

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

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

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

标签:填充   对应   读取   数据   关闭
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图