需要的兩個包及下載地址:
(1)iText.jar:http://download.csdn.net/source/296416
(2)iTextAsian.jar(用來進行中文的轉換):http://download.csdn.net/source/172399
下面是代碼示例:
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class ITextDemo {
public boolean iTextTest() {
try {
/** 實例化文檔對象 */
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
/** 創建 PdfWriter 對象 */
PdfWriter.getInstance(document,// 文檔對象的引用
new FileOutputStream("d://ITextTest.pdf"));//文件的輸出路徑+文件的實際名稱
document.open();// 打開文檔
/** pdf文檔中中文字體的設置,注意一定要添加iTextAsian.jar包 */
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);//加入document:
/** 向文檔中添加內容,創建段落對象 */
document.add(new Paragraph("First page of the document."));// Paragraph添加文本
document.add(new Paragraph("我們是害蟲", FontChinese)); //FontChinese:爲中文字體的設置
/** 創建章節對象 */
Paragraph title1 = new Paragraph("第一章", FontChinese);
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
/** 創建章節中的小節 */
Paragraph title11 = new Paragraph("表格的添加", FontChinese);
Section section1 = chapter1.addSection(title11);
/** 創建段落並添加到小節中 */
Paragraph someSectionText = new Paragraph("下面展示的爲3 X 2 表格.",
FontChinese);
section1.add(someSectionText);
/** 創建表格對象(包含行列矩陣的表格) */
Table t = new Table(3, 2);// 2行3列
t.setBorderColor(new Color(220, 255, 100));
t.setPadding(5);
t.setSpacing(5);
t.setBorderWidth(1);
Cell c1 = new Cell(new Paragraph("第一格", FontChinese));
t.addCell(c1);
c1 = new Cell("Header2");
t.addCell(c1);
c1 = new Cell("Header3");
t.addCell(c1);
// 第二行開始不需要new Cell()
t.addCell("1.1");
t.addCell("1.2");
t.addCell("1.3");
section1.add(t);
/** 創建章節中的小節 */
Paragraph title13 = new Paragraph("列表的添加", FontChinese);
Section section3 = chapter1.addSection(title13);
/** 創建段落並添加到小節中 */
Paragraph someSectionText3 = new Paragraph("下面展示的爲列表.", FontChinese);
section3.add(someSectionText3);
/** 創建列表並添加到pdf文檔中 */
List l = new List(true, true, 10);// 第一個參數爲true,則創建一個要自行編號的列表,
// 如果爲false則不進行自行編號
l.add(new ListItem("First item of list"));
l.add(new ListItem("第二個列表", FontChinese));
section3.add(l);
document.add(chapter1);
/** 創建章節對象 */
Paragraph title2 = new Paragraph("第二章", FontChinese);
Chapter chapter2 = new Chapter(title2, 1);
chapter2.setNumberDepth(0);
/** 創建章節中的小節 */
Paragraph title12 = new Paragraph("png圖片添加", FontChinese);
Section section2 = chapter2.addSection(title12);
/** 添加圖片 */
section2.add(new Paragraph("圖片添加: 餅圖", FontChinese));
Image png = Image.getInstance("D:/pie.png");//圖片的地址
section2.add(png);
document.add(chapter2);
document.close();
return true;
} catch (Exception e2) {
System.out.println(e2.getMessage());
}
return false;
}
public static void main(String args[]) {
System.out.println(new ITextDemo().iTextTest());
}
}
註釋:如果發現中文無法顯示的時候,可以在你需要顯示中文的地方先new Paragraph("",FontChinese);