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