滴水穿石Java 生成PDF文件iText使用之入门

iText是开源站点sourceforge的一个项目,是用于生成PDF文档的一个Java类库。支持文本,表格,图形的操作,可以方便的跟 Servlet 进行结合。

使用方法:

1、从官网上下载iText.jar,将其构建到自己的项目中,即安装成功。下载地址:http://sourceforge.net/projects/itext/

2、参照文档可以开始自己的测试代码了。

最简单的分成如下五个步骤:

step1--创建一个Document

step2--得到一个pdfWriter实例

step3--打开创建的Document

step4--添加内容

step5--关闭文档

下面,开始HelloWorld吧:

import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class TestPdf { 
    public static void main(String[] args) throws Exception{
        //step1  
        Document document = new Document();  
        //step2  
        PdfWriter.getInstance(document, new FileOutputStream("first.pdf"));
        //step3  
        document.open();  
        //step4  
        document.add(new Paragraph("Hello World,this is my first pdf!"));  
        //step5  
        document.close();   }}
    }
}

原文地址:https://www.cnblogs.com/nexiyi/p/2808162.html