comparison servlet/src/main/java/digilib/pdf/PDFTitlePage.java @ 892:ba1eb2d821a2 mvnify

rearrange sources to maven directory standard
author robcast
date Tue, 19 Apr 2011 18:44:25 +0200
parents servlet/src/digilib/pdf/PDFTitlePage.java@e8668edcb880
children
comparison
equal deleted inserted replaced
891:6584af320296 892:ba1eb2d821a2
1 package digilib.pdf;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.net.MalformedURLException;
6 import java.net.URL;
7
8 import org.apache.log4j.Logger;
9
10 import com.itextpdf.text.Anchor;
11 import com.itextpdf.text.BadElementException;
12 import com.itextpdf.text.Chunk;
13 import com.itextpdf.text.Element;
14 import com.itextpdf.text.FontFactory;
15 import com.itextpdf.text.Image;
16 import com.itextpdf.text.Paragraph;
17
18 import digilib.io.FileOpException;
19 import digilib.servlet.PDFCache;
20 import digilib.servlet.PDFRequest;
21
22 /** A class for the generation of title pages for the generated pdf documents.
23 *
24 *
25 */
26 public class PDFTitlePage {
27
28 private PDFRequest job_info = null;
29 private DigilibInfoReader info_reader= null;
30 protected static Logger logger = Logger.getLogger("digilib.servlet");
31
32
33 /**
34 * Initialize a TitlePage
35 * @param pdfji
36 */
37 public PDFTitlePage(PDFRequest pdfji){
38 job_info = pdfji;
39
40 // use MPIWG-style info.xml
41 info_reader = getInfoXmlReader(pdfji);
42 }
43
44 /**
45 * @param pdfji
46 * @return
47 */
48 protected DigilibInfoReader getInfoXmlReader(PDFRequest pdfji) {
49 try {
50 // try to load ../presentation/info.xml
51 File imgDir = pdfji.getImageJobInformation().getFileDirectory().getDir();
52 File docDir = imgDir.getParentFile();
53 File infoFn = new File(new File(docDir, "presentation"), "info.xml");
54 return new DigilibInfoReader(infoFn.getAbsolutePath());
55 } catch (FileOpException e) {
56 logger.warn("info.xml not found");
57 }
58 return null;
59 }
60
61 /**
62 * generate iText-PDF-Contents for the title page
63 *
64 * @return
65 */
66 public Element getPageContents(){
67 Paragraph content = new Paragraph();
68 content.setAlignment(Element.ALIGN_CENTER);
69
70 // add vertical whitespace
71 for(int i=0; i<8; i++){
72 content.add(Chunk.NEWLINE);
73 }
74
75 // add logo
76 content.add(getLogo());
77 content.add(Chunk.NEWLINE);
78 content.add(Chunk.NEWLINE);
79
80 // add title
81 Anchor title = new Anchor(new Paragraph(getTitle(),FontFactory.getFont(FontFactory.HELVETICA,16)));
82 String burl = job_info.getImageJobInformation().getAsString("base.url");
83
84 title.setReference(burl+"digilib.jsp?fn="+job_info.getImageJobInformation().getAsString("fn"));
85 content.add(title);
86 content.add(Chunk.NEWLINE);
87
88 // add author
89 if(getDate()!=" ")
90 content.add(new Paragraph(getAuthor()+" ("+getDate()+")",FontFactory.getFont(FontFactory.HELVETICA,14)));
91 else
92 content.add(new Paragraph(getAuthor(),FontFactory.getFont(FontFactory.HELVETICA,14)));
93
94 content.add(Chunk.NEWLINE);
95
96 // add page numbers
97 content.add(new Paragraph(getPages(), FontFactory.getFont(FontFactory.HELVETICA, 12)));
98
99
100 content.add(Chunk.NEWLINE);
101 content.add(Chunk.NEWLINE);
102 content.add(Chunk.NEWLINE);
103
104 // add digilib version
105 content.add(new Paragraph(getDigilibVersion(),FontFactory.getFont(FontFactory.HELVETICA,10)));
106
107 for(int i=0; i<8; i++){
108 content.add(Chunk.NEWLINE);
109 }
110 Anchor address = new Anchor(
111 new Paragraph(burl+"digilib.jsp?fn="+job_info.getImageJobInformation().getAsString("fn"), FontFactory.getFont(FontFactory.COURIER, 9))
112 );
113 address.setReference(burl+"digilib.jsp?fn="+job_info.getImageJobInformation().getAsString("fn"));
114
115 content.add(address);
116
117
118 return content;
119 }
120
121 /*
122 * Methods for the different attributes.
123 *
124 */
125
126 private Image getLogo(){
127 try {
128 URL url = new URL(job_info.getDlConfig().getAsString("pdf-logo"));
129 if(url!=null && !url.equals("")){
130 Image logo = Image.getInstance(url);
131 logo.setAlignment(Element.ALIGN_CENTER);
132 return logo;
133 }
134 } catch (BadElementException e) {
135 logger.error(e.getMessage());
136 } catch (MalformedURLException e) {
137 logger.error(e.getMessage());
138 } catch (IOException e) {
139 logger.error(e.getMessage());
140 }
141 return null;
142 }
143
144 private String getTitle(){
145 if(info_reader.hasInfo())
146 return info_reader.getAsString("title");
147 else
148 return job_info.getImageJobInformation().getAsString("fn");
149 }
150
151 private String getAuthor(){
152 if(info_reader.hasInfo())
153 return info_reader.getAsString("author");
154 else
155 return " ";
156 }
157
158 private String getDate(){
159 if(info_reader.hasInfo())
160 return info_reader.getAsString("date");
161 else
162 return " ";
163 }
164
165 private String getPages(){
166 return "Pages "+job_info.getAsString("pgs") + " (scan page numbers)";
167 }
168
169 private String getDigilibVersion(){
170 return "Digilib PDFMaker v."+PDFCache.version;
171 }
172
173 }