BaseFont.createFont(PDFTicket.class.getResource(\+\ BaseFont.IDENTITY_H,BaseFont.EMBEDDED); ArrayList<BaseFont> fontList =
newArrayList<BaseFont>(); fontList.add(bf); AcroFieldss = ps.getAcroFields(); s.setSubstitutionFonts(fontList); s.setField(\ s.setField(\);
s.setField(\ s.setField(\ s.setField(\ s.setField(\ s.setField(\);
s.setField(\ s.setField(\
s.setField(\ 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[java]view plain copy
/** *@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)
java根据模板生成pdf文件并导出
首先你的制作一个pdf模板:
1.先用word做出模板界面2.文件另存为pdf格式文件3.通过Adobe Acrobat pro软件打开刚刚用word转换成的pdf文件(注:如果没有这个软件可以通过我的百度云下载,链接:http://pan.http://m.njliaohua.com//s/1pL2klzt)如果无法下载可以联系博主。4.点击右边的\准备表单\按钮,选择\测试.pdf\选择开始
进去到编辑页面,打开后它会自动侦测并命名表单域,右键表单域,点击属性,出现文本域属性对话框(其实无需任何操作,一般情况下不需要修改什么东西,至少我没有修改哦。如果你想修改fill1等信息,可以进行修改)5.做完上面的工作后,直接\另存为\将pdf存储就可以
*****************************************************************************
以上部分是制作pdf模板操作,上述完成后,就开始通过程序来根据pdf模板生成pdf文件了,上java程序: 1.首先需要依赖包:itext的jar包,我是maven项目,所以附上maven依赖
[html] view plain copy print?<!--
https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version>
</dependency> [html] view plain copy print?<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian --> <span style=\ </span><dependency> <span style=\ </span> <groupId>com.itextpdf</groupId> <span style=\ </span> <artifactId>itext-asian</artifactId> <span style=\ </span> <version>5.2.0</version> <span style=\
</span></dependency> 2.下面就是生成pdf代码了
[java] view plain copy print?import java.io.ByteArrayOutputStream; import
java.io.FileOutputStream; import java.io.IOException; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.AcroFields; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import
com.itextpdf.text.pdf.PdfStamper; public class Snippet { // 利用模板生成pdf public static void fillTemplate() { // 模板路径 String templatePath = \测试3.pdf\ // 生成的新文件路径 String newPDFPath = \ PdfReader reader; FileOutputStream out; ByteArrayOutputStream bos; PdfStamper stamper; try { out = new FileOutputStream(newPDFPath);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板 bos = new ByteArrayOutputStream(); stamper = new PdfStamper(reader, bos); AcroFields form = stamper.getAcroFields(); String[] str = { \男\
\河北省保定市\
int i = 0; java.util.Iterator<String> it = form.getFields().keySet().iterator(); while (it.hasNext()) { String name = it.next().toString(); System.out.println(name);
form.setField(name, str[i++]); } stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true stamper.close(); Document doc = new Document(); PdfCopy copy = new
PdfCopy(doc, out); doc.open(); PdfImportedPage importPage =
copy.getImportedPage(new PdfReader(bos.toByteArray()), 1); copy.addPage(importPage); doc.close(); } catch (IOException e) { System.out.println(1); } catch (DocumentException e)
{ System.out.println(2); } } public static void main(String[] args) { fillTemplate(); } } 3.运行结果如下*********************************************************************
如果没有模板,就行自己生成pdf文件保存到磁盘:下面的方法可以实现:
[java] view plain copy print?public static void test1(){//生成pdf Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream(\ document.open(); document.add(new Paragraph(\
document.close(); } catch (Exception e) { System.out.println(\
exception\ } } 但是上述方法中包含中文时就会出现问题,所以可以使用下面这行代码实现,所使用的jar包,上面的两个依赖都包含了:
[java] view plain copy print?public static void
test1_1(){ BaseFont bf; Font font = null; try { bf =
BaseFont.createFont( \ BaseFont.NOT_EMBEDDED);//创建字体 font = new Font(bf,12);//使用字体 } catch (Exception e)
{ e.printStackTrace(); } Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream(\ document.open(); document.add(new
Paragraph(\你好 世界\引用字体 document.close(); } catch (Exception e) { System.out.println(\exception\ } }
**************************************************************************************
当然,如果你想弄的炫一点,想实现其他字体,可以去网上搜字体文件然后下载下来,放到项目里,我这里是在项目里新建了一个font文件夹,将字体文件放到了里面。 1.把华康少女的字体文件拷贝到这个文件夹里面了:运行以下代码就能得到pdf文件
[java] view plain copy print?public static void
test1_2(){ BaseFont bf; Font font = null; try { // bf = BaseFont.createFont(\//注意这里有一个,1 //
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//宋体文字 bf = BaseFont.createFont(\华康少女文字W5(P).TTC,1\
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//华康少女文字 font = new Font(bf,12); } catch (Exception e)
{ e.printStackTrace(); } Document document = new Document(); try { PdfWriter.getInstance(document, new FileOutputStream(\ document.open(); document.add(new Paragraph(\上善若水\ document.close(); } catch (Exception e) { System.out.println(\
exception\ } } 当然,如果你还想换其他字体,就去下载字体文件吧,然后把相关部分替换掉就行,上面注释的是宋体的。。。***********************以下是转载别人的****************************
以下为转载的别人方法:可以供参考版权声明:欢迎大家转载,转载请声明转载地址http://blog.csdn.net/u012377333,谢谢大家。
我们系统需要生成一个可以打印的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[java]view plain copy
/** *@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 =
{ 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[java]view plain copy
/** *@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(\
ticket.setTicketCreateTime(\年4月27日\
ticket.setTicketCompany(\武汉日创科技有限公司\ ticket.setSysName(\智能看护系统\ ticket.setMoneyLittle(\ ticket.setMoneyBig(\伍万元整\
ticket.setAccountCompany(\洪山福利院\ ticket.setBedNumber(\床位\ ticket.setUsername(\
ticket.setPassword(\
PDFTempletTicketpdfTT = new PDFTempletTicket(); pdfTT.setTemplatePdfPath(\ pdfTT.setTargetPdfpath(\ pdfTT.setTicket(ticket); Filefile = new File(\ file.createNewFile();
pdfTT.templetTicket(file); //OSSConfigureconfig = OSSUtil.getOSSConfigure(); //OSSManageUtil.uploadFile(config, file,
\ //System.out.println(\
config.getAccessUrl()); } }
导出来后的结果:
备注:导出PDF文档,From域的内容不可见问题;iText找不到字体;
问题1:我开始导出的From域内容看不到,将
ps.setFormFlattening(true);设置为flase后,可以看到From域,还是看不到内容,我点击进入From后可以看到内容,该方法是将From域隐藏;后来看到有人设置From域内容的字体:BaseFontbf =
BaseFont.createFont(PDFTicket.class.getResource(\+\
BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 以及:
s.setSubstitutionFonts(fontList); 问题2:别人的代码:
BaseFont bf = BaseFont.createFont(\\会报找不到字体的错误,这里的解决方式就是通过使用自己的字体就好,simsun.ttc是Windows下面自带的字体(简体宋体:C:\\Windows\\Fonts下面有很多字体可以使用)
******************************以上是转载************************************
问题1:我开始导出的From域内容看不到,将
ps.setFormFlattening(true);设置为flase后,可以看到From域,还是看不到内容,我点击进入From后可以看到内容,该方法是将From域隐藏;后来看到有人设置From域内容的字体:BaseFontbf =
BaseFont.createFont(PDFTicket.class.getResource(\+\
BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 以及:
s.setSubstitutionFonts(fontList); 问题2:别人的代码:
BaseFont bf = BaseFont.createFont(\\会报找不到字体的错误,这里的解决方式就是通过使用自己的字体就好,simsun.ttc是Windows下面自带的字体(简体宋体:C:\\Windows\\Fonts下面有很多字体可以使用)
******************************以上是转载************************************