KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CreateBook


1 // $Id: CreateBook.java,v 1.6 2003/11/03 10:15:38 mike Exp $
2

3 import org.faceless.pdf2.*;
4 import java.util.Locale JavaDoc;
5 import java.awt.Color JavaDoc;
6 import java.util.*;
7 import java.io.*;
8
9 /**
10  * CreateBook - creates a "book" from a text file.
11  *
12  * This example tests the ability of the library to deal with massive
13  * amounts of text. Given a plain text file as the argument, it turns it
14  * into a "book" - basically renders the text on successive pages of
15  * two-column A4.
16  *
17  * If no argument is supplied, it uses the supplied text file - part of
18  * "Great Expectations" by Charles Dickens (modified from the version
19  * available from the excellent "Project Gutenburg" website).
20  *
21  *
22  */

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 JavaDoc 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 JavaDoc args[])
35         throws IOException
36     {
37     String JavaDoc filename = (args.length > 0 ? args[0] : "resources/GreatExpectations.txt");
38
39     // Create a new PDF. Set the locale of the PDF to english, which affects
40
// the style of double-quote (") substitution in the requote() method.
41
//
42
pdf = new PDF();
43     pdf.setLocale(Locale.ENGLISH);
44
45     // Create a new style to render the text in. We use 11pt Times-Roman
46
//
47
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     // Create a new style for the page numbers
53
//
54
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 JavaDoc line;
63
64     long starttime = System.currentTimeMillis();
65     System.out.println(new Date()+": Starting file");
66
67
68     // Read the lines of the file, adding each line to a layout box
69
// when we reach the end of a chapter split the layout box into pages
70
// and draw it.
71
//
72
// An assumption has been made that each chapter of the book will start
73
// with the word "Chapter".
74
//
75
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         // Call requote() to get nicer looking quote characters
85
//
86
line = textstyle.getFont().requote(line, pdf.getLocale());
87
88         // When a chapter has been read the LayoutBox is sent off to be drawn
89
//
90
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     // Write the last chapter
102
//
103
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     // Write the chapter, printing each page side by side. The layout box will be broken up into page height chunks.
112
// Chapters always start on a new odd numbered page.
113
//
114
private static void writeChapter(LayoutBox chapter, int chapternumber)
115     {
116     PDFPage page=null;
117     boolean firstpage = true;
118     float left;
119
120     // Flush the layout box, this needs to be done before measuring the height.
121
chapter.flush();
122
123     while (chapter!=null)
124     {
125         // Split the layoutbox
126
//
127
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         // Draw the page number
137
//
138
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 its the first page of a new chapter add the bookmark
151
//
152
if (firstpage) {
153         pdf.getBookmarks().add(new PDFBookmark("Chapter "+chapternumber, PDFAction.goTo(page)));
154         firstpage = false;
155         }
156     }
157
158     // Make sure next chapter starts on an odd page
159
//
160
pagenum |= 1;
161     }
162 }
163
Popular Tags