使用PDF类库itext2.1.4
javaDoc下载地址:http://nchc.dl.sourceforge.net/sourceforge/itext/iText-docs-2.1.4.tar.gz
iText-2.1.4.jar URL:http://jaist.dl.sourceforge.net/sourceforge/itext/iText-2.1.4.jar
iText-2.1.4-sources.jar URL:http://jaist.dl.sourceforge.net/sourceforge/itext/iText-2.1.4-sources.jar
1.生成简单hello world级别PDF文件
* 引入itext.2.1.4.jar
import com.lowagie.text.*;
* 生成文档对象: Document document = new Document();
* 生成PdfWriter对象: PdfWriter.getInstance(document, new FileOutputStream("d:\\helloworldB.pdf"));
* 打开文档对象: document.open();
* 给文档加入内容对象: document.add(new Chunk("Hello World!"));
Chunk是可加入的最小单元,其它可加入对象还有Phrase Paragraph等
* 关闭文档对象: document.close();
2.更改默认分隔符,当文本内容太长需要换行时,itext会根据分隔符截断文本进行换行
* 使用setSplitCharacter(SplitCharacter splitCharacter)方法
* SplitCharacter是一个接口,需要我们传递一个类实现它的isSplitCharacter方法
参见splitCharacter()方法或itext文档
* 无意间发现: 在对一个对象改变内容后,有可能会影响到前面已经使用过这个对象的地方
Chunk chunk = new Chunk("a");
Paragraph paragraph = new Paragraph(chunk);
//此时paragraph 的内容应该是"a";
chunk.append("bc");
//此时paragraph 的内容就改变成是"abc";
参见testChange()方法
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.SplitCharacter;
import com.lowagie.text.pdf.PdfChunk;
import com.lowagie.text.pdf.PdfWriter;
/**
* @author wfy
*
*/
public class HelloWorld {
public HelloWorld(){
}
public static void main(String[] args){
try {
HelloWorld hw = new HelloWorld();
//生成一个简单PDF文件
//hw.createHelloWorldPdf();
//测试分隔符
//hw.splitCharacter();
hw.testChange();
} catch (Exception e) {
e.printStackTrace();
}
}
public void createHelloWorldPdf() throws Exception{
Document document = new Document();
//PdfWriter pdfWriterA = PdfWriter.getInstance(document, new FileOutputStream("d:\\helloworldA.pdf"));
PdfWriter pdfWriterB = PdfWriter.getInstance(document, new FileOutputStream("d:\\helloworldB.pdf"));
document.open();
//pdfWriterA.pause();
document.add(new Paragraph("Writer to :"));
//pdfWriterA.resume();
Chunk chunk = new Chunk("Hello World", FontFactory.getFont(FontFactory.COURIER, 20, Font.ITALIC, new Color(255, 0, 0)));
document.add(chunk);
chunk.setTextRise((float) 2.0);
Paragraph paragraph1 = new Paragraph("this is a Paragraph");
paragraph1.add(new Chunk("add a chunk", FontFactory.getFont(FontFactory.COURIER_BOLD, 20, Font.SYMBOL, new Color(10, 0 , 250))));
paragraph1.add(new Paragraph( "insert"));
paragraph1.add("String");
paragraph1.add(new Paragraph(" Paragraph2"));
Phrase phrase1 = new Phrase("string1 ", new Font(Font.TIMES_ROMAN));
phrase1.add("string2 ");
phrase1.add(new Phrase("string3", new Font(Font.HELVETICA)));
document.add(phrase1);
document.add(paragraph1);
document.close();
}
public void splitCharacter() throws Exception{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("d:\\splitchar.pdf"));
document.open();
Chunk chunk = new Chunk("app-*.xml/i text ");
//更改默认分隔符" "和"-"为"/"
chunk.setSplitCharacter(new SplitCharacter(){
public boolean isSplitCharacter(int start, int current, int end,
char[] cc, PdfChunk[] ck) {
char c;
if (ck == null)
c = cc[current];
else
c = (char) ck[Math.min(current, ck.length - 1)].getUnicodeEquivalent(cc[current]);
//用此句替换下面被注释的默认设置
if(c == '/')
return true;
/*if (c <= ' ' || c == '-') {
return true;
}*/
if (c < 0x2e80)
return false;
return ((c >= 0x2e80 && c < 0xd7a0)
|| (c >= 0xf900 && c < 0xfb00)
|| (c >= 0xfe30 && c < 0xfe50)
|| (c >= 0xff61 && c < 0xffa0));
}
});
Paragraph paragraph1 = new Paragraph("01" + chunk);
paragraph1.add(chunk);
paragraph1.add(chunk);
paragraph1.add(chunk);
paragraph1.add(chunk);
paragraph1.add(chunk);
paragraph1.add(chunk);
//此语句影响到前面使用过chunk的地方
paragraph1.add(chunk.append(" wfy ").toString());
document.add(paragraph1);
//document.add(new Phrase("WFY"));
paragraph1.clear();
paragraph1.add("02" + chunk+ "wfy789 ");
paragraph1.add(chunk);
paragraph1.add(chunk);
paragraph1.add(chunk);
paragraph1.add(chunk);
paragraph1.add(chunk);
document.add(paragraph1);
document.close();
}
public void testChange() throws Exception{
Document doc = new Document();
PdfWriter.getInstance(doc, new FileOutputStream("d:\\testChange.pdf"));
doc.open();
Chunk chunk = new Chunk("a");
doc.add(new Paragraph("before change: " + chunk));
//在chunk对象更改之前已经定义好了paragraph对象
Paragraph p = new Paragraph(chunk);
chunk.append("bc");
doc.add(new Chunk("after change: " + chunk));
doc.add(p);
doc.close();
}
}