700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > iText导出PDF经典实现

iText导出PDF经典实现

时间:2022-06-28 22:48:30

相关推荐

iText导出PDF经典实现

原文地址为: iText导出PDF经典实现

上篇文章我们借助POI导出了EXCEL格式的文件,这次我们换另一种格式PDF,那么,用什么开源组件呢?呵呵,听说iText不错,我们就用iText吧。

首先我们来了解下iText: 简单地说,iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本,表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。使用iText与PDF能够使你正确的控制Servlet的输出。恩?怎么像教科书啊?呵呵,这就是官网的介绍了。那么,唐伯虎点蚊香的武状元出场了,请问,到哪里可以下载得到呢?下面是该项目主页:/iText/我这里使用的是 2.0.7 版本。

我们已经接触过一个第三方开源组件POI,那么应该积累了一点使用经验了。我们还是利用上篇文章/lenotang/archive//08/24/2823230.aspx上的两个业务类:Student.java 和Book.java,有心理准备了吗?呵呵,出代码:

packageorg.leno.export.util;

importjava.io.UnsupportedEncodingException;

publicclassStrHelp {

publicstaticString getChinese(String s) {

try{

returnnewString(s.getBytes("gb2312"), "iso-8859-1");

}catch(UnsupportedEncodingException e) {

returns;

}

}

}

packageorg.leno.export.util;

importjava.io.IOException;

importcom.lowagie.text.*;

importcom.lowagie.text.pdf.BaseFont;

