使用iText JAR生成PDF
在很多的場合,需要使用PDF文件,有時候也要設置PDF文件的表格和文字等。
如果讓一個PDF工具類非常靈活,那么這個工具類就會很復雜;一般根據實際情況,寫幾個合適的工具類來分別實現不同要求;
需要引入第三方jar包:iText-2.1.4.jar和iTextAsian.jar
package com.daily;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.daily.EasyExcel.Content;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
/**
*生成pdf文件,書寫普通文字,制作表格,設置文字顏色和簡單排版等
*@author范芳銘
*/
public class EasyPdf {
privatevoid createPdfFile(String outFilename,Map model,List table) throws Exception{
try{
OutputStream file = newFileOutputStream(new File(outFilename));
Document document = newDocument();
PdfWriter.getInstance(document,file);
document.open();
document.add(newParagraph("Hello PDF")); //簡單文字
document.add(new Paragraph(newDate().toString())); //簡單日期
//設置字體,表格等內容
BaseFont bfComic =BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = newFont(bfComic, 16,Font.NORMAL);//聲明字體對象
font = newFont(bfComic,16,1,new Color(255,0,0));//設置字體
String header =(String)model.get("header");//獲取標題
Paragraph headerParagraph =new Paragraph(header,font);
headerParagraph.setAlignment(Paragraph.ALIGN_CENTER);//設置格式
document.add(headerParagraph);//添加到Document對象中
font = new Font(bfComic,10, Font.NORMAL);//聲明字體樣式
//設置PDF中表格的表頭
String title[] = {"code","名稱"};
Table t = newTable(title.length);//聲明表格對象
t.normalize();
t.setAutoFillEmptyCells(true);
t.setAlignment(Table.ALIGN_CENTER);//居中
t.setWidth(108);//設置寬度
for(int i = 0 ; i
Cell celltitle = newCell();//聲明單元格
celltitle.add(newPhrase(title[i],font));//循環加入表頭信息
celltitle.setHorizontalAlignment(Element.ALIGN_CENTER);
t.addCell(celltitle);
}
t.endHeaders();
//加入表格內容
Content content = null;
for (int i = 0; i
//得到要寫入表格的記錄
content = table.get(i);
CelloneCell = new Cell(); //聲明單元格對象
Paragraphpgdata = new Paragraph(content.getCode(),font);
oneCell.setHorizontalAlignment(Element.ALIGN_CENTER);//對齊方式
oneCell.addElement(pgdata);
t.addCell(oneCell);
CelltwoCell = new Cell(); //聲明單元格對象
twoCell.setHorizontalAlignment(Element.ALIGN_CENTER);//對齊方式
twoCell.addElement(pgdata);
t.addCell(twoCell);
}
document.add(t);
String copyright =(String)model.get("copyright");
ParagraphcopyrightParagraph = new Paragraph(copyright,font);
copyrightParagraph.setAlignment(Paragraph.ALIGN_BOTTOM);
document.add(copyrightParagraph);
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
}
publicstatic void main(String[] args) throws Exception{
//初始化數據
Mapmodel = new HashMap();
//文件標題頭
model.put("header","詳細信息");
//文件版權信息
model.put("copyright","版權所有:阿飯同學的博客 http://blog.csdn.net/ffm83");
//生成表格的內容,內部類的構造有點特殊
Listcontents = new ArrayList();
contents.add(newEasyPdf().new Content("100","name100"));
contents.add(newEasyPdf().new Content("101","name101"));
contents.add(newEasyPdf().new Content("102","name102"));
longstart = System.currentTimeMillis();
EasyPdfpdf = new EasyPdf();
pdf.createPdfFile("d:/ffm83/easy.pdf",model,contents);
longend = System.currentTimeMillis();
System.out.println("生成pdf文件耗時:"+ (end - start) + "(毫秒)");
}
//一個簡單的保存簡單信息的內部類
publicclass Content {
privateString code;
private String name;
public Content(String code,String name){
this.code= code;
this.name= name;
}
publicString getCode() {
returncode;
}
publicvoid setCode(String code) {
this.code= code;
}
publicString getName() {
returnname;
}
publicvoid setName(String name) {
this.name= name;
}
}
}