JSP学习笔记(四十八):使用iText生成pdf文档

iText是一个可以操作pdf的开源项目,应用比较广泛,下载地址:http://www.lowagie.com/iText/download.html

我们的内容一般都有中文,在下载完iText的jar包后,还需要额外的下载iText中文支持包iTextAsian.jar,下载地址:http://nchc.dl.sourceforge.net/sourceforge/itext/iTextAsian.jar

 

1.现在开始使用iText,编写一个最简单的hello world例子:

        Document doc = new Document();

try {

// 定义输出位置并把文档对象装入输出对象中

PdfWriter.getInstance(doc,
new FileOutputStream("c:/myfile.pdf"));

// 打开文档对象

doc.open();


// 加入文字“Hello World”

doc.add(
new Paragraph("Hello World"));


// 关闭文档对象,释放资源

doc.close();

}
catch (FileNotFoundException e) {

e.printStackTrace();

}
catch (DocumentException e) {

e.printStackTrace();

}

该例子将在c盘建立文件myfile.pdf,生成pdf的内容为Hello World

 

2.下面我们需要把内容"Hello World"替换为中文"测试内容",代码修改如下:

        Document doc = new Document();

try {

// 定义输出位置并把文档对象装入输出对象中

PdfWriter.getInstance(doc,
new FileOutputStream("c:/myfile.pdf"));

// 打开文档对象

doc.open();

BaseFont bfChinese
= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese
= new Font(bfChinese, 12, Font.NORMAL);

// 加入文字“测试内容”

doc.add(
new Paragraph("测试内容",FontChinese));

// 关闭文档对象,释放资源

doc.close();

}
catch (FileNotFoundException e) {

e.printStackTrace();

}
catch (DocumentException e) {

e.printStackTrace();

}

 

3.在很多情况下,我们需要把生成的pdf文档直接让用户下载,而不是保存在服务器上某一个位置,继续改造我们前面编写的代码:

Document doc = new Document();

try {

// 使用ByteArrayOutputStream 代替 FileOutputStream

ByteArrayOutputStream outputStream
= new ByteArrayOutputStream();
 PdfWriter.getInstance(doc, outputStream);

// 打开文档对象

doc.open();

// 加入文字“Hello World”

doc.add(
new Paragraph("HelloWorld"));

// 关闭文档对象,释放资源

doc.close();

     //设置输出类型,这里我们需要以文件流的形式提供下载
response.addHeader("Content-Disposition", "attachment; filename=myfile.pdf");
     response.setContentType("application/octet-stream");
response.setContentLength(outputStream.size());

      ServletOutputStream out = response.getOutputStream();
      outputStream.writeTo(out);
      out.flush();

}
catch (DocumentException e) {

e.printStackTrace();

}

为了理解下载的功能,可以参考我写的上一篇文章:JSP学习笔记(四十七):以文件流方式读取文件,并且强制下载

4.当然我们制作pdf的时候,不可能仅仅输入简单的文字,下面提供制作表格的代码,我偷了下懒,是从网上直接copy过来的,不过我经过测试,是可以运行的:

        Document doc = new Document();

try {

// 定义输出位置并把文档对象装入输出对象中

PdfWriter.getInstance(doc,
new FileOutputStream("c:/myfile2.pdf"));

// 打开文档对象

doc.open();

// 加入表格

Table table
= new Table(3);
table.setBorderWidth(
1);
table.setPadding(
5);
table.setSpacing(
5);
Cell cell
= new Cell("aabb");
cell.setHeader(
true);
cell.setColspan(
3);
table.addCell(cell);
table.endHeaders();
cell
= new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(
2);
table.addCell(cell);
table.addCell(
"1.1");
table.addCell(
"2.1");
table.addCell(
"1.2");
table.addCell(
"2.2");
table.addCell(
"cell test1");
cell
= new Cell("big cell");
cell.setRowspan(
2);
cell.setColspan(
2);
table.addCell(cell);
table.addCell(
"cell test2");
doc.add(table);

// 关闭文档对象,释放资源

doc.close();

}
catch (FileNotFoundException e) {

e.printStackTrace();

}
catch (DocumentException e) {

e.printStackTrace();

}

 

posted @ 2008-09-15 10:10  魔豆  阅读(3006)  评论(1编辑  收藏  举报