publicclassPdfParagraphextendsParagraph {

privatestaticfinallongserialVersionUID= -244970043180837974L;

publicPdfParagraph(String content) {

super(content,getChineseFont(12,false));

}

publicPdfParagraph(String content,intfontSize,booleanisBold) {

super(content,getChineseFont(fontSize, isBold));

}

// 设置字体-返回中文字体

protectedstaticFont getChineseFont(intnfontsize,booleanisBold) {

BaseFont bfChinese;

Font fontChinese =null;

try{

bfChinese = BaseFont.createFont("c://windows//fonts//simsun.ttc,1",

BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

if(isBold) {

fontChinese =newFont(bfChinese, nfontsize, Font.BOLD);

}else{

fontChinese =newFont(bfChinese, nfontsize, Font.NORMAL);

}

}catch(DocumentException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}catch(IOException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}

returnfontChinese;

}

// 转化中文

protectedCell ChangeCell(String str,intnfontsize,booleanisBold)

throwsIOException, BadElementException, DocumentException {

Phrase ph = ChangeChinese(str, nfontsize, isBold);

Cell cell =newCell(ph);

// cell.setBorderWidth(3);

returncell;

}

// 转化中文

protectedChunk ChangeChunk(String str,intnfontsize,booleanisBold)

throwsIOException, BadElementException, DocumentException {

Font FontChinese =getChineseFont(nfontsize, isBold);

Chunk chunk =newChunk(str, FontChinese);

returnchunk;

}

// 转化中文

protectedPhrase ChangeChinese(String str,intnfontsize,booleanisBold)

throwsIOException, BadElementException, DocumentException {

Font FontChinese =getChineseFont(nfontsize, isBold);

Phrase ph =newPhrase(str, FontChinese);

returnph;

}

}

以上是两个帮助封装类,都是为了对付iText的中文问题的。下面就是主角出场:

packageorg.leno.export.util;

importjava.io.*;

importjava.lang.reflect.*;

importjava.util.*;

importcom.lowagie.text.*;

importcom.lowagie.text.pdf.*;

importjava.awt.Color;

importjavax.swing.JOptionPane;

import.MalformedURLException;

importjava.text.SimpleDateFormat;

/**

* 利用开源组件IText 2.0.7 动态导出PDF文档 转载时请保留以下信息,注明出处!

*

*@authorleno

*@versionv1.0

*@param<T>

* 应用泛型,代表任意一个符合javabean风格的类

* 注意这里为了简单起见,boolean型的属性xxx的get器方式为getXxx(),而不是isXxx()

* byte[]表图片数据,注意合适的大小

*/

publicclassExportPdf<T> {

publicvoidexportPdf(Collection<T> dataset, OutputStream out) {

exportPdf("测试iText导出PDF文档",null, dataset, out, "yyyy-MM-dd");

}

publicvoidexportPdf(String[] headers, Collection<T> dataset,

OutputStream out) {

exportPdf("测试iText导出PDF文档", headers, dataset, out, "yyyy-MM-dd");

}

publicvoidexportPdf(String[] headers, Collection<T> dataset,

OutputStream out, String pattern) {

exportPdf("测试iText导出PDF文档", headers, dataset, out, pattern);

}

/**

* 这是一个通用的方法,利用了JAVA的反射机制,可以将放置在JAVA集合中并且符号一定条件的数据以PDF 的形式输出到指定IO设备上

*

*@paramtitle

* 表格标题名

*@paramheaders

* 表格属性列名数组

*@paramdataset

* 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。此方法支持的

* javabean属性的数据类型有基本数据类型及String,Date,byte[](图片数据)

*@paramout

* 与输出设备关联的流对象,可以将PDF文档导出到本地文件或者网络中

*@parampattern

* 如果有时间数据,设定输出格式。默认为"yyy-MM-dd"

*/

@SuppressWarnings("unchecked")

publicvoidexportPdf(String title, String[] headers,

Collection<T> dataset, OutputStream out, String pattern) {

// 作为报表的PDF文件,一定要适合打印机的输出打印

Rectangle rectPageSize =newRectangle(PageSize.A4);// 定义A4页面大小

// rectPageSize = rectPageSize.rotate();// 加上这句可以实现A4页面的横置

Document document =newDocument(rectPageSize, 50, 50, 50, 50);// 其余4个参数,设置了页面的4个边距

try{

// 将PDF文档写出到out所关联IO设备上的书写对象

PdfWriter.getInstance(document, out);

// 添加文档元数据信息

document.addTitle(StrHelp.getChinese(title));

document.addSubject("export information");

document.addAuthor("leno");

document.addCreator("leno");

document.addKeywords("pdf itext");

// 定义页头和页尾

HeaderFooter header =newHeaderFooter(newPdfParagraph(title, 20,

true),false);

header.setAlignment(Element.ALIGN_CENTER);

HeaderFooter footer =newHeaderFooter(newPhrase(

"This is page "),newPhrase("."));

footer.setAlignment(Element.ALIGN_CENTER);

document.setHeader(header);

document.setFooter(footer);

// 打开PDF文档

document.open();

// 添加一张表格,使用Table或者PdfPTable

// Table table = new Table(headers.length);

// table.setWidth(16*headers.length);

// //table.setWidths(new float[]{20,20,20,30});

// table.setCellsFitPage(true);

// table.setAutoFillEmptyCells(true);

// table.setAlignment(Table.ALIGN_CENTER);

// table.setBackgroundColor(Color.yellow);

// table.setBorderColor(Color.green);

PdfPTable table =newPdfPTable(headers.length);

// table.setHorizontalAlignment(Element.ALIGN_CENTER);

table.setWidthPercentage(16 * headers.length);

// 产生表格标题行

for(inti = 0; i < headers.length; i++) {

PdfPCell cell =newPdfPCell(newPdfParagraph(headers[i], 14,

true));

cell.setHorizontalAlignment(Cell.ALIGN_CENTER);

cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);

cell.setBackgroundColor(Color.cyan);

cell.setBorderColor(Color.green);

table.addCell(cell);

}

// 遍历集合数据,产生数据行

Iterator<T> it = dataset.iterator();

intindex = 0;

while(it.hasNext()) {

index++;

T t = (T) it.next();

// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值

Field[] fields = t.getClass().getDeclaredFields();

for(shorti = 0; i < fields.length; i++) {

PdfPCell cell =null;

Field field = fields[i];

String fieldName = field.getName();

String getMethodName = "get"

+ fieldName.substring(0, 1).toUpperCase()

+ fieldName.substring(1);

try{

Class tCls = t.getClass();

Method getMethod = tCls.getMethod(getMethodName,

newClass[] {});

Object value = getMethod.invoke(t,newObject[] {});

// 判断值的类型后进行强制类型转换

String textValue =null;

if(valueinstanceofBoolean) {

booleanbValue = (Boolean) value;

textValue = "男";

if(!bValue) {

textValue = "女";

}

}elseif(valueinstanceofDate) {

Date date = (Date) value;

SimpleDateFormat sdf =newSimpleDateFormat(pattern);

textValue = sdf.format(date);

}elseif(valueinstanceofbyte[]) {

byte[] bsValue = (byte[]) value;

Image img = Image.getInstance(bsValue);

cell =newPdfPCell(img);

}else{

textValue = value.toString();

}

// 如果不是图片数据,就当做文本处理

if(textValue !=null) {

cell =newPdfPCell(newPdfParagraph(textValue));

}

cell.setHorizontalAlignment(Cell.ALIGN_CENTER);

cell.setVerticalAlignment(Cell.ALIGN_MIDDLE);

cell.setBorderColor(Color.green);

table.addCell(cell);

}catch(SecurityException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}catch(NoSuchMethodException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}catch(IllegalArgumentException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}catch(IllegalAccessException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}catch(InvocationTargetException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}catch(MalformedURLException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}catch(IOException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}finally{

// 清理资源

}

}

}

document.add(table);

document.close();

}catch(DocumentException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}

}

publicstaticvoidmain(String[] args) {

// 测试学生

ExportPdf<Student> ex =newExportPdf<Student>();

String[] headers = { "学号", "姓名", "年龄", "性别", "出生日期" };

java.util.List<Student> dataset =newArrayList<Student>();

dataset.add(newStudent(10000001, "张三", 20,true,newDate()));

dataset.add(newStudent(20000002, "李四", 24,false,newDate()));

dataset.add(newStudent(30000003, "王五", 22,true,newDate()));

// 测试图书

ExportPdf<Book> ex2 =newExportPdf<Book>();

String[] headers2 = { "图书编号", "图书名称", "图书作者", "图书价格", "图书ISBN",

"图书出版社", "封面图片" };

java.util.List<Book> dataset2 =newArrayList<Book>();

try{

BufferedInputStream bis =newBufferedInputStream(

newFileInputStream("book.jpg"));

byte[] buf =newbyte[bis.available()];

while((bis.read(buf)) != -1) {

//

}

dataset2.add(newBook(1, "jsp", "leno", 300.33f , "1234567",

"清华出版社", buf));

dataset2.add(newBook(2, "java编程思想", "brucl", 300.33f , "1234567",

"阳光出版社", buf));

dataset2.add(newBook(3, "DOM艺术", "lenotang", 300.33f , "1234567",

"清华出版社", buf));

dataset2.add(newBook(4, "c++经典", "leno", 400.33f , "1234567",

"清华出版社", buf));

dataset2.add(newBook(5, "c#入门", "leno", 300.33f , "1234567",

"汤春秀出版社", buf));

OutputStream out =newFileOutputStream("E://a.pdf");

OutputStream out2 =newFileOutputStream("E://b.pdf");

ex.exportPdf(headers, dataset, out);

ex2.exportPdf(headers2, dataset2, out2);

out.close();

out2.close();

JOptionPane.showMessageDialog(null, "pdf导出成功!");

System.out.println("pdf导出成功!");

}catch(FileNotFoundException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}catch(IOException e) {

//TODOAuto-generated catch block

e.printStackTrace();

}

}

}

恩,有了上一次的经验和教训,相信这次大家看这些代码已经不觉得头疼了。大同小异嘛,连测试和方法的声明都一样。大家结合文档,应该很快就能掌握iText的用法了。那么在web环境下使用该类上篇文章已经介绍过了,我这里就不赘述啦。这样当下次你的项目中要用到数据的导出功能时,你就可以学以致用。

好啦,两个开源组件学习完毕,我这里只是把它们在实际项目中使用的方式列出来了,并没有一点一点介绍它们的API,关于它们系统的学习,还请参考其他的文章和帮助文档。那么,我这里要着重强调一点:我们按照一定的设计思想写出代码,重要的的就不是代码,而是我们的思维!大家要尽可能让自己的代码变得通用灵活并且结构清晰,要学会与代码对话,在项目中更是要侧重设计和功能,而不是技术。始终记住,技术是为业务需求服务的!呵呵,唠叨就到这里了。希望您有所进步!

转载请注明本文地址: iText导出PDF经典实现

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。