iText导出pdf、word、图片

iText导出pdf、word、图片

一、前言
在企业的信息系统中,报表处理一直占比较重要的作用,本文将介绍一种生成PDF报表的Java组件–iText。通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超级连接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题。

二、iText简介
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。

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

*三、建立第一个PDF文档 *
用iText生成PDF文档需要5个步骤:

①建立com.lowagie.text.Document对象的实例。   
Document document = new Document();   
②建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。   
PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));   
③打开文档。   
document.open();   
④向文档中添加内容。   
document.add(new Paragraph("Hello World"));   
⑤关闭文档。   
document.close();

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

建立com.lowagie.text.Document对象的实例
com.lowagie.text.Document对象的构建函数有三个,分别是:
public Document();
public Document(Rectangle pageSize);
public Document(Rectangle pageSize, int marginLeft, int marginRight, int marginTop, int marginBottom);
构建函数的参数pageSize是文档页面的大小,对于第一个构建函数,页面的大小为A4,同Document(PageSize.A4)的效果一样;对于第三个构建函数,参数marginLeft、marginRight、marginTop、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)对象与之关联。通过书写器(Writer)对象可以将具体文档存盘成需要的格式,如com.lowagie.text.PDF.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)

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)
package junit.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import org.junit.Test;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter2;

/**
 * 导出图片、word、pdf 
 * 
 * @author 林计钦
 * @version 1.0 Feb 7, 2014 9:02:38 AM
 */
public class PdfAndWordTest {
    /**
     * 导出pdf
     */
    @Test
    public void exportPdf() {
        Document document=null;
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 设置中文字体
            Font headFont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小

            //第一步:创建一个document对象。
            document = new Document(); 
            //第二步:创建一个PdfWriter实例,将文件输出流指向一个文件。
            PdfWriter.getInstance(document, new FileOutputStream("D:/test/123.pdf"));
            //第三步:打开文档。 
            document.open();
            Paragraph title = new Paragraph("你好,Pdf!", headFont);
            //第四步:在文档中增加一个段落。    
            document.add(title);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(document!=null){
                //第五步:关闭文档。 
                document.close();
            }
        }

    }

    /**
     * 导出word
     */
    @Test
    public void exportWord() {
        Document document=null;
        try {
            document = new Document(); 
            RtfWriter2.getInstance(document, new FileOutputStream("D:/test/word.doc"));
            document.open();
            Paragraph title = new Paragraph("你好,Word!");
            document.add(title);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }finally{
            if(document!=null){
                document.close();
            }
        }
    }

    /**
     * 导出图片
     */
    @Test
    public void exportImg() {
        Document document=null;
        try {
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 设置中文字体
            Font headFont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小

            document = new Document(); 
            PdfWriter.getInstance(document, new FileOutputStream("D:/test/img.pdf"));
            //设定文档的作者
            document.addAuthor("林计钦"); //测试无效
            document.open();
            document.add(new Paragraph("你好,Img!", headFont));
            //读取一个图片
            Image image = Image.getInstance("D:/test/1.gif");
            //插入一个图片
            document.add(image);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(document!=null){
                document.close();
            }
        }
    }  
}

参考:www.cnblogs.com/linjiqin/p/3539283...

导出例子:

iText导出pdf、word、图片

本作品采用《CC 协议》,转载必须注明作者和本文链接
MissYou123
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!