在项目中,遇到了一个变态的需求,要我们在后台生成一个pdf文件,格式又很复杂,想了想如果自己手动利用iText绘制一个pdf,不太现实,通过查找资料后,发现了一种方法:
利用itextpdf+adobe acrobat+pdf模板生成pdf文件
要实现这个功能,我使用了这一个第三方包了:itextpdf.jar
还需要adobe acrobat这个软件,主要用来制作PDF模板
1.使用adobe acrobat的步骤:
1.1 将word转换成pdf文件,比如说:remark.pdf;
1.2 右击remark.pdf点击使用adobe acrobat 编辑的按钮;
1.3 选择准备表单;
1.4 adobe acrobat 工具会在表格中自动填充一些“文本域”,默认已 “fill_” 开头;
1.5 你可以修改表单域的名字,选择添加“文本”域,在想要添加的位置添加即可
2.编译java代码
2.1编译表单域的名字的javaBean,与adobe acrobat中的表单域的名字一一对应:
public class Ticket {
private String growup;
private String attainment;
private String present;
private String late;
private String leave;
private String sicklate;
private String bigbreak;
private String hint;
private String evaluate;
private String bearpalm;
private String praise;
private String improve;
private String yinzhang;
public Ticket() {
super();
}
public Ticket(String growup, String attainment, String present, String late, String leave, String sicklate,
String bigbreak, String hint, String evaluate, String bearpalm, String praise, String improve,
String yinzhang) {
super();
this.growup = growup;
this.attainment = attainment;
this.present = present;
this.late = late;
this.leave = leave;
this.sicklate = sicklate;
this.bigbreak = bigbreak;
this.hint = hint;
this.evaluate = evaluate;
this.bearpalm = bearpalm;
this.praise = praise;
this.improve = improve;
this.yinzhang = yinzhang;
}
public String getGrowup() {
return growup;
}
public void setGrowup(String growup) {
this.growup = growup;
}
public String getAttainment() {
return attainment;
}
public void setAttainment(String attainment) {
this.attainment = attainment;
}
public String getPresent() {
return present;
}
public void setPresent(String present) {
this.present = present;
}
public String getLate() {
return late;
}
public void setLate(String late) {
this.late = late;
}
public String getLeave() {
return leave;
}
public void setLeave(String leave) {
this.leave = leave;
}
public String getSicklate() {
return sicklate;
}
public void setSicklate(String sicklate) {
this.sicklate = sicklate;
}
public String getBigbreak() {
return bigbreak;
}
public void setBigbreak(String bigbreak) {
this.bigbreak = bigbreak;
}
public String getHint() {
return hint;
}
public void setHint(String hint) {
this.hint = hint;
}
public String getEvaluate() {
return evaluate;
}
public void setEvaluate(String evaluate) {
this.evaluate = evaluate;
}
public String getBearpalm() {
return bearpalm;
}
public void setBearpalm(String bearpalm) {
this.bearpalm = bearpalm;
}
public String getPraise() {
return praise;
}
public void setPraise(String praise) {
this.praise = praise;
}
public String getImprove() {
return improve;
}
public void setImprove(String improve) {
this.improve = improve;
}
public String getYinzhang() {
return yinzhang;
}
public void setYinzhang(String yinzhang) {
this.yinzhang = yinzhang;
}
}
2.2在表单域中绘制表格(这是4行2列的表格):
import java.util.List;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfStamper;
public class InsertExcel {
public static void insert(PdfStamper ps, AcroFields s) throws DocumentException
{
List list = s.getFieldPositions("evaluate");
Rectangle rect = list.get(0).position;
PdfContentByte cb = ps.getOverContent(1);
PdfPTable table = new PdfPTable(2);
float tatalWidth = rect.getRight() - rect.getLeft() - 1;
table.setTotalWidth(tatalWidth);
float[] widths = { 0.65f, 0.35f};
for(int i = 0;i < 4;i++){
PdfPCell cell1 = new PdfPCell(new Phrase("好"));
cell1.setUseBorderPadding(true);
cell1.setBorderWidth(0.2f);
cell1.setUseAscender(true);
cell1.setVerticalAlignment(cell1.ALIGN_MIDDLE);
cell1.setHorizontalAlignment(cell1.ALIGN_CENTER);
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));
cell2.setUseBorderPadding(true);
cell2.setBorderWidth(0.2f);
cell2.setUseAscender(true);
cell2.setVerticalAlignment(cell2.ALIGN_MIDDLE);
cell2.setHorizontalAlignment(cell2.ALIGN_CENTER);
table.addCell(cell2);
}
table.setWidths(widths);
table.setTotalWidth(154);
table.setLockedWidth(true);
table.getDefaultCell().setMinimumHeight(50);
table.writeSelectedRows(0, -1, rect.getLeft(), rect.getTop(), cb);
}
}
2.3在表单域中插入图片:
String imagePath = "E:\\1.png";
int pageNo = s.getFieldPositions("present").get(0).page;
Rectangle signRect = s.getFieldPositions("present").get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 读图片
Image image = Image.getInstance(imagePath);
// 获取操作的页面
PdfContentByte under = ps.getOverContent(pageNo);
// 根据域的大小缩放图片
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
2.4制作多页的pdf,插入文字,图片,表格:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import testPDF.Ticket;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
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 PDFTempletTicket {
private String templatePdfPath;
private String ttcPath;
private String targetPdfpath;
private Ticket ticket;
private GetImage getImage;
private InsertExcel insertExcel;
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 {
FileOutputStream fos = new FileOutputStream(file);
int page = 2;
ByteArrayOutputStream bos[] = new ByteArrayOutputStream[page];
for (int item = 0; item < page; item++) {
bos[item] = new ByteArrayOutputStream();
PdfReader reader = new PdfReader(templatePdfPath);
PdfStamper ps = new PdfStamper(reader, bos[item]);
//使用中文字体
BaseFont bf = BaseFont.createFont("c://WINDOWS//Fonts//simsun.TTC,0", BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
ArrayList fontList = new ArrayList ();
fontList.add(bf);
AcroFields s = ps.getAcroFields();
s.setSubstitutionFonts(fontList);
s.setField("growup",ticket.getGrowup());
s.setField("attainment",ticket.getAttainment());
s.setField("late",ticket.getLate());
s.setField("leave",ticket.getLeave());
s.setField("sicklate",ticket.getSicklate());
s.setField("bigbreak",ticket.getBigbreak());
s.setField("bearpalm",ticket.getBearpalm());
s.setField("praise",ticket.getPraise());
s.setField("improve",ticket.getImprove());
String imagePath = "E:\\1.png";
int pageNo = s.getFieldPositions("present").get(0).page;
Rectangle signRect = s.getFieldPositions("present").get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 读图片
Image image = Image.getInstance(imagePath);
// 获取操作的页面
PdfContentByte under = ps.getOverContent(pageNo);
// 根据域的大小缩放图片
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
int pageNo1 = s.getFieldPositions("hint").get(0).page;
Rectangle signRect1 = s.getFieldPositions("hint").get(0).position;
float x1 = signRect1.getLeft();
float y1 = signRect1.getBottom();
// 读图片
Image image1 = Image.getInstance(imagePath);
// 获取操作的页面
PdfContentByte under1 = ps.getOverContent(pageNo1);
// 根据域的大小缩放图片
image1.scaleToFit(signRect1.getWidth(), signRect1.getHeight());
// 添加图片
image1.setAbsolutePosition(x1, y1);
under1.addImage(image1);
String imagePath2 = "E:\\0.png";
int pageNo2 = s.getFieldPositions("yinzhang").get(0).page;
Rectangle signRect2 = s.getFieldPositions("yinzhang").get(0).position;
float x2 = signRect2.getLeft();
float y2 = signRect2.getBottom();
// 读图片
Image image2 = Image.getInstance(imagePath2);
// 获取操作的页面
PdfContentByte under2 = ps.getOverContent(pageNo2);
// 根据域的大小缩放图片
image2.scaleToFit(signRect2.getWidth(), signRect2.getHeight());
// 添加图片
image2.setAbsolutePosition(x2, y2);
under.addImage(image2);
//插入表格
insertExcel.insert(ps,s);
ps.setFormFlattening(true);
ps.close();
}
Document doc = new Document();
PdfCopy pdfCopy = new PdfCopy(doc, fos);
doc.open();
PdfImportedPage impPage = null;
/**取出之前保存的每页内容*/
for (int i = 0; i < page; i++) {
impPage = pdfCopy.getImportedPage(new PdfReader(bos[i]
.toByteArray()), 1);
pdfCopy.addPage(impPage);
}
doc.close();//当文件拷贝 记得关闭doc
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;
}
}
2.5mian函数
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class TestTempletTicket {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Ticket ticket = new Ticket();
ticket.setGrowup("Growup");
ticket.setAttainment("Attainment");
ticket.setPresent("aa");
ticket.setLate("Late");
ticket.setLeave("Leave");
ticket.setSicklate("Sicklate");
ticket.setBigbreak("Bigbreak");
ticket.setHint("Hint");
ticket.setEvaluate("Evaluate");
ticket.setBearpalm("Bearpalm");
ticket.setPraise("好");
ticket.setImprove("继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力继续努力"
+ "继续努力继续努力继续努!");
PDFTempletTicket pdfTT = new PDFTempletTicket();
pdfTT.setTemplatePdfPath("E:\\remark.pdf");
pdfTT.setTicket(ticket);
File file = new File("E:\\remark1.pdf");
file.createNewFile();
pdfTT.templetTicket(file);
}
}