Java生成PDF文档 iText使用PDF模板二

编程入门 行业动态 更新时间:2024-10-28 00:18:19

Java生成PDF文档 iText使用PDF<a href=https://www.elefans.com/category/jswz/34/1770549.html style=模板二"/>

Java生成PDF文档 iText使用PDF模板二

此篇是基于Java生成PDF文档 iText使用PDF模板一 的基础上写的,也都差不多,就是写了2个Demo而已。

如果对您有帮助 ,请多多支持.多少都是您的心意与支持,一分也是爱,再次感谢!!!打开支付宝首页搜“556723462”领红包,领到大红包的小伙伴赶紧使用哦!

 支付宝赞赏:记得点击下面的余额宝,红包可能要大些。

注意:余额宝红包有效期三天(72小时) 在有效期内
余额宝红包使用完或过期才能有机会领取下个余额宝红包,感谢大家的支持!您的支持,我会继续分享更多的文章,欢迎关注!

废话不多说了,基本上是一样的操作:

代码如下:

一:创建maven项目,加入依赖

<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.4.2</version></dependency><!--pdf itext 的jar依赖  --><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

 

二:创建对应pdf模板,这里,就不仔细介绍了,模板图所下:

三:创建对应的实体类如下:

package com.demo3;public class JuanBook {private String id;// 编号private String name;//名称private String shoolName;// 学校名称private String rmb;// 人民币private String data;// 日期public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getShoolName() {return shoolName;}public void setShoolName(String shoolName) {this.shoolName = shoolName;}public String getRmb() {return rmb;}public void setRmb(String rmb) {this.rmb = rmb;}public String getData() {return data;}public void setData(String data) {this.data = data;}@Overridepublic String toString() {return "JuanBook [id=" + id + ", name=" + name + ", shoolName=" + shoolName + ", rmb=" + rmb + ", data=" + data+ "]";}//有参构造public JuanBook(String id, String name, String shoolName, String rmb, String data) {super();this.id = id;this.name = name;this.shoolName = shoolName;this.rmb = rmb;this.data = data;}//无参public JuanBook() {super();}}

四:创建模板:

package com.demo3;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;import com.demo.Ticket;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
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 PDFTempletBook {private String templatePdfPath;//模板路径private String ttcPath;//生成的新路径private String targetPdfpath;//源路径private JuanBook juanBook;//捐赠实体private static final String FONT = "中黑体.ttf";//引入的字体//get与setpublic String getTemplatePdfPath() {return templatePdfPath;}public void setTemplatePdfPath(String templatePdfPath) {this.templatePdfPath = templatePdfPath;}public String getTtcPath() {return ttcPath;}public void setTtcPath(String ttcPath) {this.ttcPath = ttcPath;}public String getTargetPdfpath() {return targetPdfpath;}public void setTargetPdfpath(String targetPdfpath) {this.targetPdfpath = targetPdfpath;}public JuanBook getJuanBook() {return juanBook;}public void setJuanBook(JuanBook juanBook) {this.juanBook = juanBook;}//无参构造public PDFTempletBook() {super();}//有参构造public PDFTempletBook(String templatePdfPath, String ttcPath, String targetPdfpath, JuanBook juanBook) {super();this.templatePdfPath = templatePdfPath;this.ttcPath = ttcPath;this.targetPdfpath = targetPdfpath;this.juanBook = juanBook;}// 使用中文字体 Font f1 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//获取public void templetTicket(File file) throws Exception {//创建一个pdf读取对象PdfReader reader = new PdfReader(templatePdfPath);//创建一个输出流ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建pdf模板,参数reader  bosPdfStamper ps = new PdfStamper(reader, bos);//封装数据,取出模板中的所有字段数据,读取文本域AcroFields s = ps.getAcroFields();s.setField("id", juanBook.getId());s.setField("name", juanBook.getName());s.setField("shoolname", juanBook.getShoolName());s.setField("rmb", juanBook.getRmb());s.setField("data", juanBook.getData());ps.setFormFlattening(true);//必须要调用这个,否则文档不会生成的ps.close();//关闭PdfStamperFileOutputStream fos = new FileOutputStream(file);//创建文件输出流fos.write(bos.toByteArray());//写入数据fos.close();//关闭输出流}
}

五:测试代码效果:

package com.demo3;import java.io.File;public class TestDemo {public static void main(String[] args) throws Exception {//捐赠对象JuanBook juanBook = new JuanBook();juanBook.setId("518518666");juanBook.setName("中国人民");juanBook.setShoolName("中国大学人民的大学");juanBook.setRmb("50000亿");juanBook.setData("2018年8月8日");//模板对象PDFTempletBook Ticketpdf = new PDFTempletBook();//模板源路径Ticketpdf.setTemplatePdfPath("D:\\pdfDemo\\捐赠证书.pdf");//设置tarGet的路径,这个是项目中的源路径//Ticketpdf.setTargetPdfpath("D:\\a.pdf");//set封装捐赠对象信息Ticketpdf.setJuanBook(juanBook);//指定生成的新路径File file = new File("D:\\pdfDemo\\4.pdf");//创建文件file.createNewFile();//使生成的文件file生效,这个必须有Ticketpdf.templetTicket(file);}
}

六:结果如下:

更多推荐

Java生成PDF文档 iText使用PDF模板二

本文发布于:2024-03-10 12:31:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1727986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   文档   Java   PDF   iText

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!