一个用Java iText包动态生成PDF文件的代码例子
首先下载:itext-2.0.1.jar和中文支持包iTextAsian.jar,并加入类路径CLASSPATH。
package com.sunrise.psmis.util;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.commons.lang.StringUtils;
import com.lowagie.text.Cell;
import com.lowagie.text.Chapter;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
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 TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO
TestMain test=new TestMain();
test.genPDF();
}
private Font smallFont; try{ //输出表格Table }
public void genPDF() {
try {
//声明中文字体,使iText支持中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
smallFont = new Font(bfChinese, 12, Font.NORMAL);
} catch (Exception e) {
smallFont = FontFactory.getFont(FontFactory.HELVETICA, 7,
Font.NORMAL, new Color(0, 0, 0));
e.printStackTrace();
}
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("E:\\ITextTest.pdf"));
document.open();
//输出Paragraph段落
document.add(new Paragraph("First page of the document."));
document
.add(new Paragraph(
"Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD,
new Color(255, 150, 200))));
Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(
FontFactory.HELVETICA, 18, Font.BOLDITALIC,
new Color(0, 0, 255)));
//输出章节Chapter
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,
new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph(
"This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
Table t = new Table(3, 2);
t.setBorderColor(new Color(220, 255, 100));
// t.setPadding(5);
// t.setSpacing(5);
t.setBorderWidth(1);
Cell c1 = new Cell(new Chunk("中文",smallFont));
c1.setHeader(true);
t.addCell(c1);
c1 = new Cell(new Chunk("中国汉字",smallFont));
t.addCell(c1);
c1 = new Cell("Header3");
t.addCell(c1);
t.endHeaders();
t.addCell("1.1");
t.addCell("1.2");
t.addCell("1.3");
section1.add(t);
document.add(chapter1);
//输出图片image,支持GIF, Jpeg, PNG, wmf等格式
Image image = Image.getInstance("C:/Documents and Settings/Administrator/My Documents/My Pictures/aishwarya_rai3.jpg");
document.add(image);
document.close();
}catch(Exception e){
e.printStackTrace();
}
}
iText Project description
iText is a library that allows you to generate PDF files on the fly.
The iText classes are very useful for people who need to generate read-only, platform independent documents containing text, lists, tables and images. The library is especially useful in combination with Java(TM) technology-based Servlets: The look and feel of HTML is browser dependent; with iText and PDF you can control exactly how your servlet's output will look.
iText requires JDK 1.4. It's available for free under a multiple license: MPL and LGPL.
Introduction
You will find lots of code samples in the online tutorial iText by Example, but for a nice and simple introduction to the tool, please read the articles about iText that was published on the site of PDFDev, AdobePress and IBM (Note that the IBM article was translated into Japanese Chinese, and Korean). More experienced iText users will certainly benefit from the article published on O'Reilly Network.
Technical Requirements
In order to be able to use these classes you should be acquainted with JAVA (TM) programming.
You'll also need the following Software:
If for some reason the library should have to work with an earlier version of the JDK, you will have to compile a slightly altered version of the source yourself (with JDK 1.2 or 1.3).
Getting started with iText
There are three pages that are extremely important if you want to work with iText.