iText生成PDF入门实例
最近想把网站的文章导出为PDF文件,所以又得学学怎样导出PDF。iText是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF文档,而且可以将HTML网页转化为PDF文件,同时它可以很方便的和web或者其他应用整合使用。
iText的官网:http://www.itextpdf.com
现在最新版是itext-5.3.5.zip
使用就很简单的了,只要把itextpdf-5.3.5.jar添加到classpath即可
iText生成PDF入门实例
环境准备好了,现在开始一个最简单的helloworld,直接看源码
package com.naxsu.utils;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
/**
* java导出PDF
*
* @author huangyineng http://www.656463.com
* @date 2013-1-30 下午1:09:53
* @description
*/
public class ItextUtil {
public static void main(String[] args) throws Exception {
String pdfPath = "F://iText-hello.pdf";
createPdf(pdfPath);
}
public static void createPdf(String filename)
throws DocumentException,IOException {
// step 1:创建Document对象
Document document = new Document();
// step 2:获取PdfWriter实例
PdfWriter.getInstance(document,
new FileOutputStream(filename));
// step 3:打开Document
document.open();
// step 4:添加内容
document.add(new Paragraph("Hello World!"));
// step 5:关闭打开的Document
document.close();
}
}
本节先到些结束,下节说一个很经典的话题,那就是解决中文问题
本文链接:iText生成PDF入门实例,由领悟书生原创
转载请注明出处【http://www.656463.com/article/349】