原文地址:https://i-blog.csdnimg.cn/blog_migrate/e6d755c11ecbc1261b877fabe8fba9e9.png
首先你的制作一个pdf模板:
1.先用word做出模板界面
2.文件另存为pdf格式文件
3.通过Adobe Acrobat pro软件打开刚刚用word转换成的pdf文件(注:如果没有这个软件可以通过我的百度云下载,链接:http://pan.baidu.com/s/1pL2klzt)如果无法下载可以联系博主。
4.点击右边的"准备表单"按钮,选择"测试.pdf"选择开始
进去到编辑页面,打开后它会自动侦测并命名表单域,右键表单域,点击属性,出现文本域属性对话框(其实无需任何操作,一般情况下不需要修改什么东西,至少我没有修改哦。如果你想修改fill1等信息,可以进行修改)
5.做完上面的工作后,直接"另存为"将pdf存储就可以
*****************************************************************************
以上部分是制作pdf模板操作,上述完成后,就开始通过程序来根据pdf模板生成pdf文件了,上java程序:
1.首先需要依赖包:itext的jar包,我是maven项目,所以附上maven依赖
- <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
- <dependency>
- <groupId>com.itextpdf</groupId>
- <artifactId>itextpdf</artifactId>
- <version>5.5.10</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
- <dependency>
- <groupId>com.itextpdf</groupId>
- <artifactId>itext-asian</artifactId>
- <version>5.2.0</version>
- </dependency>
- 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 = "E:/测试3.pdf";
- // 生成的新文件路径
- String newPDFPath = "E:/ceshi.pdf";
- 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 = { "123456789", "TOP__ONE", "男", "1991-01-01", "130222111133338888", "河北省保定市" };
- 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();
- }
- }
*********************************************************************
如果没有模板,就行自己生成pdf文件保存到磁盘:下面的方法可以实现:
- public static void test1(){//生成pdf
- Document document = new Document();
- try {
- PdfWriter.getInstance(document, new FileOutputStream("E:/1.pdf"));
- document.open();
- document.add(new Paragraph("hello word"));
- document.close();
- } catch (Exception e) {
- System.out.println("file create exception");
- }
- }
- public static void test1_1(){
- BaseFont bf;
- Font font = null;
- try {
- bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
- BaseFont.NOT_EMBEDDED);//创建字体
- font = new Font(bf,12);//使用字体
- } catch (Exception e) {
- e.printStackTrace();
- }
- Document document = new Document();
- try {
- PdfWriter.getInstance(document, new FileOutputStream("E:/2.pdf"));
- document.open();
- document.add(new Paragraph("hello word 你好 世界",font));//引用字体
- document.close();
- } catch (Exception e) {
- System.out.println("file create exception");
- }
- }
当然,如果你想弄的炫一点,想实现其他字体,可以去网上搜字体文件然后下载下来,放到项目里,我这里是在项目里新建了一个font文件夹,将字体文件放到了里面。
1.把华康少女的字体文件拷贝到这个文件夹里面了:
运行以下代码就能得到pdf文件
- public static void test1_2(){
- BaseFont bf;
- Font font = null;
- try {
- // bf = BaseFont.createFont("font/simsun.ttc,1", //注意这里有一个,1
- // BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//宋体文字
- bf = BaseFont.createFont("font/华康少女文字W5(P).TTC,1", //simsun.ttc
- 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("E:/3.pdf"));
- document.open();
- document.add(new Paragraph("上善若水",font));
- document.close();
- } catch (Exception e) {
- System.out.println("file create 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
- /**
- *@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 {
- private String templatePdfPath;
- private String ttcPath;
- private String targetPdfpath;
- private Ticket ticket;
- public PDFTempletTicket() {
- super();
- }
- public PDFTempletTicket(String templatePdfPath, String ttcPath,
- StringtargetPdfpath, Ticket ticket) {
- this.templatePdfPath= templatePdfPath;
- this.ttcPath= ttcPath;
- this.targetPdfpath= targetPdfpath;
- this.ticket= ticket;
- }
- public void 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
- */
- public String getTemplatePdfPath() {
- return templatePdfPath;
- }
- /**
- * @param templatePdfPath the templatePdfPathto set
- */
- public void setTemplatePdfPath(String templatePdfPath) {
- this.templatePdfPath= templatePdfPath;
- }
- /**
- * @return the ttcPath
- */
- public String getTtcPath() {
- returnttcPath;
- }
- /**
- * @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;
- }
- }
数据类-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 {
- private String ticketId;
- private String homesId;
- private String ticketCreateTime;
- private String ticketCompany;
- private String sysName;
- private String moneyLittle;
- private String moneyBig;
- private String accountCompany;
- private String bedNumber;
- private String username;
- private String password;
- public Ticket() {
- super();
- }
- public Ticket(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
- */
- public String getTicketId() {
- return ticketId;
- }
- /**
- * @param ticketId the ticketId to set
- */
- public void setTicketId(String ticketId) {
- this.ticketId= ticketId;
- }
- /**
- * @return the homesId
- */
- public String getHomesId() {
- return homesId;
- }
- /**
- * @param homesId the homesId to set
- */
- public void setHomesId(String homesId) {
- this.homesId= homesId;
- }
- /**
- * @return the ticketCreateTime
- */
- public String getTicketCreateTime() {
- return ticketCreateTime;
- }
- /**
- * @param ticketCreateTime the ticketCreateTimeto set
- */
- public void setTicketCreateTime(String ticketCreateTime) {
- this.ticketCreateTime= ticketCreateTime;
- }
- /**
- * @return the ticketCompany
- */
- public String getTicketCompany() {
- return ticketCompany;
- }
- /**
- * @param ticketCompany the ticketCompany toset
- */
- public void setTicketCompany(String ticketCompany) {
- this.ticketCompany= ticketCompany;
- }
- /**
- * @return the sysName
- */
- public String getSysName() {
- return sysName;
- }
- /**
- * @param sysName the sysName to set
- */
- public void setSysName(String sysName) {
- this.sysName= sysName;
- }
- /**
- * @return the moneyLittle
- */
- public String getMoneyLittle() {
- return moneyLittle;
- }
- /**
- * @param moneyLittle the moneyLittle to set
- */
- public void setMoneyLittle(String moneyLittle) {
- this.moneyLittle= moneyLittle;
- }
- /**
- * @return the moneyBig
- */
- public String getMoneyBig() {
- return moneyBig;
- }
- /**
- * @param moneyBig the moneyBig to set
- */
- public void setMoneyBig(String moneyBig) {
- this.moneyBig= moneyBig;
- }
- /**
- * @return the accountCompany
- */
- public String getAccountCompany() {
- return accountCompany;
- }
- /**
- * @param accountCompany the accountCompany toset
- */
- public void setAccountCompany(String accountCompany) {
- this.accountCompany= accountCompany;
- }
- /**
- * @return the bedNumber
- */
- public String getBedNumber() {
- return bedNumber;
- }
- /**
- * @param bedNumber the bedNumber to set
- */
- public void setBedNumber(String bedNumber) {
- this.bedNumber= bedNumber;
- }
- /**
- * @return the username
- */
- public String getUsername() {
- return username;
- }
- /**
- * @param username the username to set
- */
- public void setUsername(String username) {
- this.username= username;
- }
- /**
- * @return the password
- */
- public String getPassword() {
- return password;
- }
- /**
- * @param password the password to set
- */
- public void 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 {
- public static void main(String[] args) throws Exception {
- Ticket ticket = 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");
- PDFTempletTicket pdfTT = new PDFTempletTicket();
- pdfTT.setTemplatePdfPath("D:\\ticket_from.pdf");
- pdfTT.setTargetPdfpath("D:\\aaabbbccc.pdf");
- pdfTT.setTicket(ticket);
- File file = 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域内容的字体:BaseFont bf = 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下面有很多字体可以使用)