1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.cms.core.pdf;
17
18 import java.awt.Color;
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import com.lowagie.text.Chunk;
24 import com.lowagie.text.Document;
25 import com.lowagie.text.Font;
26 import com.lowagie.text.FontFactory;
27 import com.lowagie.text.Paragraph;
28 import com.lowagie.text.Rectangle;
29 import com.lowagie.text.RomanList;
30 import com.lowagie.text.pdf.CMYKColor;
31 import com.lowagie.text.pdf.PdfContentByte;
32 import com.lowagie.text.pdf.PdfPageEventHelper;
33 import com.lowagie.text.pdf.PdfSpotColor;
34 import com.lowagie.text.pdf.PdfTemplate;
35 import com.lowagie.text.pdf.PdfWriter;
36
37 /***
38 * Event Helper, it will be called on-hook.
39 * @see com.lowagie.text.pdf.PdfPageEvent
40 * @see PdfPageEventHelper
41 * @author someda
42 */
43 public class PdfCmsPageEvents extends PdfPageEventHelper {
44
45 private static final float PAGEMARGIN_TOP = 50;
46
47 private static final float PAGEMARGIN_BOTTOM = 50;
48 private static final float PAGEMARGIN_LEFT = 50;
49
50 private static final String CREATER_APPLICATION = "WebUDA Tuigwaa";
51
52 public static final String GENERICTAG_HORIZONTAL = "horizontal";
53 public static final String GENERICTAG_TOC = "toc";
54 public static final String GENERICTAG_CLOSETOC = "closetoc";
55
56 private static final float size_ = 8;
57 private static final String FOOTERPREFIX = "";
58 private static final String FOOTERMIDDLE = " / ";
59
60 private static final float linewidth_ = 0.5f;
61
62 private Font chapterFont_;
63 private Font sectionFont_;
64 private Font subsectionFont_;
65
66 private PdfSpotColor cmykSpc_ = new PdfSpotColor("Tuigwaa", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f));
67
68 private PdfTemplate template_;
69 private PdfContentByte cb_;
70
71 private String headerTitle_;
72 private float headerX_;
73 private float headerY_;
74 private float footerX_;
75
76 private boolean needHeader = true;
77 private boolean needFooter = true;
78 private boolean needSidebar = true;
79
80 private boolean titleFooter = false;
81
82 private List templateList = new ArrayList();
83
84 public PdfCmsPageEvents(String headerTitle, Font defaultFont, String style){
85 super();
86 headerTitle_ = headerTitle;
87 chapterFont_ = FontFactory.getFont(PdfUtils.getChapterfontProperties(defaultFont));
88 sectionFont_ = FontFactory.getFont(PdfUtils.getSectionfontProperties(defaultFont,2));
89 subsectionFont_ = FontFactory.getFont(PdfUtils.getSectionfontProperties(defaultFont,3));
90
91 String[] styleColomns = style.split("-");
92 if(styleColomns.length > 2){
93 String pageStyle = styleColomns[1];
94 if("n".equals(pageStyle)){
95
96 }else if("s".equals(pageStyle)){
97 needHeader = false;
98 needFooter = false;
99 needSidebar = false;
100 }else if("r".equals(pageStyle)){
101 needHeader = false;
102 needSidebar = false;
103 needFooter = false;
104 titleFooter = true;
105 }
106 }
107 }
108
109 public void onGenericTag(PdfWriter writer, Document document,Rectangle rect,String text){
110
111 float y = (rect.top() + rect.bottom())/2;
112 if(GENERICTAG_HORIZONTAL.equals(text)){
113 cb_.setLineWidth(linewidth_);
114 cb_.moveTo(document.left()+20,y);
115 cb_.lineTo(document.right()-20,y);
116 cb_.stroke();
117 }else if(GENERICTAG_TOC.equals(text)){
118 float x = rect.right() + 20;
119 cb_.setLineWidth(linewidth_);
120 cb_.setLineDash(1.0f,2.0f,0f);
121 cb_.moveTo(x,y);
122 cb_.lineTo(document.right()-80,y);
123 cb_.stroke();
124 PdfTemplate currentPageTemplate_ = cb_.createTemplate(10,10);
125 cb_.addTemplate(currentPageTemplate_,document.right()-70,y-3);
126 templateList.add(currentPageTemplate_);
127
128 if(needFooter){
129 document.resetPageCount();
130 needFooter = false;
131 }
132
133 }else if(GENERICTAG_CLOSETOC.equals(text)){
134 needFooter = true;
135
136 }
137 }
138
139 public void onOpenDocument(PdfWriter writer, Document document){
140 cb_ = writer.getDirectContent();
141 template_ = cb_.createTemplate(50,50);
142
143 float width = document.right() - document.left();
144 headerX_ = (PAGEMARGIN_LEFT + width - PdfUtils.BASEFONT_GOTHIC.getWidthPoint(headerTitle_,size_))/2;
145 headerY_ = document.top() + PAGEMARGIN_TOP/2;
146 footerX_ = (PAGEMARGIN_LEFT + width - PdfUtils.BASEFONT_GOTHIC.getWidthPoint(FOOTERPREFIX + " " + FOOTERMIDDLE + " ",size_))/2;
147 }
148
149 public void onEndPage(PdfWriter writer, Document document) {
150
151 cb_.setLineWidth(linewidth_);
152
153 if(needSidebar)
154 renderSidebar(writer,document);
155
156 if(needHeader)
157 renderHeader(writer,document);
158
159 if(needFooter){
160 renderFooter(writer,document);
161 }else if(titleFooter){
162 renderTitleFooter(writer,document);
163 }
164
165 }
166
167 public void onCloseDocument(PdfWriter writer, Document document) {
168
169 template_.beginText();
170 template_.setFontAndSize(PdfUtils.BASEFONT_MINCHO, size_);
171 template_.showText(String.valueOf(writer.getPageNumber() - 1));
172 template_.endText();
173 }
174
175 public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {
176
177 if(titleFooter){
178 titleFooter = false;
179
180 document.setPageCount(1);
181 }
182
183 for (Iterator i = title.getChunks().iterator(); i.hasNext();) {
184 Chunk chunk = (Chunk) i.next();
185 chunk.setFont(chapterFont_);
186 }
187 setPagenumberToTOC(document);
188 }
189
190 public void onSection(PdfWriter writer,Document document,float paragraphPosition, int depth, Paragraph title){
191
192 StringBuffer buf = new StringBuffer();
193 for (Iterator i = title.getChunks().iterator(); i.hasNext();) {
194 Chunk chunk = (Chunk) i.next();
195 buf.append(chunk.content());
196 if(depth > 2){
197 chunk.setFont(subsectionFont_);
198 }else{
199 chunk.setFont(sectionFont_);
200 }
201 }
202 setPagenumberToTOC(document);
203 }
204
205
206 private void renderHeader(PdfWriter writer,Document document){
207
208 cb_.moveTo(document.left(),document.top()+10);
209 cb_.lineTo(document.right(),document.top()+10);
210 cb_.stroke();
211
212
213 cb_.beginText();
214 cb_.setFontAndSize(PdfUtils.BASEFONT_GOTHIC,size_);
215 cb_.setTextMatrix(headerX_, headerY_);
216 cb_.showText(headerTitle_);
217 cb_.endText();
218 }
219
220 private void renderFooter(PdfWriter writer, Document document){
221
222
223 int num = document.getPageNumber();
224 String text = FOOTERPREFIX + num + FOOTERMIDDLE;
225 float len = PdfUtils.BASEFONT_MINCHO.getWidthPoint(text, size_);
226
227 cb_.moveTo(document.left(),PAGEMARGIN_BOTTOM - 5);
228 cb_.lineTo(document.right(),PAGEMARGIN_BOTTOM - 5);
229 cb_.stroke();
230
231
232 cb_.beginText();
233 cb_.setFontAndSize(PdfUtils.BASEFONT_MINCHO,size_);
234 cb_.setTextMatrix(footerX_, PAGEMARGIN_BOTTOM/2);
235 cb_.showText(text);
236 cb_.endText();
237
238
239 cb_.addTemplate(template_,footerX_+len, PAGEMARGIN_BOTTOM/2);
240 }
241
242 private void renderTitleFooter(PdfWriter writer, Document document){
243
244 String text = RomanList.toRomanLowerCase(writer.getPageNumber());
245
246 cb_.moveTo(document.left(),PAGEMARGIN_BOTTOM - 5);
247 cb_.lineTo(document.right(),PAGEMARGIN_BOTTOM - 5);
248 cb_.stroke();
249
250
251 cb_.beginText();
252 cb_.setFontAndSize(PdfUtils.BASEFONT_MINCHO,size_);
253 cb_.setTextMatrix(footerX_, PAGEMARGIN_BOTTOM/2);
254 cb_.showText(text);
255 cb_.endText();
256
257
258 }
259
260
261 private void renderSidebar(PdfWriter writer, Document document){
262
263 cb_.rectangle(0,0,PAGEMARGIN_LEFT/2,document.getPageSize().top());
264 cb_.setColorFill(cmykSpc_,cmykSpc_.getTint());
265 cb_.fill();
266 cb_.resetCMYKColorFill();
267
268
269 cb_.beginText();
270 cb_.setFontAndSize(PdfUtils.BASEFONT_GOTHIC,size_*1.5f);
271 cb_.setColorFill(Color.BLUE);
272 cb_.setTextMatrix(0,1,-1,0,PAGEMARGIN_LEFT/3,document.top()/2);
273 cb_.showText(CREATER_APPLICATION);
274 cb_.resetRGBColorFill();
275 cb_.endText();
276 }
277
278 private void setPagenumberToTOC(Document document){
279 if(templateList.size() > 0){
280 PdfTemplate currentPageTemplate_ = (PdfTemplate)templateList.remove(0);
281 currentPageTemplate_.beginText();
282 currentPageTemplate_.setFontAndSize(PdfUtils.BASEFONT_MINCHO,10);
283 currentPageTemplate_.showText(document.getPageNumber() + "");
284 currentPageTemplate_.endText();
285 }
286 }
287
288 }