我们系统需要生成一个可以打印的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
-
-
-
-
-
-
-
-
- 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;
-
-
-
-
-
-
-
- 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 fontList = newArrayList();
- 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();
- }
-
-
-
-
- publicString getTemplatePdfPath() {
- returntemplatePdfPath;
- }
-
-
-
-
- publicvoid setTemplatePdfPath(String templatePdfPath) {
- this.templatePdfPath= templatePdfPath;
- }
-
-
-
-
- publicString getTtcPath() {
- returnttcPath;
- }
-
-
-
-
- publicvoid setTtcPath(String ttcPath) {
- this.ttcPath= ttcPath;
- }
-
-
-
-
- publicString getTargetPdfpath() {
- returntargetPdfpath;
- }
-
-
-
-
- publicvoid setTargetPdfpath(String targetPdfpath) {
- this.targetPdfpath= targetPdfpath;
- }
-
-
-
-
- publicTicket getTicket() {
- returnticket;
- }
-
-
-
-
- publicvoid setTicket(Ticket ticket) {
- this.ticket= ticket;
- }
-
-
- }
数据类-Ticket
-
-
-
-
-
-
-
-
- package org.csun.ns.entity;
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
- publicString getTicketId() {
- returnticketId;
- }
-
-
-
-
- publicvoid setTicketId(String ticketId) {
- this.ticketId= ticketId;
- }
-
-
-
-
- publicString getHomesId() {
- returnhomesId;
- }
-
-
-
-
- publicvoid setHomesId(String homesId) {
- this.homesId= homesId;
- }
-
-
-
-
- publicString getTicketCreateTime() {
- returnticketCreateTime;
- }
-
-
-
-
- publicvoid setTicketCreateTime(String ticketCreateTime) {
- this.ticketCreateTime= ticketCreateTime;
- }
-
-
-
-
- publicString getTicketCompany() {
- returnticketCompany;
- }
-
-
-
-
- publicvoid setTicketCompany(String ticketCompany) {
- this.ticketCompany= ticketCompany;
- }
-
-
-
-
- publicString getSysName() {
- returnsysName;
- }
-
-
-
-
- publicvoid setSysName(String sysName) {
- this.sysName= sysName;
- }
-
-
-
-
- publicString getMoneyLittle() {
- returnmoneyLittle;
- }
-
-
-
-
- publicvoid setMoneyLittle(String moneyLittle) {
- this.moneyLittle= moneyLittle;
- }
-
-
-
-
- publicString getMoneyBig() {
- returnmoneyBig;
- }
-
-
-
-
- publicvoid setMoneyBig(String moneyBig) {
- this.moneyBig= moneyBig;
- }
-
-
-
-
- publicString getAccountCompany() {
- returnaccountCompany;
- }
-
-
-
-
- publicvoid setAccountCompany(String accountCompany) {
- this.accountCompany= accountCompany;
- }
-
-
-
-
- publicString getBedNumber() {
- returnbedNumber;
- }
-
-
-
-
- publicvoid setBedNumber(String bedNumber) {
- this.bedNumber= bedNumber;
- }
-
-
-
-
- publicString getUsername() {
- returnusername;
- }
-
-
-
-
- publicvoid setUsername(String username) {
- this.username= username;
- }
-
-
-
-
- publicString getPassword() {
- returnpassword;
- }
-
-
-
-
- publicvoid setPassword(String password) {
- this.password= password;
- }
-
- }
测试类-TestTempletTicket
-
-
-
-
-
-
-
-
- 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;
-
-
-
-
-
-
-
- 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);
-
-
-
-
-
-
- }
-
- }
导出来后的结果:
备注:导出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下面有很多字体可以使用)