因业务需要,做了一个通过Java组件iText生成PDF合同,运行成功了,做个记录,也分享给大家。
首先,我们需要准备好一个有文本域的pdf文件。
1.先用word做出你需要的模板,并保存。
2.通过Adobe Acrobat Pro DC软件打开:文件—创建—从文件创建PDF—选择你的word文件并等待一分钟左右。如果没有Adobe Acrobat Pro DC软件,可以去下载(Adobe Acrobat Pro DC**版下载)。
3.点击“准备表单”—“开始”—“保存”—“确定”
4.“添加文本域”—依次添加文本域,并修改名字—点击保存。
此时,pdf文件就已经准备好了。
然后,我们就开始准备代码了。
代码参考来源:https://blog.csdn.net/u012377333/article/details/51264122
前提:在pom文件里添加项目需要的依赖:
<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>
1.根据你的pdf中需要填的字段创建实体类-Ticket
package com.ymy.model;/*
@author ymy
@DESCRIPTION ${DESCRIPTION}
@create 2019/4/12
*/
public class Ticket {
Integer fileId;
String productName;
String name;
String idCard;
String tel;
String address;
public Ticket() {
}
public Ticket(Integer fileId, String productName, String name, String idCard, String tel, String address) {
this.fileId = fileId;
this.productName = productName;
this.name = name;
this.idCard = idCard;
this.tel = tel;
this.address = address;
}
public Integer getFileId() {
return fileId;
}
public void setFileId(Integer fileId) {
this.fileId = fileId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.创建功能类-PDFTempletTicket
package com.ymy.util;/*
@author ymy
@DESCRIPTION ${DESCRIPTION}
@create 2019/4/12
*/
import com.ymy.model.*;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class PDFTempletTicket {
private String templatePdfPath;
private String ttcPath;
private String targetPdfpath;
private Ticket ticket;
public PDFTempletTicket() {
super();
}
public PDFTempletTicket(String templatePdfPath, String ttcPath,
String targetPdfpath, Ticket ticket) {
this.templatePdfPath= templatePdfPath;
this.ttcPath= ttcPath;
this.targetPdfpath= targetPdfpath;
this.ticket= ticket;
}
public void templetTicket(File file) throws Exception {
PdfReader reader = new PdfReader(templatePdfPath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper ps = new PdfStamper(reader, bos);
/*使用中文字体 */
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
/*BaseFont bf = BaseFont.createFont(PDFTicket.class.getResource("/") + "org/csun/ns/util/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);*/
ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
fontList.add(bf);
AcroFields s = ps.getAcroFields();
s.setSubstitutionFonts(fontList);
s.setField("productName",ticket.getProductName());
s.setField("name",ticket.getName());
s.setField("idCard",ticket.getIdCard());
s.setField("tel",ticket.getTel());
s.setField("address",ticket.getAddress());
ps.setFormFlattening(false);
ps.close();
FileOutputStream fos = new FileOutputStream(file);
fos.write(bos.toByteArray());
fos.close();
}
/**
* @return the templatePdfPath
*/
public String getTemplatePdfPath() {
return templatePdfPath;
}
/**
* @param templatePdfPath the templatePdfPathto set
*/
public void setTemplatePdfPath(String templatePdfPath) {
this.templatePdfPath= templatePdfPath;
}
/**
* @return the ttcPath
*/
public String getTtcPath() {
return ttcPath;
}
/**
* @param ttcPath the ttcPath to set
*/
public void setTtcPath(String ttcPath) {
this.ttcPath= ttcPath;
}
/**
* @return the targetPdfpath
*/
public String getTargetPdfpath() {
return targetPdfpath;
}
/**
* @param targetPdfpath the targetPdfpath toset
*/
public void setTargetPdfpath(String targetPdfpath) {
this.targetPdfpath= targetPdfpath;
}
/**
* @return the ticket
*/
public Ticket getTicket() {
return ticket;
}
/**
* @param ticket the ticket to set
*/
public void setTicket(Ticket ticket) {
this.ticket= ticket;
}
}
3.测试类-TestTempletTicket
package com.ymy.util;/*
@author ymy
@DESCRIPTION ${DESCRIPTION}
@create 2019/4/12
*/
import com.ymy.model.Ticket;
import java.io.File;
public class TestTempletTicket {
public static void main(String[] args) throws Exception {
Ticket ticket = new Ticket();
ticket.setProductName("产品一");
ticket.setName("张三");
ticket.setIdCard("411323166517786546");
ticket.setTel("15723546678");
ticket.setAddress("上海市浦东新区");
PDFTempletTicket pdfTT = new PDFTempletTicket();
pdfTT.setTemplatePdfPath("D:\\file.pdf");//你的pdf模板的位置
pdfTT.setTargetPdfpath("D:\\successFile.pdf");
pdfTT.setTicket(ticket);
File file = new File("D:\\successFile.pdf");
file.createNewFile();
pdfTT.templetTicket(file);
}
}
最后,运行成功出来: