[Java] Java 利用BFO操作PDF文件 →→→→→进入此内容的聊天室

来自 , 2020-01-20, 写在 Java, 查看 132 次.
URL http://www.code666.cn/view/be3159ad
  1. import org.faceless.pdf2.*;
  2. import java.util.Locale;
  3. import java.awt.Color;
  4. import java.util.*;
  5. import java.io.*;
  6.  
  7. /**
  8.  * 创建一个PDF文档,内容来源于一个TXT文本文件。
  9.  * 相关包下载 http://big.faceless.org/products/bfopdf-2.11.4.zip
  10.  * author:小段
  11.  */
  12. public class CreateBook {
  13.         private static PDFStyle numstyle;
  14.         private static int pagenum = 1;
  15.         private static PDF pdf;
  16.         private static final String PAGESIZE = "A4-Landscape";
  17.         private static final float WIDTH, HEIGHT;
  18.         static {
  19.                 PDFPage page = new PDFPage(PAGESIZE);
  20.                 WIDTH = (page.getWidth() / 2) - 100;
  21.                 HEIGHT = page.getHeight() - 100;
  22.         }
  23.  
  24.         public static void main(String args[]) throws IOException {
  25.         String filename = args.length > 0 ? args[0] : "C:\\bfo.txt";
  26.         // 设置文本源文件,并且创建一个PDF对象。
  27.         pdf = new PDF();
  28.         pdf.setLocale(Locale.ENGLISH);
  29.         // 创建一个新的PDF格式,设置字体(罗马字体)、字号(11)以及颜色(黑色)。
  30.         PDFStyle textstyle = new PDFStyle();
  31.         textstyle.setFont(new StandardFont(StandardFont.TIMES), 11);
  32.         textstyle.setFillColor(Color.black);
  33.         textstyle.setTextAlign(PDFStyle.TEXTALIGN_JUSTIFY);
  34.         numstyle = new PDFStyle();
  35.         numstyle.setFont(new StandardFont(StandardFont.TIMES), 8);
  36.         numstyle.setFillColor(Color.black);
  37.         numstyle.setTextAlign(PDFStyle.TEXTALIGN_CENTER);
  38.         LayoutBox chapter = new LayoutBox(WIDTH);
  39.         int chapternumber = 0;
  40.         BufferedReader in = new BufferedReader(new FileReader(filename));
  41.         String line;
  42.         long starttime = System.currentTimeMillis();
  43.         System.out.println(new Date()+": Starting file");
  44.         // 文件开始被读取.
  45.         while ((line=in.readLine())!=null) {
  46.             line = line.trim();
  47.             if (line.length()==0) {
  48.                 line = "\n\n";
  49.             } else {
  50.                 line += " ";
  51.             }
  52.             // 调用requote方法。
  53.             line = textstyle.getFont().requote(line, pdf.getLocale());
  54.             // 开始将内容写进PDF文档。
  55.             if (line.startsWith("Chapter ")) {
  56.                 if (chapternumber>0) {
  57.                     System.out.println(new Date()+": Writing Chapter "+chapternumber);
  58.                     writeChapter(chapter, chapternumber);
  59.                 }
  60.                 chapternumber++;
  61.                 chapter = new LayoutBox(WIDTH);
  62.             }
  63.             chapter.addText(line, textstyle, pdf.getLocale());
  64.         }
  65.         // 将最后一个段落写入PDF文档
  66.         System.out.println(new Date()+": Writing Chapter "+chapternumber);
  67.         writeChapter(chapter, chapternumber);
  68.         System.out.println(new Date()+": Compressing and writing to file");
  69.         OutputStream out = new FileOutputStream("C:\\BFO.pdf");
  70.         pdf.render(out);
  71.         out.close();
  72.         // 显示操作PDF文档的总共时间。
  73.         System.out.println("Total time was "+(System.currentTimeMillis()-starttime)+"ms");
  74.     }
  75.  
  76.         private static void writeChapter(LayoutBox chapter, int chapternumber) {
  77.         PDFPage page=null;
  78.         boolean firstpage = true;
  79.         float left;
  80.         // 测量文本高度以前,必须清空缓存。
  81.         chapter.flush();
  82.         while (chapter!=null) {
  83.             // 清空布局格式。
  84.             LayoutBox next=null;
  85.             if (chapter.getHeight() > HEIGHT) {
  86.                 next = chapter.splitAt(HEIGHT);
  87.             }
  88.             if (pagenum%2 == 1) {
  89.                 page = pdf.newPage(PAGESIZE);
  90.                 left = 50;
  91.                 // 写下页数。
  92.                 page.setStyle(numstyle);
  93.                 page.drawText("Page "+ pagenum, page.getWidth()/4, 30);
  94.                 page.drawText("Page "+ (pagenum+1), 3*page.getWidth()/4, 30);
  95.             } else {
  96.                  left = (page.getWidth()/2)+50;
  97.             }
  98.             page.drawLayoutBox(chapter, left, page.getHeight()-50);
  99.             chapter = next;
  100.             pagenum++;
  101.             // 如果是第一页的话,添加一个书签。
  102.             if (firstpage) {
  103.                 pdf.getBookmarks().add(new PDFBookmark("Chapter "+chapternumber, PDFAction.goTo(page)));
  104.                 firstpage = false;
  105.             }
  106.         }
  107.         // 确定下一个段落应该写在剩下的页面。
  108.         pagenum |= 1;
  109.     }}

回复 "Java 利用BFO操作PDF文件"

这儿你可以回复上面这条便签

captcha