001: package com.xoetrope.export.pdf;
002:
003: import java.net.URL;
004: import java.io.File;
005: import java.io.FileOutputStream;
006: import java.util.ArrayList;
007: import java.util.Hashtable;
008: import java.util.Vector;
009:
010: import com.xoetrope.export.Exporter;
011: import com.xoetrope.export.TableBlock;
012: import com.xoetrope.export.TableModelBlock;
013: import com.xoetrope.export.ArrayArrayBlock;
014: import com.xoetrope.export.FragmentBlock;
015: import com.xoetrope.export.Block;
016:
017: import net.xoetrope.xui.XProject;
018: import net.xoetrope.xui.style.XStyleManager;
019: import net.xoetrope.xui.style.XStyleEx;
020: import net.xoetrope.xui.PageSupport;
021: import net.xoetrope.xui.style.XStyle;
022:
023: import com.lowagie.text.Chunk;
024: import com.lowagie.text.Image;
025: import com.lowagie.text.Document;
026: import com.lowagie.text.Paragraph;
027: import com.lowagie.text.Phrase;
028: import com.lowagie.text.DocumentException;
029: import com.lowagie.text.pdf.PdfWriter;
030: import com.lowagie.text.Font;
031: import com.lowagie.text.Element;
032: import com.lowagie.text.PageSize;
033: import com.lowagie.text.Rectangle;
034:
035: /**
036: * Export a PDF document
037: *
038: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
039: * the GNU Public License (GPL), please see license.txt for more details. If
040: * you make commercial use of this software you must purchase a commercial
041: * license from Xoetrope.</p>
042: * <p> $Revision: 1.2 $</p>
043: */
044: public class PdfExporter extends Exporter {
045: protected Document document;
046: protected XStyleManager styleMgr;
047: protected Paragraph currentParagraph;
048: private float[] margins;
049: private Rectangle pageSize;
050:
051: /**
052: * @param instance the current page or object that provides the methods or fields used in the evaluation
053: */
054: public PdfExporter(XProject project, Object instance) {
055: super (project, instance);
056: styleMgr = currentProject.getStyleManager();
057: document = new Document();
058:
059: pageSize = PageSize.A4;
060: margins = new float[4];
061: margins[0] = 36;
062: margins[1] = 36;
063: margins[2] = 36;
064: margins[3] = 36;
065: }
066:
067: /**
068: * Get the content type supported by this exporter
069: * @return "PDF"
070: */
071: public String getContentType() {
072: return "PDF";
073: }
074:
075: /**
076: * Overloaded from the base abstract function. Retrieve a new
077: * PdfTableBlock from this exporter.
078: *
079: *
080: * @return a new TableBlock Object
081: */
082: public TableBlock getTableBlock() {
083: PdfTableBlock block = new PdfTableBlock();
084: return block;
085: }
086:
087: /**
088: * Overloaded from the base abstract function. Retrieve a new
089: * PdfTableModelBlock from this exporter.
090: *
091: *
092: * @return a new TableModelBlock Object
093: */
094: public TableModelBlock getTableModelBlock() {
095: PdfTableModelBlock Block = new PdfTableModelBlock(this );
096: return Block;
097: }
098:
099: /**
100: * Retrieve a new ArrayArrayBlock from this exporter
101: *
102: * @return a new ArrayArrayBlock Object
103: */
104: public ArrayArrayBlock getArrayArrayBlock() {
105: PdfArrayArrayBlock Block = new PdfArrayArrayBlock(this );
106: return Block;
107: }
108:
109: /**
110: * Overloaded from the base abstract function. Retrieve a new
111: * PdfFragmentBlock from this exporter.
112: *
113: *
114: * @return a new FragmentBlock Object
115: */
116: public FragmentBlock getFragmentBlock() {
117: return new PdfFragmentBlock(this );
118: }
119:
120: /**
121: * Retrieve a new Block from this exporter.
122: * @return a new Block Object
123: */
124: public Block getFreeFormBlock() {
125: return new Block();
126: }
127:
128: public void addBlock(Block blockExport) {
129: super .addBlock(blockExport);
130: }
131:
132: /**
133: * Export to the named file.
134: * @param fileName the name of the file to be exported to.
135: */
136: public void export(String fileName) {
137: try {
138: createDocument(fileName);
139: int numItems = blocks.size();
140: for (int i = 0; i < numItems; i++) {
141: Block block = (Block) blocks.get(i);
142: exportItem(block, true);
143: }
144: writeToFile(fileName);
145: } catch (DocumentException ex) {
146: ex.printStackTrace();
147: }
148: }
149:
150: private void exportItem(Block blockItem, boolean top)
151: throws DocumentException {
152: Vector subBlockItems = blockItem.getBlocks();
153: if (top)
154: exportBlockItem(blockItem);
155:
156: int numSubItems = subBlockItems.size();
157: for (int i = 0; i < numSubItems; i++) {
158: Block subBlockItem = (Block) subBlockItems.get(i);
159: exportBlockItem(subBlockItem);
160: }
161: }
162:
163: private void exportBlockItem(Block subBlockItem)
164: throws DocumentException {
165: if (subBlockItem instanceof PdfRowBlockItem) {
166: if (currentParagraph != null)
167: document.add(currentParagraph);
168: currentParagraph = getStyledParagraph("", subBlockItem
169: .getStyle());
170: } else {
171: String tag = subBlockItem.getTag();
172: if (tag != null) {
173: tag = tag.toLowerCase();
174: if (tag.equals("p")) {
175: String content = subBlockItem.getValue();
176: if ((content != null) && (content.length() > 0)) {
177: if (currentParagraph != null)
178: document.add(currentParagraph);
179: currentParagraph = getStyledParagraph(content,
180: subBlockItem.getStyle());
181: } else {
182: if (currentParagraph != null)
183: document.add(currentParagraph);
184: if (subBlockItem.getBlocks().size() == 0)
185: document.add(new Chunk(Chunk.NEWLINE));
186: currentParagraph = getStyledParagraph(content,
187: subBlockItem.getStyle());
188: }
189: } else if (tag.equals("replaceable")) {
190: String content = subBlockItem.getValue();
191: if (content != null)
192: currentParagraph.add(getStyledChunk(content,
193: subBlockItem.getStyle()));
194: } else if (tag.equalsIgnoreCase("img")) {
195: try {
196: String style = subBlockItem.getStyle();
197: URL imgUrl = currentProject
198: .findResource(subBlockItem
199: .getAttrib("src"));
200: Image image = Image.getInstance(imgUrl);
201: image.setSpacingBefore(0);
202: image.setSpacingAfter(0);
203: XStyle xstyle = styleMgr.getStyle(style);
204: if (xstyle instanceof XStyleEx) {
205: String alignment = ((XStyleEx) xstyle)
206: .getStyleAsString("alignment");
207: if ("right".equals(alignment)) {
208: image.setAlignment(Image.ALIGN_RIGHT);
209: image.setIndentationLeft(10.0F);
210: } else if ("left".equals(alignment)) {
211: image.setAlignment(Image.ALIGN_LEFT);
212: image.setIndentationRight(10.0F);
213: } else if ("center".equals(alignment)) {
214: image.setAlignment(Image.ALIGN_CENTER);
215: image.setIndentationRight(10.0F);
216: image.setIndentationLeft(10.0F);
217: } else if ("textwrap".equals(alignment)) {
218: image.setAlignment(Image.TEXTWRAP);
219: image.setIndentationRight(10.0F);
220: }
221: }
222: //currentParagraph.add( image );
223: document.add(image);
224: } catch (Exception e) {
225: e.printStackTrace();
226: }
227: }
228: } else {
229: String content = subBlockItem.getValue();
230: if (content != null)
231: currentParagraph.add(getStyledChunk(content,
232: subBlockItem.getStyle()));
233: }
234: }
235: }
236:
237: private Paragraph getStyledParagraph(String content,
238: String styleName) {
239: if (styleName != null) {
240: XStyle xstyle = styleMgr.getStyle(styleName);
241: if (xstyle != null) {
242: String fontFace = xstyle
243: .getStyleAsString(xstyle.FONT_FACE);
244: int fontSize = xstyle.getStyleAsInt(xstyle.FONT_SIZE);
245: int fontWeight = xstyle
246: .getStyleAsInt(xstyle.FONT_WEIGHT);
247: int fontItalic = xstyle
248: .getStyleAsInt(xstyle.FONT_ITALIC);
249:
250: Font font = new Font(Font.HELVETICA, fontSize,
251: (fontItalic > 0 ? Font.ITALIC : 0)
252: | (fontWeight > 0 ? Font.BOLD : 0),
253: xstyle.getStyleAsColor(xstyle.COLOR_FORE));
254:
255: Paragraph p = new Paragraph(content, font);
256: if (xstyle instanceof XStyleEx) {
257: String alignment = ((XStyleEx) xstyle)
258: .getStyleAsString("alignment");
259: if ("right".equals(alignment))
260: p.setAlignment(Element.ALIGN_RIGHT);
261: else if ("center".equals(alignment))
262: p.setAlignment(Element.ALIGN_CENTER);
263: else if ("justified".equals(alignment))
264: p.setAlignment(Element.ALIGN_JUSTIFIED);
265: else
266: p.setAlignment(Element.ALIGN_LEFT);
267: } else
268: p.setAlignment(Element.ALIGN_LEFT);
269:
270: return p;
271: }
272: }
273:
274: return new Paragraph(content);
275: }
276:
277: private Chunk getStyledChunk(String content, String styleName) {
278: if (styleName != null) {
279: XStyle xstyle = styleMgr.getStyle(styleName);
280: if (xstyle != null) {
281: String fontFace = xstyle
282: .getStyleAsString(xstyle.FONT_FACE);
283: int fontSize = xstyle.getStyleAsInt(xstyle.FONT_SIZE);
284: int fontWeight = xstyle
285: .getStyleAsInt(xstyle.FONT_WEIGHT);
286: int fontItalic = xstyle
287: .getStyleAsInt(xstyle.FONT_ITALIC);
288:
289: Font font = new Font(Font.HELVETICA, fontSize,
290: (fontItalic > 0 ? Font.ITALIC : 0)
291: | (fontWeight > 0 ? Font.BOLD : 0),
292: xstyle.getStyleAsColor(xstyle.COLOR_FORE));
293:
294: Chunk p = new Chunk(content, font);
295:
296: return p;
297: }
298: }
299:
300: return new Chunk(content);
301: }
302:
303: public void createDocument(String fileName) {
304: try {
305: document = new Document();
306: document.setPageSize(pageSize);
307: document.setMargins(margins[0], margins[1], margins[2],
308: margins[3]);
309: PdfWriter.getInstance(document, new FileOutputStream(
310: fileName));
311: document.open();
312: } catch (Exception e) {
313: e.printStackTrace();
314: }
315: }
316:
317: /**
318: * Retrieve the file extension
319: * @return the extension ".pdf"
320: */
321: public String getFileExtension() {
322: return ".pdf";
323: }
324:
325: public void writeToFile(String fileName) throws DocumentException {
326: if (currentParagraph != null)
327: document.add(currentParagraph);
328: document.close();
329: }
330:
331: public void setProperties(ArrayList marginList) {
332: String page = (String) marginList.get(0);
333:
334: if (page.compareTo("A4") == 0)
335: pageSize = PageSize.A4;
336:
337: margins[0] = ((Float) marginList.get(1)).floatValue();
338: margins[1] = ((Float) marginList.get(2)).floatValue();
339: margins[2] = ((Float) marginList.get(3)).floatValue();
340: margins[3] = ((Float) marginList.get(4)).floatValue();
341: }
342: }
|