ITEXT操作PDF,非常简单:
(本方法使用itext1.0.4版本,高版本的itext5.5.0同样兼容,改变一下导入包路径就好了)
1,HELLOWORLD项目
package demo;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class HelloWorld {
/**
* 生成一个简单的PDF文件:HelloWorld.pdf
*
* 作者:小段
*/
public static void main(String[] args) {
System.out.println("Hello World");
//第一步:创建一个document对象。
Document document = new Document();
try {
//使用本地字库宋体simsun
BaseFont bfChinese = BaseFont.createFont("c://windows//fonts//simsun.ttc,1",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
//使用itext提供的字体,额外需要下载itextasion.jar,此方法因为itextasion有几个版本,所以很难成功
// 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);
// // 第二步:
// 创建一个PdfWriter实例,
// 将文件输出流指向一个文件。
PdfWriter.getInstance(document,new FileOutputStream("C:\\HelloWorld.pdf"));
// 第三步:打开文档。
document.open();
// 第四步:在文档中增加一个段落。
document.add(new Paragraph("中文",fontChinese));
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// 第五步:关闭文档。
document.close();
// 检验程序是否正常运行到这里。
System.out.println("快去看看吧");
}
}
创建很简单,难点在于显示中文,最好的方法就是使用本地字库,因为大部分电脑都有宋体;
2,创建表格:
package demo;
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class Table {
public static void main(String[] args) {
System.out.println("My First PdfPTable");
// 步骤 1: 创建一个document对象
Document document = new Document();
try {
//使用本地中文字体,simsun.ttc是本地的宋体
BaseFont bfChinese = BaseFont.createFont("c://windows//fonts//simsun.ttc,1",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
// 步骤 2:
// 我们为document创建一个监听,并把PDF流写到文件中
PdfWriter.getInstance(document, new FileOutputStream("c:\\MyFirstTable.pdf"));
// 步骤 3:打开文档
document.open();
//创建一个有3列的表格
PdfPTable table = new PdfPTable(3);
//定义一个表格单元
PdfPCell cell = new PdfPCell(new Paragraph("显示中文标题,成功",fontChinese));
//定义一个表格单元的跨度
cell.setColspan(3);
//把单元加到表格中
table.addCell(cell);
//把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
table.addCell("1.3");
table.addCell("2.3");
table.addCell("3.3");
//重新定义单元格
cell = new PdfPCell(new Paragraph("cell test1"));
//定义单元格的框颜色
cell.setBorderColor(new Color(255, 0, 0));
//把单元格加到表格上,默认为一个单元
table.addCell(cell);
//重新定义单元格
cell = new PdfPCell(new Paragraph("cell test2"));
//定义单元格的跨度
cell.setColspan(2);
//定义单元格的背景颜色
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
//增加到表格上
table.addCell(cell);
//增加到文档中
document.add(table);
} catch (DocumentException de) {
System.err.println(de.getMessage());
} catch (IOException ioe) {
System.err.println(ioe.getMessage());
}
// 步骤 5:关闭文档
document.close();
}
}
原文:http://my.oschina.net/acitiviti/blog/371983