1,所需包 iText.jar iTextAsian.jar(这个包是用来支持中文)
2.列子
package com.pdf;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Table;
import com.lowagie.text.html.HtmlWriter;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.rtf.RtfWriter;
import com.lowagie.text.rtf.RtfWriter2;
public class WriterPDF2 {
public static void main(String[] args){
//实例化对象
Document document = new Document(PageSize.A4);
//建立读写器
try {
//实列化不同的类,可以生成不同的文件
PdfWriter.getInstance(document, new FileOutputStream("c:\\one.pdf"));//生成PDF
//HtmlWriter.getInstance(document, new FileOutputStream("c:\\11.doc"));//本来是写Html的 但也可以用来写word 或 excel
//RtfWriter2.getInstance(document, new FileOutputStream("c:\\22.doc"));//生成doc
//打开文档
document.open();
//向文档中添加内容
Table table = new Table(3);
//table.setBorder(1);
table.setBorderWidth(1);
table.setBorderColor(new Color(0,0,255));
table.setPadding(5);
table.setSpacing(5);
Cell cell = new Cell("header");
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
table.endHeaders();
cell = new Cell("aaaaaaaaaaaaaaaaa");
cell.setRowspan(2);
cell.setBorderColor(new Color(255,0,0));
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
table.addCell(cell);
table.addCell("cell test2");
document.add(table);
document.close();
//关闭文档
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}