1. 程式人生 > >java生成pdf

java生成pdf

根據 att ioe ins ntc spa 修改 tle 項目

  最近項目需要根據客戶填寫的信息,依照模板生成一個pdf文件。

  要是格式是固定的類型,可以使用Adobe Acrobat DC將模板的pdf文件轉化成可以編輯的類型,然後根據編輯時候取的變量名字一一將數據插入。

首先需要導入itextpdf的jar包。

// 模板路徑
String templatePath = "E:/test.pdf";
// 生成的新文件路徑
String newPDFPath = "E:/ceshi.pdf";
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //設置中文字體
Font font = new Font(bf, 12, Font.NORMAL);
out = new FileOutputStream(newPDFPath);// 輸出流
reader = new PdfReader(templatePath);// 讀取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields(); //獲取表單

String[] str = { "2017-05-10", "12345678", "LC123456", "2017-05-10", "深圳XXXXX有限公司", "4567890132","深圳市XXXX室", "34267687","123344","深圳市XXXXX室"};
int i = 0;
java.util.Iterator<String> it = form.getFields().keySet().iterator();

//賦值

while (it.hasNext()) {
String name = it.next().toString();
form.setFieldProperty(name, "textfont", bf, null);
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) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}

  但是因為要根據用戶輸入的信息,動態的生成一個對應的表格。所以最後就直接按照模板生成了一個新的pdf。

  這個主要就是使用段落、表格進行布局,中間拼接一些數據就OK了。(最後做出來的文件不是很好看,還是要在字體和布局上進行修改)

//實例化文檔對象
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
//設置中文字體
BaseFont bfChinese =
BaseFont.createFont("C:/Windows/Fonts/simhei.ttf",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

//字體
Font titleChinese = new Font(bfChinese, 18, Font.BOLD);
Font secondtitleChinese = new Font(bfChinese, 14, Font.BOLD);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
//創建PdfWriter對象
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("C:\\ITextTest.pdf"));
document.open();
Paragraph title=new Paragraph("主標題",titleChinese);
title.setAlignment(Element.ALIGN_CENTER);
Paragraph title2=new Paragraph("(副標題)",fontChinese);
title2.setAlignment(Element.ALIGN_CENTER);
//設置段落間距
title2.setSpacingAfter(30);
document.add(title);
document.add(title2);
String str="正文一類的東西。。。。。。。。";
Paragraph content=new Paragraph(str,fontChinese);
//首行縮進
content.setFirstLineIndent(20);
//設置行間距
content.setLeading(20);
content.setSpacingAfter(30);
document.add(content);
Paragraph title3=new Paragraph("供應商應收賬款轉讓清單",secondtitleChinese);
title3.setAlignment(Element.ALIGN_CENTER);
document.add(title3);
//表格編號和日期
Paragraph id=new Paragraph();
Chunk chunk=new Chunk(" 編號:ABCDEFGH",fontChinese);
Chunk chunk2=new Chunk(" 日期:2017年6月29日",fontChinese);
id.add(chunk);
id.add(chunk2);
id.setSpacingAfter(5);
document.add(id);
// 建立一個pdf表格
float[] widths = {25f, 30f, 35f, 25f, 25f, 25f, 30f, 30f};
PdfPTable table = new PdfPTable(widths);
PdfPCell cell = null;
//第一行
cell = new PdfPCell(new Paragraph("供應商",fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("發票號",fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("幣別",fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("發票日",fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("發票票面金額",fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("供應商應收賬款金額(承諾付款金額)",fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("承諾付款日",fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("備註",fontChinese));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
//往表格內部填充數據,必須填充為列數的整數倍的單元格,沒有則填充空
String[] danju=new String[]{"1","2","3","4","5","6","7","8"};

for (int i = 0; i <danju.length; i++) {
table.addCell(new Paragraph(danju[i],fontChinese));
}
table.addCell(new Paragraph("合計",fontChinese));
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
document.add(table);
//簽名
PdfPTable table2=new PdfPTable(2);
table2.setSpacingBefore(30);
PdfPCell sgin1 = new PdfPCell(new Paragraph("XX企業電子簽名:",fontChinese));
sgin1.setBorder(0);
PdfPCell sgin2 = new PdfPCell(new Paragraph("XX平臺電子簽名:",fontChinese));
sgin2.setBorder(0);
table2.addCell(sgin1);
table2.addCell(sgin2);
PdfPCell image=new PdfPCell(new Paragraph("簽名區域",fontChinese));
image.setFixedHeight(50);
image.setBorder(0);
table2.addCell(image);
table2.addCell(image);
Chunk underline = new Chunk("2017");
underline.setUnderline(0.1f, -1f);
Chunk underline2 = new Chunk("6");
underline2.setUnderline(0.1f, -1f);
Chunk underline3 = new Chunk("29");
underline3.setUnderline(0.1f, -1f);
Paragraph date=new Paragraph();
date.add(underline);
date.add(new Chunk("年",fontChinese));
date.add(underline2);
date.add(new Chunk("月",fontChinese));
date.add(underline3);
date.add(new Chunk("日",fontChinese));
PdfPCell date1 = new PdfPCell(date);
date1.setBorder(0);
date1.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(date1);
table2.addCell(date1);
document.add(table2);
document.close();

  還是覺得這個方法不太好,有多少個模板就要寫多少個,不能直接利用模板。

java生成pdf