您的位置:首页 > 其它

iText导出pdf、word、图片

2014-12-07 22:11 537 查看
<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.iText</strong><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">:</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="white-space:pre">	</span>著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。</span>


iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。

2. 用iText生成PDF文档需要5个步骤:

1).建立document对象的实例。

<pre name="code" class="java"><pre name="code" class="java">//1).建立document对象的实例
Document document = new Document();


<pre name="code" class="java">//2).建立一个书写器Writer与document对象关联,通过书写器Writer可以将文档写入到磁盘中


PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));
//3).打开文档
document.open();
//4).向文档中添加内容
<pre name="code" class="java">document.add(new Paragraph("Hello World"));
//5).关闭文档

<pre name="code" class="java">document.close();




通过上面的5个步骤,就能产生一个HelloWorld.PDF的文件,文件内容为“Hello World!”;

public Document(); //页面的大小是A4,同Document(PageSize.A4)的效果一样
public Document(Rectangle pageSize);//<span style="font-family: Arial, Helvetica, sans-serif;">构造函数的pageSize是文档页面的大小,如Document(PageSize.A4)</span>
public Document(Rectangle pageSize, int marginLeft, int marginRight, int marginTop, int marginBottom);//参数分别为:页面大小、左、右、上、下的页边距


通过参数pageSize可以设定页面大小、面背景色、以及页面横向/纵向等属性。iText定义了A0-A10、AL、LETTER、HALFLETTER、_11x17、LEDGER、NOTE、B0-B5、ARCH_A-ARCH_E、FLSA和FLSE等纸张类型,也可以通过Rectangle pageSize = new Rectangle(144, 720);自定义纸张。通过Rectangle方法rotate()可以将页面设置成横向。

书写器(Writer)对象

一旦文档(document)对象建立好之后,需要建立一个或多个书写器(Writer对象与之关联)。通过书写器对象可以将具体文档存盘成需要的格式,如com.lowagie.text.pdf.PdfWriter.PdfWriter可以将文档存成PDF文件,com.lowagie.text.html.HtmlWriter可以将文档存成html文件。

设定文档属性

在文档打开之前,可以设定文档的标题、主题、作者、关键字、装订方式、创建者、生产者、创建日期等属性。调用的方法分别是:

public boolean addTitle(String title)
public boolean addSubject(String subject)
public boolean addKeywords(String keywords)
public boolean addAuthor(String author)
public boolean addCreator(String creator)
public boolean addProducer()
public boolean addCreationDate()
public boolean addHeader(String name, String content)//此方法对于PDF文档无效,addHeader仅对html文档有效,用于添加文档的头信息


新的页面产生之前,可以设定页面的大小、书脚注(HeaderFooter)等信息,调用的方法是:

public boolean setPageSize(Rectangle pageSize)
public boolean add(Watermark watermark)
public void removeWatermark()
public void setHeader(HeaderFooter header)
public void resetHeader()
public void setFooter(HeaderFooter footer)
public void resetFooter()
public void resetPageCount()
public void setPageCount(int pageN)


如果要设定第一页的页面属性,这些方法必须在文档打开之前调用。

对于PDF文档,iText还提供了文档的显示属性,通过调用书写器的 setViewerPreferences方法可以控制文档打开时Acrobat Reader的显示属性,如是否单页显示、是否全屏显示、是否隐藏状态条等属性。

另外,iText也提供了对PDF文件iText的安全保护,通过书写器(Writer)的setEncryption方法,可以设定文档的用户口令、只读、可打印等属性。

所有向文档添加的内容都是以对象为单位的,如Phrase、Paragraph、Table、Graphic对象等。比较常用的是段落对象,用于向文档中添加一段文字。

3. 文本处理

iText中用文本块(Chunk)、短语(Phrase)和段落(Paragraph)处理文本。文本块(Chunk)是处理文本的最小单位,有一串格式(包括字体、颜色、大小)的字条串组成。

<pre name="code" class="java">Chunk chunk1 = new Chunk("This text is underlined", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE));




短语(Paragraph)由一个或多个文本块(Chunk)组成,短语(Phrase)也可以设定字体,但对于其中以设定过字体的文本块(Chunk)无效。通过短语成员函数add可以将一个文本块回到短语中。

段落(Paragraph)由一个或多个文本块或短语组成,相word文档中的段落概念,同样可以设定段落的字体大小、颜色等属性。另外也可以设定段落的首先缩进、对齐方式(左对齐、右对齐、居中对齐)。通过函数setAlignment可以设定段落的对齐方式,setAlignment的参数1为居中对齐、2为右对齐、3为左对齐,默认为左对齐。

4. 表格处理

iText中处理表格的类为:com.lowagie.text.Table和com.lowagie.text.PDF.PDFPTable,对于比较简单的表格处理可以用com.lowagie.text.Table,但是如果要处理复杂的表格,这就需要com.lowagie.text.PDF.PDFPTable进行处理。这里就类com.lowagie.text.Table进行说明。

Table(int columns) //设定表格的列数
Table(int columns, int rows) //列数、行数
Table(Properties attributes)//表格属性

创建表格时必须指定表格的列数,而对于行数可以不用指定。

建立表格之后可以设置表格的属性。

5. 图像处理

iText中处理图像的类为com.lowagie.text.Image,目前iText支持的图像格式有:GIF, Jpeg, PNG, wmf等格式,对于不同的图像格式,iText用同样的构造函数自动识别图像格式。通过下面的代码分别获得gif、jpg、png图像的实例。

图像的位置

图像的位置主要是指图像在文档中的对齐方式、图像和文本的位置关系。IText中通过函数public void setAlignment(int alignment)进行处理,参数alignment为Image.RIGHT、Image.MIDDLE、Image.LEFT分别指右对齐、居中、左对齐;当参数alignment为Image.TEXTWRAP、Image.UNDERLYING分别指文字绕图形显示、图形作为文字的背景显示。这两种参数可以结合以达到预期的效果,如setAlignment(Image.RIGHT|Image.TEXTWRAP)显示的效果为图像右对齐,文字围绕图像显示。

图像的尺寸和旋转

函数public void scaleAbsolute(int newWidth, int newHeight)直接设定显示尺寸;

函数public void scalePercent(int percent)设定显示比例,如scalePercent(50)表示显示的大小为原尺寸的50%;而函数scalePercent(int percentX, int percentY)则图像高宽的显示比例。如果图像需要旋转一定角度之后在文档中显示,可以通过函数public void setRotation(double r)设定,参数r为弧度,如果旋转角度为30度,则参数r=
Math.PI/6。

6. 中文处理

默认的iText字体设置不支持中文字体,需要下载远东字体包iTextAsian.jar,否则不能往PDF文档中输出中文字体。通过下面的代码就可以在文档中使用中文了:

BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
Paragraph pragraph=new Paragraph("你好", FontChinese);


7.iTextDemo

import java.awt.Color;
import java.io.FileOutputStream;

import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
/**
*
* @author
*
*/
public class ITextDemo {
public boolean iTextTest() {
try {
/** 实例化文档对象 */
Document document = new Document(PageSize.A4, 50, 50, 50, 50);

/** 创建 PdfWriter 对象 */
PdfWriter.getInstance(document,// 文档对象的引用
new FileOutputStream("C://Users//LiQiaoqiao//Desktop//demo//ITextTest.pdf"));//文件的输出路径+文件的实际名称
document.open();// 打开文档

/** pdf文档中中文字体的设置,注意一定要添加iTextAsian.jar包 */
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);//加入document:

/** 向文档中添加内容,创建段落对象 */
document.add(new Paragraph("First page of the document."));// Paragraph添加文本
document.add(new Paragraph("我们是害虫", FontChinese)); //FontChinese:为中文字体的设置

/** 创建章节对象 */
Paragraph title1 = new Paragraph("第一章", FontChinese);
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
/** 创建章节中的小节 */
Paragraph title11 = new Paragraph("表格的添加", FontChinese);
Section section1 = chapter1.addSection(title11);
/** 创建段落并添加到小节中 */
Paragraph someSectionText = new Paragraph("下面展示的为3 X 2 表格.",
FontChinese);
section1.add(someSectionText);

/** 创建表格对象(包含行列矩阵的表格) */
Table t = new Table(3, 2);// 2行3列
t.setBorderColor(new Color(220, 255, 100));
t.setPadding(5);
t.setSpacing(5);
t.setBorderWidth(1);
Cell c1 = new Cell(new Paragraph("第一格", FontChinese));
t.addCell(c1);
c1 = new Cell("Header2");
t.addCell(c1);
c1 = new Cell("Header3");
t.addCell(c1);
// 第二行开始不需要new Cell()
t.addCell("1.1");
t.addCell("1.2");
t.addCell("1.3");
section1.add(t);

/** 创建章节中的小节 */
Paragraph title13 = new Paragraph("列表的添加", FontChinese);
Section section3 = chapter1.addSection(title13);
/** 创建段落并添加到小节中 */
Paragraph someSectionText3 = new Paragraph("下面展示的为列表.", FontChinese);
section3.add(someSectionText3);
/** 创建列表并添加到pdf文档中 */
List l = new List(true, true, 10);// 第一个参数为true,则创建一个要自行编号的列表,
// 如果为false则不进行自行编号
l.add(new ListItem("First item of list"));
l.add(new ListItem("第二个列表", FontChinese));
section3.add(l);
document.add(chapter1);

/** 创建章节对象 */
Paragraph title2 = new Paragraph("第二章", FontChinese);
Chapter chapter2 = new Chapter(title2, 1);
chapter2.setNumberDepth(0);
/** 创建章节中的小节 */
Paragraph title12 = new Paragraph("png图片添加", FontChinese);
Section section2 = chapter2.addSection(title12);

/** 添加图片 */
section2.add(new Paragraph("图片添加: 饼图", FontChinese));
Image png = Image.getInstance("D:/pie.png");//图片的地址
section2.add(png);

document.add(chapter2);
document.close();
return true;
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
return false;
}

public static void main(String args[]) {
System.out.println(new ITextDemo().iTextTest());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: