iText是一个开发源代码的项目,你可以使用iText方便的实现PDF的输出。
一、iText的下载:
你可以在 查看关于iText的相关信息,包括源代码,文档..
1. itext-src-1.4.zip(源代码)
2. itext-1.4.jar(可以直接导入的jar文件)
3. 亚洲语言包
(或者)
二、示例程序:
首先把上面的这几个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);
-
五、参考文档: