1 3 import org.faceless.pdf2.*; 4 import java.util.Locale ; 5 import java.awt.Color ; 6 import java.util.*; 7 import java.io.*; 8 9 23 public class CreateBook 24 { 25 private static PDFStyle numstyle; 26 private static int pagenum = 1; 27 private static PDF pdf; 28 29 private static String pagesize = PDF.PAGESIZE_A4_LANDSCAPE; 30 private static float pagewidth = ((new PDFPage(pagesize)).getWidth()-200)/2; 31 private static float pageheight = ((new PDFPage(pagesize)).getHeight())-100; 32 33 34 public static void main(String args[]) 35 throws IOException 36 { 37 String filename = (args.length > 0 ? args[0] : "resources/GreatExpectations.txt"); 38 39 pdf = new PDF(); 43 pdf.setLocale(Locale.ENGLISH); 44 45 PDFStyle textstyle = new PDFStyle(); 48 textstyle.setFont(new StandardFont(StandardFont.TIMES), 11); 49 textstyle.setFillColor(Color.black); 50 textstyle.setTextAlign(PDFStyle.TEXTALIGN_JUSTIFY); 51 52 numstyle = new PDFStyle(); 55 numstyle.setFont(new StandardFont(StandardFont.TIMES), 8); 56 numstyle.setFillColor(Color.black); 57 numstyle.setTextAlign(PDFStyle.TEXTALIGN_CENTER); 58 59 LayoutBox chapter = new LayoutBox(pagewidth); 60 int chapternumber = 0; 61 BufferedReader in = new BufferedReader(new FileReader(filename)); 62 String line; 63 64 long starttime = System.currentTimeMillis(); 65 System.out.println(new Date()+": Starting file"); 66 67 68 while ((line=in.readLine())!=null) 76 { 77 line=line.trim(); 78 if (line.length()==0) { 79 line = "\n\n"; 80 } else { 81 line += " "; 82 } 83 84 line = textstyle.getFont().requote(line, pdf.getLocale()); 87 88 if (line.startsWith("Chapter ")) { 91 if (chapternumber>0) { 92 System.out.println(new Date()+": Writing Chapter "+chapternumber); 93 writeChapter(chapter,chapternumber); 94 } 95 chapternumber++; 96 chapter = new LayoutBox(pagewidth); 97 } 98 chapter.addText(line, textstyle, pdf.getLocale()); 99 } 100 101 System.out.println(new Date()+": Writing Chapter "+chapternumber); 104 writeChapter(chapter,chapternumber); 105 106 System.out.println(new Date()+": Compressing and writing to file"); 107 pdf.render(new FileOutputStream("CreateBook.pdf")); 108 System.out.println("Total time was "+(System.currentTimeMillis()-starttime)+"ms"); 109 } 110 111 private static void writeChapter(LayoutBox chapter, int chapternumber) 115 { 116 PDFPage page=null; 117 boolean firstpage = true; 118 float left; 119 120 chapter.flush(); 122 123 while (chapter!=null) 124 { 125 LayoutBox next=null; 128 if (chapter.getHeight() > pageheight) { 129 next = chapter.splitAt(pageheight); 130 } 131 132 if (pagenum%2==1) { 133 page = pdf.newPage(pagesize); 134 left = 50; 135 136 page.setStyle(numstyle); 139 page.drawText("Page "+ pagenum, page.getWidth()/4, 30); 140 page.drawText("Page "+ (pagenum+1), 3*page.getWidth()/4, 30); 141 142 } else { 143 left = (page.getWidth()/2)+50; 144 } 145 146 page.drawLayoutBox(chapter, left, page.getHeight()-50); 147 chapter = next; 148 pagenum++; 149 150 if (firstpage) { 153 pdf.getBookmarks().add(new PDFBookmark("Chapter "+chapternumber, PDFAction.goTo(page))); 154 firstpage = false; 155 } 156 } 157 158 pagenum |= 1; 161 } 162 } 163 | Popular Tags |