iText是一個開發源代碼的項目,你可以使用iText方便的實現PDF的輸出。
一、iText的下載:
你可以在 http://www.lowagie.com/iText/ 查看關於iText的相關信息,包括源代碼,文檔..
1. itext-src-1.4.zip(源代碼)
http://jaist.dl.sourceforge.net/sourceforge/itext/itext-src-1.4.zip
2. itext-1.4.jar(可以直接導入的jar文件)
http://jaist.dl.sourceforge.net/sourceforge/itext/itext-1.4.jar
3. 亞洲語言包
http://itextdocs.lowagie.com/downloads/iTextAsian.jar
(或者)
http://itextdocs.lowagie.com/downloads/iTextAsianCmaps.jar
http://prdownloads.sourceforge.net/itext/iTextAsian.jar
二、示例程序:
首先把上面的這幾個jar包的完整路徑添加到環境變量 classpath 中,然後再下面的程序
中導入相應的包
java 代碼
-
-
-
-
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
-
import com.lowagie.text.*;
-
import com.lowagie.text.pdf.PdfWriter;
-
-
public class HelloWorld {
-
-
public static void main(String[] args) {
-
-
System.out.println("Hello World");
-
-
-
Document document = new Document();
-
-
try
-
{
-
-
-
PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
-
-
-
document.addTitle("Hello World example");
-
document.addAuthor("Bruno Lowagie");
-
document.addSubject("This example explains how to add metadata.");
-
document.addKeywords("iText, Hello World, step 3, metadata");
-
document.addCreator("My program using iText");
-
-
-
document.open();
-
-
-
document.add(new Paragraph("Hello World!"));
-
-
}
-
catch (DocumentException de)
-
{
-
System.err.println(de.getMessage());
-
}
-
catch (IOException ioe)
-
{
-
System.err.println(ioe.getMessage());
-
}
-
-
-
document.close();
-
}
-
}
-
編譯運行以後,我們可以在運行的目錄發現生成的HelloWorld.pdf,打開可以看到我們寫的文字:
三、中文問題:
由於iText不支持東亞語言,我們下載了iTextAsian.jar 以後,就可以在PDF裏面寫中文:
java 代碼
-
-
-
-
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
-
import com.lowagie.text.*;
-
import com.lowagie.text.pdf.PdfWriter;
-
import com.lowagie.text.pdf.BaseFont;
-
import com.lowagie.text.Font;
-
import java.awt.Color;
-
-
public class AsianTest{
-
-
public static void main(String[] args) {
-
-
-
Document document = new Document();
-
-
try
-
{
-
-
-
PdfWriter.getInstance(document, new FileOutputStream("AsianTest.pdf"));
-
-
-
-
-
-
-
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
-
-
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
-
-
-
document.open();
-
-
-
Paragraph par = new Paragraph("我們",fontChinese);
-
-
document.add(par);
-
-
}
-
catch (DocumentException de)
-
{
-
System.err.println(de.getMessage());
-
}
-
catch (IOException ioe)
-
{
-
System.err.println(ioe.getMessage());
-
}
-
-
-
document.close();
-
}
-
}
-
四、其他問題:(應導入相應的包)
java 代碼
-
1. 換頁:
-
-
document.newPage();
-
-
2. 表格:
-
-
-
Table aTable = new Table(3);
-
int width[] = {25,25,50};
-
aTable.setWidths(width);
-
aTable.setWidth(80);
-
-
aTable.setDefaultHorizontalAlignment(Element.ALIGN_LEFT);
-
aTable.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);
-
aTable.setAutoFillEmptyCells(true);
-
aTable.setPadding(1);
-
aTable.setSpacing(1);
-
aTable.setDefaultCellBorder(0);
-
aTable.setBorder(0);
-
-
Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 數據", fontChinese ));
-
cell.setVerticalAlignment(Element.ALIGN_TOP);
-
cell.setRowspan(3);
-
aTable.addCell(cell);
-
-
aTable.addCell(new Cell("#1"));
-
aTable.addCell(new Cell("#2"));
-
aTable.addCell(new Cell("#3"));
-
-
aTable.addCell(new Cell("#4"));
-
aTable.addCell(new Cell("#5"));
-
aTable.addCell(new Cell("#6"));
-
-
document.add(aTable);
-
-
3. 圖片:
-
-
-
Image img = Image.getInstance("logo.gif");
-
-
-
-
img.setAbsolutePosition(0, 0);
-
-
document.add(img);
-
五、參考文檔: