上一次我们用iText这个工具在Java环境下操作PDF文件,现在我们换一个工具:BFO
iText的确小巧,但是功能也有限制,只能简单的生成PDF文件,BFO却不同,能设置字体、版面等元素。
Snap12
最新版本是:2.11.4
官方的解释: The Big Faceless Java PDF Library is the smartest Java PDF library for creating, editing, displaying and printing Acrobat PDF documents.
 
我的开发环境:JDK1.6+MyEclipse7+Tomcat5.5.27
首先在MyEclipse中新建一个项目BFO,将下载的jar文件放入该项目的类库中。
Snap1
 
新建一个Class(类)CreateBook.java,Package(包)为com.bfo 代码如下:
package com.bfo;
import org.faceless.pdf2.*;
import java.util.Locale;
import java.awt.Color;
import java.util.*;
import java.io.*;
/**
* 创建一个PDF文档,内容来源于一个TXT文本文件。
*
* author:小段
*/
public class CreateBook
{
    private static PDFStyle numstyle;
    private static int pagenum = 1;
    private static PDF pdf;
    private static final String PAGESIZE = "A4-Landscape";
    private static final float WIDTH, HEIGHT;
    static {
        PDFPage page = new PDFPage(PAGESIZE);
        WIDTH  = (page.getWidth()/2) - 100;
        HEIGHT = page.getHeight()    - 100;
    }
    public static void main(String args[]) throws IOException {
        String filename = args.length > 0 ? args[0] : "C:\\bfo.txt";
        // 设置文本源文件,并且创建一个PDF对象。
        pdf = new PDF();
        pdf.setLocale(Locale.ENGLISH);
        // 创建一个新的PDF格式,设置字体(罗马字体)、字号(11)以及颜色(黑色)。
        PDFStyle textstyle = new PDFStyle();
        textstyle.setFont(new StandardFont(StandardFont.TIMES), 11);
        textstyle.setFillColor(Color.black);
        textstyle.setTextAlign(PDFStyle.TEXTALIGN_JUSTIFY);
        numstyle = new PDFStyle();
        numstyle.setFont(new StandardFont(StandardFont.TIMES), 8);
        numstyle.setFillColor(Color.black);
        numstyle.setTextAlign(PDFStyle.TEXTALIGN_CENTER);
        LayoutBox chapter = new LayoutBox(WIDTH);
        int chapternumber = 0;
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String line;
        long starttime = System.currentTimeMillis();
        System.out.println(new Date()+": Starting file");
        // 文件开始被读取.
        while ((line=in.readLine())!=null) {
            line = line.trim();
            if (line.length()==0) {
                line = "\n\n";
            } else {
                line += " ";
            }
            // 调用requote方法。
            line = textstyle.getFont().requote(line, pdf.getLocale());
            // 开始将内容写进PDF文档。
            if (line.startsWith("Chapter ")) {
                if (chapternumber>0) {
                    System.out.println(new Date()+": Writing Chapter "+chapternumber);
                    writeChapter(chapter, chapternumber);
                }
                chapternumber++;
                chapter = new LayoutBox(WIDTH);
            }
            chapter.addText(line, textstyle, pdf.getLocale());
        }
        // 将最后一个段落写入PDF文档
        System.out.println(new Date()+": Writing Chapter "+chapternumber);
        writeChapter(chapter, chapternumber);
        System.out.println(new Date()+": Compressing and writing to file");
        OutputStream out = new FileOutputStream("C:\\BFO.pdf");
        pdf.render(out);
        out.close();
        // 显示操作PDF文档的总共时间。
        System.out.println("Total time was "+(System.currentTimeMillis()-starttime)+"ms");
    }
    private static void writeChapter(LayoutBox chapter, int chapternumber) {
        PDFPage page=null;
        boolean firstpage = true;
        float left;
        // 测量文本高度以前,必须清空缓存。
        chapter.flush();
        while (chapter!=null) {
            // 清空布局格式。
            LayoutBox next=null;
            if (chapter.getHeight() > HEIGHT) {
                next = chapter.splitAt(HEIGHT);
            }
            if (pagenum%2 == 1) {
                page = pdf.newPage(PAGESIZE);
                left = 50;
                // 写下页数。
                page.setStyle(numstyle);
                page.drawText("Page "+ pagenum, page.getWidth()/4, 30);
                page.drawText("Page "+ (pagenum+1), 3*page.getWidth()/4, 30);
            } else {
                 left = (page.getWidth()/2)+50;
            }
            page.drawLayoutBox(chapter, left, page.getHeight()-50);
            chapter = next;
            pagenum++;
            // 如果是第一页的话,添加一个书签。
            if (firstpage) {
                pdf.getBookmarks().add(new PDFBookmark("Chapter "+chapternumber, PDFAction.goTo(page)));
                firstpage = false;
            }
        }
        // 确定下一个段落应该写在剩下的页面。
        pagenum |= 1;
    }
}
 
这是我C:\bfo.txt的内容:
Snap5
 
运行CreateBook.java后,控制台显示信息:
Snap2
 
同时发现C盘根目录下生成了bfo.pdf文件。打开内容为:
Snap4
 
这样一来,我们就能将一个TXT文本文件转换成PDF文档。
当然了,上次说的iText工具也支持这样的转换,只不过BFO工具功能比较强大,实际应用更加广泛。