1,所需包 iText.jar iTextAsian.jar(這個包是用來支持中文)
2.列子
package com.pdf;
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
public class WriterPDF2 {
public static void main(String[] args){
//實例化對象
Document document = new Document(PageSize.A4);
//建立讀寫器
try {
//實列化不同的類,可以生成不同的文件
PdfWriter.getInstance(document, new FileOutputStream("c:\\on
//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();
}
}
}