使用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<Content> 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 <title.length ; 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 <table.size(); 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");
//生成表格的内容,内部类的构造有点特殊
List<Content>contents = new ArrayList<Content>();
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;
}
}
}