您的当前位置:首页正文

itext使用方法

来源:爱够旅游网
如今PDF格式文档的使用已经越来越普遍,它在文档压缩、安全等方面都表现的非常优秀。那么如何使用Java语言开发应用来输出PDF格式的文档呢?答案就是iText,它是一个开发源代码的项目,你可以使用iText方便的实现PDF的输出。

iText简介

iText是一个开放源码的Java类库,可以用来方便地生成PDF文件。大家通过访问http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948下载最新版本的类库,下载完成之后会得到一个.jar包,把这个包加入JDK的classpath即可使用。如果生成的PDF文件中需要出现中文、日文、韩文字符,则还需要通过访问http://itext.sourceforge.net/downloads/iTextAsian.jar下载iTextAsian.jar包。

关于iText类库的使用,http://www.lowagie.com/iText/tutorial/index.html有比较详细的教程。该教程从入门开始,比较系统地介绍了在PDF文件中放入文字、图片、表格等的方法和技巧。

*请记住:把itext.jar放到你的ClassPath 之中

下面,先举一个HelloWorld的例子来向大家介绍iText的使用方法。

/* *创建第一个Hello World程序 */ package test1; import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.lowagie.text.*; import com.lowagie.text.pdf.*; public class HelloWorld {

public static void main(String[] args) {

//创建一个文档对象

Document doc=new Document(); try {

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

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

//打开文档对象 doc.open();

// 加入文字“Hello World”

doc.add(new Paragraph(\"HelloWorld\")); // 关闭文档对象,释放资源 doc.close(); }

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (DocumentException e) { }

e.printStackTrace();

现在运行上面的代码,如果一切正常的话你会在”c:/”看到一个名为hello.pdf的文件。打开这个文件,看到了什么?是的,文档里有一行字符“HelloWorld”。

更复杂的设置

分析一下Document的构造方法,我们发现除了我们上一个例子中的无参数构造以外还有两个:

public Document(); public Document(Rectangle pageSize); public Document(Rectangle pageSize, int marginLeft, int marginRight, int marginTop, int marginBottom); 第一个设置文档的页面大小,第二个除了设置文档的页面大小还设置页面边距。下面我分别给出例子。

Rectangle pSize=new Rectangle(144,90); //文档的背景色 pSize.setBackgroundColor(Color.blue); //创建一个文档对象,并设置他的初始化大小 Document doc=new Document(pSize); Rectangle pSize=new Rectangle(144,90); //文档的背景色 pSize.setBackgroundColor(Color.blue); //创建一个文档对象,设置初始化大小和页边距 Document doc=new Document(pSize,5,5,5,5); 将第一个例子中的代码按上面的方法修改然后运行,你可以看到输出的PDF文档将是这个样子,文档变得很小而且背景是蓝色:

在上面的例子中我们通过Rectangle设置了文档的大小,其实iText已经为我们定义好了许多常用的页面,比如:A0-A10, LEGAL, LETTER等等,这些都放

在com.lowagie.text.PageSize这个类中,你可以通过调用PageSize中的静态方法直接引用页面信息。比如:

PageSize.A4; 设置字体

使用iText可以设置文字的字体,对于我们中国的程序员来说如何显示中文是最紧要的问题。幸好iText中有一个专门的包用来设置亚洲国家的字体你可以从

http://itext.sourceforge.net/downloads/iTextAsian.jar下载这个包。然后把它直接放到你

的ClassPath中就可以了。如何设置字体呢?

BaseFont bfChinese = BaseFont.createFont(\"STSong-Light\", \"UniGB-UCS2-H\", BaseFont.NOT_EMBEDDED); Font FontChinese = new Font(bfChinese, 12, Font.NORMAL); 在上面的代码中设置了中文字体的显示,你只要使用下面的代码就可以包中文加到PDF中了

String title = \"XXXX:XXXX\"; Paragraph t = new Paragraph(title, FontChinese); doc.add(t); 如果你觉得这样设置很麻烦的话,呵呵,那你要自己扩展它的源代码了,设置字体全部在那个BaseFont里边。 编辑表格

iText中的表格很像HTML中表格的使用不过它有一个cell代表一个格子,基本上这里的Table和Swing中的Table对象是一致的,比如上面代码中对于表格的设置:

//定义一个表格 Table table = new Table(2); //设置表格边框 table.setBorderWidth(1); Cell cell = new Cell(\"Matrix III\"); cell.setHeader(true); //分列 cell.setColspan(2); cell.setBackgroundColor(Color.blue); table.addCell(cell); 放置图片

现在你一定知道如何把一个图片加到文档中了,没错只要声明一个Image对象就可以了,这里的Image和AWT中的Image使用方法是一样的。

//定义一个图片 Image jpeg = Image.getInstance(\"C:/matrix.jpg\"); //图片居中 jpeg.setAlignment(Image.ALIGN_CENTER);

如何利用iText在JSP中生成PDF报表 。 1)直接在服务器上生成PDF文件。

<%@ page import =\"com.lowagie.text.*,com.lowagie.text.pdf.*, java.io.*\"%> <% java/lang/String.java.html\" target=\"_blank\">String filename = \"PDF\"+(new Random()).nextInt()+\".pdf\" ; Document document = new Document(PageSize.A4); ServletOutputStream out1 = response.getOutputStream(); try{ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename) ); document.open(); document.add(new Paragraph(\"Hello World\")); document.close(); } catch(java/lang/Exception.java.html\" target=\"_blank\">Exception e){} %> 上面的程序在服务器上生成了一个静态的PDF文件。显然,每次运行所得的PDF文件的名称应该是独一无二不能有重的。本程序通过随机函数来命名生成的PDF文件。本程序的缺点就是,每次运行都会在服务器上产生一个PDF文件,如果不及时删除,数量会越来越大,这显然是站点维护者所不愿意看到的。 2)将PDF文件通过流的形式输送到客户端的缓存。这样做的好处是不会在服务器上留下任何“遗迹”。

i)直接通过JSP页面生成

<%@ page import=\"java.io.*,java.awt.Color,com.lowagie.text.*,com.lowagie.text.pdf.*\"%> <% response.setContentType( \"application/pdf\" ); Document document = new Document(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); PdfWriter writer=PdfWriter.getInstance( document, buffer ); document.open(); document.add(new Paragraph(\"Hello World\")); document.close(); DataOutput output = new DataOutputStream( response.getOutputStream() ); byte[] bytes = buffer.toByteArray(); response.setContentLength(bytes.length); for( int i = 0; i < bytes.length; i++ ) { output.writeByte( bytes[i] ); } %>

ii)通过Servlet生成

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; public void doGet(javax/servlet/http/HttpServletRequest.java.html\" target=\"_blank\">HttpServletRequest request, javax/servlet/http/HttpServletResponse.java.html\" target=\"_blank\">HttpServletResponse response) throws java/io/IOException.java.html\" target=\"_blank\">IOException,javax/servlet/ServletException.java.html\" target=\"_blank\">ServletException { Document document = new Document(PageSize.A4, 36,36,36,36); java/io/ByteArrayOutputStream.java.html\" target=\"_blank\">ByteArrayOutputStream ba = new java/io/ByteArrayOutputStream.java.html\" target=\"_blank\">ByteArrayOutputStream(); try { PdfWriter writer = PdfWriter.getInstance(document, ba); document.open(); document.add(new Paragraph(\"Hello World\")); } catch(DocumentException de) { de.printStackTrace(); java/lang/System.java.html\" target=\"_blank\">System.err.println(\"A Document error:\" +de.getMessage()); } document.close(); response.setContentType(\"application/pdf\"); response.setContentLength(ba.size()); javax/servlet/ServletOutputStream.java.html\" target=\"_blank\">ServletOutputStream out = response.getOutputStream(); ba.writeTo(out); out.flush(); }

因篇幅问题不能全部显示,请点此查看更多更全内容