因業務需要,做了一個通過Java組件iText生成PDF合同,運行成功了,做個記錄,也分享給大家。
首先,我們需要准備好一個有文本域的pdf文件。
1.先用word做出你需要的模板,並保存。
2.通過Adobe Acrobat Pro DC軟件打開:文件---創建---從文件創建PDF---選擇你的word文件並等待一分鍾左右。如果沒有Adobe Acrobat Pro DC軟件,可以去下載(http://www.xue51.com/soft/4070.html#xzdz)。
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); } }
最后,運行成功出來: