码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-04-29 16:35:12      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:

我们系统需要生成一个可以打印的PDF文档,老板给了我一个Word文档,按照这个Word文档的格式生成PDF文档。

第一步:下载AdobeAcrobat DC,必须使用这个来制作from域。

第二步:使用AdobeAcrobat DC将Word导成PDF文档。

第三步:由于还要加水印的效果,所以还是使用AdobeAcrobat DC来添加水印,非常方便;

    添加水印的方法:使用AdobeAcrobat DC打开PDF文档,“工具”-》“编辑PDF”-》”水印”-》”添加”

技术分享

添加水印的操作:

技术分享

点击“确定”:

技术分享

第四步:使用AdobeAcrobat DC添加From域;

添加From域方法:使用AdobeAcrobat DC打开文档,“工具”-》“准备表单”

技术分享

点击“开始”:

技术分享

点击“保存”:

技术分享技术分享

添加“文本域”到我们想要添加内容的位置:

技术分享

第五步:使用Java代码导出PDF文档;

主要三个类:功能类-PDFTempletTicket

/**
 *@Title: PDFTempletTicket.java
 *@Package: org.csun.ns.util
 *@Description: TODO
 *@Author: chisj chisj@foxmail.com
 *@Date: 2016年4月27日上午11:29:52
 *@Version V1.0
 */
package org.csun.ns.util;
 
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
 
import org.csun.ns.entity.Ticket;
 
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
 
/**
 *@ClassName: PDFTempletTicket
 *@Description: TODO
 *@Author: chisj chisj@foxmail.com
 *@Date: 2016年4月27日上午11:29:52
 */
public class PDFTempletTicket {
 
         privateString templatePdfPath;
         privateString ttcPath;
         privateString targetPdfpath;
         privateTicket ticket;
        
        
         publicPDFTempletTicket() {
                   super();
         }
        
         publicPDFTempletTicket(String templatePdfPath, String ttcPath,
                            StringtargetPdfpath, Ticket ticket) {
                   this.templatePdfPath= templatePdfPath;
                   this.ttcPath= ttcPath;
                   this.targetPdfpath= targetPdfpath;
                   this.ticket= ticket;
         }
        
         publicvoid templetTicket(File file) throws Exception {
                  
                   PdfReaderreader = new PdfReader(templatePdfPath);
                   ByteArrayOutputStreambos = new ByteArrayOutputStream();
                   PdfStamperps = new PdfStamper(reader, bos);
                  
                   /*使用中文字体 */ 
                   BaseFontbf = BaseFont.createFont(PDFTicket.class.getResource("/") +"org/csun/ns/util/simsun.ttc,1", 
                BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
       ArrayList<BaseFont> fontList = newArrayList<BaseFont>(); 
       fontList.add(bf); 
                  
                   AcroFieldss = ps.getAcroFields();
                   s.setSubstitutionFonts(fontList);
 
                   s.setField("ticketId",ticket.getTicketId());
                   s.setField("ticketCreateTime",ticket.getTicketCreateTime());
                   s.setField("ticketCompany",ticket.getTicketCompany());
                   s.setField("sysName",ticket.getSysName());
                   s.setField("moneyLittle",ticket.getMoneyLittle());
                   s.setField("moneyBig",ticket.getMoneyBig());
                   s.setField("accountCompany",ticket.getAccountCompany());
                   s.setField("bedNumber",ticket.getBedNumber());
                   s.setField("username",ticket.getUsername());
                   s.setField("password",ticket.getPassword());
                  
                  
                   ps.setFormFlattening(true);
                   ps.close();
                  
                   FileOutputStreamfos = new FileOutputStream(file);
                   fos.write(bos.toByteArray());
                   fos.close();
         }
 
         /**
          * @return the templatePdfPath
          */
         publicString getTemplatePdfPath() {
                   returntemplatePdfPath;
         }
 
         /**
          * @param templatePdfPath the templatePdfPathto set
          */
         publicvoid setTemplatePdfPath(String templatePdfPath) {
                   this.templatePdfPath= templatePdfPath;
         }
 
         /**
          * @return the ttcPath
          */
         publicString getTtcPath() {
                   returnttcPath;
         }
 
         /**
          * @param ttcPath the ttcPath to set
          */
         publicvoid setTtcPath(String ttcPath) {
                   this.ttcPath= ttcPath;
         }
 
         /**
          * @return the targetPdfpath
          */
         publicString getTargetPdfpath() {
                   returntargetPdfpath;
         }
 
         /**
          * @param targetPdfpath the targetPdfpath toset
          */
         publicvoid setTargetPdfpath(String targetPdfpath) {
                   this.targetPdfpath= targetPdfpath;
         }
 
         /**
          * @return the ticket
          */
         publicTicket getTicket() {
                   returnticket;
         }
 
         /**
          * @param ticket the ticket to set
          */
         publicvoid setTicket(Ticket ticket) {
                   this.ticket= ticket;
         }
        
        
}

数据类-Ticket

/**
 *@Title: Ticket.java
 *@Package: org.csun.ns.entity
 *@Description: TODO
 *@Author: chisj chisj@foxmail.com
 *@Date: 2016年4月26日下午7:43:31
 *@Version V1.0
 */
package org.csun.ns.entity;
 
/**
 *@ClassName: Ticket
 *@Description: TODO
 *@Author: chisj chisj@foxmail.com
 *@Date: 2016年4月26日下午7:43:31
 */
public class Ticket {
 
         privateString ticketId;
         privateString homesId;
         privateString ticketCreateTime;
         privateString ticketCompany;
         privateString sysName;
         privateString moneyLittle;
         privateString moneyBig;
         privateString accountCompany;
         privateString bedNumber;
         privateString username;
         privateString password;
 
         publicTicket() {
                   super();
         }
        
         publicTicket(String ticketId, String homesId, String ticketCreateTime,
                            StringticketCompany, String sysName, String moneyLittle,
                            StringmoneyBig, String accountCompany, String bedNumber,
                            Stringusername, String password) {
                   this.ticketId= ticketId;
                   this.homesId= homesId;
                   this.ticketCreateTime= ticketCreateTime;
                   this.ticketCompany= ticketCompany;
                   this.sysName= sysName;
                   this.moneyLittle= moneyLittle;
                   this.moneyBig= moneyBig;
                   this.accountCompany= accountCompany;
                   this.bedNumber= bedNumber;
                   this.username= username;
                   this.password= password;
         }
 
         /**
          * @return the ticketId
          */
         publicString getTicketId() {
                   returnticketId;
         }
 
         /**
          * @param ticketId the ticketId to set
          */
         publicvoid setTicketId(String ticketId) {
                   this.ticketId= ticketId;
         }
 
         /**
          * @return the homesId
          */
         publicString getHomesId() {
                   returnhomesId;
         }
 
         /**
          * @param homesId the homesId to set
          */
         publicvoid setHomesId(String homesId) {
                   this.homesId= homesId;
         }
 
         /**
          * @return the ticketCreateTime
          */
         publicString getTicketCreateTime() {
                   returnticketCreateTime;
         }
 
         /**
          * @param ticketCreateTime the ticketCreateTimeto set
          */
         publicvoid setTicketCreateTime(String ticketCreateTime) {
                   this.ticketCreateTime= ticketCreateTime;
         }
 
         /**
          * @return the ticketCompany
          */
         publicString getTicketCompany() {
                   returnticketCompany;
         }
 
         /**
          * @param ticketCompany the ticketCompany toset
          */
         publicvoid setTicketCompany(String ticketCompany) {
                   this.ticketCompany= ticketCompany;
         }
 
         /**
          * @return the sysName
          */
         publicString getSysName() {
                   returnsysName;
         }
 
         /**
          * @param sysName the sysName to set
          */
         publicvoid setSysName(String sysName) {
                   this.sysName= sysName;
         }
 
         /**
          * @return the moneyLittle
          */
         publicString getMoneyLittle() {
                   returnmoneyLittle;
         }
 
         /**
          * @param moneyLittle the moneyLittle to set
          */
         publicvoid setMoneyLittle(String moneyLittle) {
                   this.moneyLittle= moneyLittle;
         }
 
         /**
          * @return the moneyBig
          */
         publicString getMoneyBig() {
                   returnmoneyBig;
         }
 
         /**
          * @param moneyBig the moneyBig to set
          */
         publicvoid setMoneyBig(String moneyBig) {
                   this.moneyBig= moneyBig;
         }
 
         /**
          * @return the accountCompany
          */
         publicString getAccountCompany() {
                   returnaccountCompany;
         }
 
         /**
          * @param accountCompany the accountCompany toset
          */
         publicvoid setAccountCompany(String accountCompany) {
                   this.accountCompany= accountCompany;
         }
 
         /**
          * @return the bedNumber
          */
         publicString getBedNumber() {
                   returnbedNumber;
         }
 
         /**
          * @param bedNumber the bedNumber to set
          */
         publicvoid setBedNumber(String bedNumber) {
                   this.bedNumber= bedNumber;
         }
 
         /**
          * @return the username
          */
         publicString getUsername() {
                   returnusername;
         }
 
         /**
          * @param username the username to set
          */
         publicvoid setUsername(String username) {
                   this.username= username;
         }
 
         /**
          * @return the password
          */
         publicString getPassword() {
                   returnpassword;
         }
 
         /**
          * @param password the password to set
          */
         publicvoid setPassword(String password) {
                   this.password= password;
         }
        
}

测试类-TestTempletTicket

/**
 *@Title: TestTempletTicket.java
 *@Package: org.csun.ns.util
 *@Description: TODO
 *@Author: chisj chisj@foxmail.com
 *@Date: 2016年4月27日下午1:31:23
 *@Version V1.0
 */
package org.csun.ns.util;
 
import java.io.File;
 
import org.csun.ns.client.OSSConfigure;
import org.csun.ns.client.OSSUtil;
import org.csun.ns.entity.Ticket;
 
/**
 *@ClassName: TestTempletTicket
 *@Description: TODO
 *@Author: chisj chisj@foxmail.com
 *@Date: 2016年4月27日下午1:31:23
 */
public class TestTempletTicket {
 
         publicstatic void main(String[] args) throws Exception {
                  
                   Ticketticket = new Ticket();
                  
                   ticket.setTicketId("2016042710000");
                   ticket.setTicketCreateTime("2016年4月27日");
                   ticket.setTicketCompany("武汉日创科技有限公司");
                   ticket.setSysName("智能看护系统");
                   ticket.setMoneyLittle("50,000.00");
                   ticket.setMoneyBig("伍万元整");
                   ticket.setAccountCompany("洪山福利院");
                   ticket.setBedNumber("500床位");
                   ticket.setUsername("qiu");
                   ticket.setPassword("123456");
                  
                   PDFTempletTicketpdfTT = new PDFTempletTicket();
                  
                  pdfTT.setTemplatePdfPath("D:\\ticket_from.pdf");
                   pdfTT.setTargetPdfpath("D:\\aaabbbccc.pdf");
                   pdfTT.setTicket(ticket);
                  
                   Filefile = new File("D:\\aaabbbccc.pdf");
                   file.createNewFile();
                   pdfTT.templetTicket(file);
                  
                   //OSSConfigureconfig = OSSUtil.getOSSConfigure();
       //OSSManageUtil.uploadFile(config, file, "aaabbbccc.pdf","pdf", "ticket/" + "aaabbbccc");
       
       //System.out.println("path = " + config.getAccessUrl());
                  
         }
        
}

导出来后的结果:

技术分享

备注:导出PDF文档,From域的内容不可见问题;iText找不到字体;

问题1:我开始导出的From域内容看不到,将ps.setFormFlattening(true);设置为flase后,可以看到From域,还是看不到内容,我点击进入From后可以看到内容,该方法是将From域隐藏;后来看到有人设置From域内容的字体:BaseFontbf = BaseFont.createFont(PDFTicket.class.getResource("/") + "org/csun/ns/util/simsun.ttc,1"

                BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

以及:

s.setSubstitutionFonts(fontList);

问题2:别人的代码:

BaseFont bf = BaseFont.createFont("STSong-Light""UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

会报找不到字体的错误,这里的解决方式就是通过使用自己的字体就好,simsun.ttc是Windows下面自带的字体(简体宋体:C:\Windows\Fonts下面有很多字体可以使用)

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

标签:

原文地址:http://blog.csdn.net/u012377333/article/details/51264122

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!