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;
}
String filename = args.length > 0 ? args[0] : "C://bfo.txt";
pdf = new PDF();
pdf.setLocale(Locale.ENGLISH);
PDFStyle textstyle = new PDFStyle();
textstyle.setFont(new StandardFont(StandardFont.TIMES), 11);
textstyle.setFillColor(Color.black);
textstyle.setTextAlign(PDFStyle.TEXTALIGN_JUSTIFY);
numstyle.setFont(new StandardFont(StandardFont.TIMES), 8);
numstyle.setFillColor(Color.black);
numstyle.setTextAlign(PDFStyle.TEXTALIGN_CENTER);
int chapternumber = 0;
BufferedReader in = new BufferedReader(new FileReader(filename));
String line;
System.out.println(new Date()+": Starting file");
while ((line=in.readLine())!=null) {
line = line.trim();
if (line.length()==0) {
line = "/n/n";
} else {
line += " ";
}
line = textstyle.getFont().requote(line, pdf.getLocale());
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());
}
System.out.println(new Date()+": Writing Chapter "+chapternumber);
writeChapter(chapter, chapternumber);
OutputStream out = new FileOutputStream("C://BFO.pdf");
pdf.render(out);
out.close();
// 显示操作PDF文档的总共时间。
System.out.println("Total time was "+(System.currentTimeMillis()-starttime)+"ms");
}
PDFPage page=null;
boolean firstpage = true;
float left;
chapter.flush();
// 清空布局格式。
LayoutBox next=null;
if (chapter.getHeight() > HEIGHT) {
next = chapter.splitAt(HEIGHT);
}
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;
}
chapter = next;
pagenum++;
if (firstpage) {
pdf.getBookmarks().add(new PDFBookmark("Chapter "+chapternumber, PDFAction.goTo(page)));
firstpage = false;
}
}
pagenum |= 1;
}
}
509



被折叠的 条评论
为什么被折叠?



