List of usage examples for com.lowagie.text ExceptionConverter ExceptionConverter
public ExceptionConverter(Exception ex)
From source file:nl.knaw.dans.common.lang.pdf.ExtendedHtmlWorker.java
License:Apache License
public void endDocument() { try {// w w w . jav a 2 s . co m for (int k = 0; k < stack.size(); ++k) document.add((Element) stack.elementAt(k)); if (currentParagraph != null) document.add(currentParagraph); currentParagraph = null; } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:nl.knaw.dans.common.lang.pdf.ExtendedHtmlWorker.java
License:Apache License
public void startElement(String tag, HashMap h) { if (!tagsSupported.containsKey(tag)) return;// w w w .j a v a2 s.c om try { style.applyStyle(tag, h); String follow = (String) FactoryProperties.followTags.get(tag); if (follow != null) { HashMap prop = new HashMap(); prop.put(follow, null); cprops.addToChain(follow, prop); return; } FactoryProperties.insertStyle(h); if (tag.equals("a")) { cprops.addToChain(tag, h); if (currentParagraph == null) currentParagraph = new Paragraph(); stack.push(currentParagraph); currentParagraph = new Paragraph(); return; } if (tag.equals("br")) { if (currentParagraph == null) currentParagraph = new Paragraph(); currentParagraph.add(factoryProperties.createChunk("\n", cprops)); return; } if (tag.equals("font") || tag.equals("span")) { cprops.addToChain(tag, h); return; } if (tag.equals("img")) { String src = (String) h.get("src"); if (src == null) return; cprops.addToChain(tag, h); Image img = null; if (interfaceProps != null) { ImageProvider ip = (ImageProvider) interfaceProps.get("img_provider"); if (ip != null) img = ip.getImage(src, h, cprops, document); if (img == null) { HashMap images = (HashMap) interfaceProps.get("img_static"); if (images != null) { Image tim = (Image) images.get(src); if (tim != null) img = Image.getInstance(tim); } else { if (!src.startsWith("http")) { // relative src references only String baseurl = (String) interfaceProps.get("img_baseurl"); if (baseurl != null) { src = baseurl + src; img = Image.getInstance(src); } } } } } if (img == null) { if (!src.startsWith("http")) { String path = cprops.getProperty("image_path"); if (path == null) path = ""; src = new File(path, src).getPath(); } img = Image.getInstance(src); } String align = (String) h.get("align"); String width = (String) h.get("width"); String height = (String) h.get("height"); String before = cprops.getProperty("before"); String after = cprops.getProperty("after"); if (before != null) img.setSpacingBefore(Float.parseFloat(before)); if (after != null) img.setSpacingAfter(Float.parseFloat(after)); float wp = lengthParse(width, (int) img.getWidth()); float lp = lengthParse(height, (int) img.getHeight()); if (wp > 0 && lp > 0) img.scalePercent(wp > lp ? lp : wp); else if (wp > 0) img.scalePercent(wp); else if (lp > 0) img.scalePercent(lp); img.setWidthPercentage(0); if (align != null) { endElement("p"); int ralign = Image.MIDDLE; if (align.equalsIgnoreCase("left")) ralign = Image.LEFT; else if (align.equalsIgnoreCase("right")) ralign = Image.RIGHT; img.setAlignment(ralign); Img i = null; boolean skip = false; if (interfaceProps != null) { i = (Img) interfaceProps.get("img_interface"); if (i != null) skip = i.process(img, h, cprops, document); } if (!skip) document.add(img); cprops.removeChain(tag); } else { cprops.removeChain(tag); if (currentParagraph == null) currentParagraph = FactoryProperties.createParagraph(cprops); currentParagraph.add(new Chunk(img, 0, 0)); } return; } endElement("p"); if (tag.equals("h1") || tag.equals("h2") || tag.equals("h3") || tag.equals("h4") || tag.equals("h5") || tag.equals("h6")) { if (!h.containsKey("size")) { int v = 7 - Integer.parseInt(tag.substring(1)); h.put("size", Integer.toString(v)); } cprops.addToChain(tag, h); return; } if (tag.equals("ul")) { if (pendingLI) endElement("li"); skipText = true; cprops.addToChain(tag, h); com.lowagie.text.List list = new com.lowagie.text.List(false, 10); list.setListSymbol("\u2022"); String indent = ((String) h.get("indent")); if (indent != null && indent.matches("[0-9]+")) { list.setSymbolIndent(Integer.valueOf(indent)); } stack.push(list); return; } if (tag.equals("ol")) { if (pendingLI) endElement("li"); skipText = true; cprops.addToChain(tag, h); com.lowagie.text.List list = new com.lowagie.text.List(true, 10); String type = ((String) h.get("type")); if (type != null && type.toLowerCase().equals("a")) { list.setLettered(true); list.setNumbered(false); list.setLowercase(type.equals("a")); } String indent = ((String) h.get("indent")); if (indent != null && indent.matches("[0-9]+")) { list.setSymbolIndent(Integer.valueOf(indent)); } stack.push(list); return; } if (tag.equals("li")) { if (pendingLI) endElement("li"); skipText = false; pendingLI = true; cprops.addToChain(tag, h); stack.push(FactoryProperties.createListItem(cprops)); return; } if (tag.equals("div") || tag.equals("body")) { cprops.addToChain(tag, h); return; } if (tag.equals("pre")) { if (!h.containsKey("face")) { h.put("face", "Courier"); } cprops.addToChain(tag, h); isPRE = true; return; } if (tag.equals("p")) { cprops.addToChain(tag, h); currentParagraph = FactoryProperties.createParagraph(h); return; } if (tag.equals("tr")) { if (pendingTR) endElement("tr"); skipText = true; pendingTR = true; cprops.addToChain("tr", h); return; } if (tag.equals("td") || tag.equals("th")) { if (pendingTD) endElement(tag); skipText = false; pendingTD = true; cprops.addToChain("td", h); stack.push(new IncCell(tag, cprops)); return; } if (tag.equals("table")) { cprops.addToChain("table", h); IncTable table = new IncTable(h); stack.push(table); tableState.push(new boolean[] { pendingTR, pendingTD }); pendingTR = pendingTD = false; skipText = true; return; } } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:nl.knaw.dans.common.lang.pdf.ExtendedHtmlWorker.java
License:Apache License
public void endElement(String tag) { if (!tagsSupported.containsKey(tag)) return;// ww w .j a v a 2 s . c o m try { String follow = (String) FactoryProperties.followTags.get(tag); if (follow != null) { cprops.removeChain(follow); return; } if (tag.equals("font") || tag.equals("span")) { cprops.removeChain(tag); return; } if (tag.equals("a")) { if (currentParagraph == null) currentParagraph = new Paragraph(); ALink i = null; boolean skip = false; if (interfaceProps != null) { i = (ALink) interfaceProps.get("alink_interface"); if (i != null) skip = i.process(currentParagraph, cprops); } if (!skip) { String href = cprops.getProperty("href"); if (href != null) { ArrayList chunks = currentParagraph.getChunks(); for (int k = 0; k < chunks.size(); ++k) { Chunk ck = (Chunk) chunks.get(k); ck.setAnchor(href); } } } Paragraph tmp = (Paragraph) stack.pop(); Phrase tmp2 = new Phrase(); tmp2.add(currentParagraph); tmp.add(tmp2); currentParagraph = tmp; cprops.removeChain("a"); return; } if (tag.equals("br")) { return; } if (currentParagraph != null) { if (stack.empty()) document.add(currentParagraph); else { Object obj = stack.pop(); if (obj instanceof TextElementArray) { TextElementArray current = (TextElementArray) obj; current.add(currentParagraph); } stack.push(obj); } } currentParagraph = null; if (tag.equals("ul") || tag.equals("ol")) { if (pendingLI) endElement("li"); skipText = false; cprops.removeChain(tag); if (stack.empty()) return; Object obj = stack.pop(); if (!(obj instanceof com.lowagie.text.List)) { stack.push(obj); return; } if (stack.empty()) document.add((Element) obj); else ((TextElementArray) stack.peek()).add(obj); return; } if (tag.equals("li")) { pendingLI = false; skipText = true; cprops.removeChain(tag); if (stack.empty()) return; Object obj = stack.pop(); if (!(obj instanceof ListItem)) { stack.push(obj); return; } if (stack.empty()) { document.add((Element) obj); return; } Object list = stack.pop(); if (!(list instanceof com.lowagie.text.List)) { stack.push(list); return; } ListItem item = (ListItem) obj; ((com.lowagie.text.List) list).add(item); ArrayList cks = item.getChunks(); if (!cks.isEmpty()) item.getListSymbol().setFont(((Chunk) cks.get(0)).getFont()); stack.push(list); return; } if (tag.equals("div") || tag.equals("body")) { cprops.removeChain(tag); return; } if (tag.equals("pre")) { cprops.removeChain(tag); isPRE = false; return; } if (tag.equals("p")) { cprops.removeChain(tag); return; } if (tag.equals("h1") || tag.equals("h2") || tag.equals("h3") || tag.equals("h4") || tag.equals("h5") || tag.equals("h6")) { cprops.removeChain(tag); return; } if (tag.equals("table")) { if (pendingTR) endElement("tr"); cprops.removeChain("table"); IncTable table = (IncTable) stack.pop(); PdfPTable tb = table.buildTable(); tb.setSplitRows(true); if (stack.empty()) document.add(tb); else ((TextElementArray) stack.peek()).add(tb); boolean state[] = (boolean[]) tableState.pop(); pendingTR = state[0]; pendingTD = state[1]; skipText = false; return; } if (tag.equals("tr")) { if (pendingTD) endElement("td"); pendingTR = false; cprops.removeChain("tr"); ArrayList cells = new ArrayList(); IncTable table = null; while (true) { Object obj = stack.pop(); if (obj instanceof IncCell) { cells.add(((IncCell) obj).getCell()); } if (obj instanceof IncTable) { table = (IncTable) obj; break; } } table.addCols(cells); table.endRow(); stack.push(table); skipText = true; return; } if (tag.equals("td") || tag.equals("th")) { pendingTD = false; cprops.removeChain("td"); skipText = true; return; } } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:org.apache.maven.doxia.module.itext.ITextFont.java
License:Apache License
/** * Convenience method to get a defined MonoSpaced font depending the wanted style and size. * * @param style the font style./*from w w w .j a v a 2s .co m*/ * @param size the font size. * @param color the font color. * @return a font the font. */ public static Font getMonoSpacedFont(int style, float size, Color color) { try { return new Font(BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, false), size, style, color); } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:org.cyberoam.iview.charts.Chart.java
License:Open Source License
/** * This Event handler Method adds Header and Footer in PDF File *//* w w w . ja va 2 s . c om*/ public void onEndPage(PdfWriter writer, Document document) { try { if (document.getPageNumber() > 1) { String seperator = System.getProperty("file.separator"); //String path=System.getProperty("catalina.home") +seperator+"webapps" +seperator+"ROOT" + seperator + "images" + seperator; String path = InitServlet.contextPath + seperator + "images" + seperator; Image imgHead = Image.getInstance(path + "iViewPDFHeader.JPG"); Image imgFoot = Image.getInstance(path + "iViewPDFFooter.JPG"); Rectangle page = document.getPageSize(); PdfPTable head = new PdfPTable(1); head.addCell(imgHead); head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin() - 10, page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); PdfPTable foot = new PdfPTable(1); foot.addCell(imgFoot); foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); foot.writeSelectedRows(0, -1, document.leftMargin() - 10, document.bottomMargin() + 24, writer.getDirectContent()); } } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:org.drools.verifier.doc.DroolsDocsComponentFactory.java
License:Apache License
public void onEndPage(PdfWriter writer, Document document) { try {/*from w w w .j a va2s. co m*/ Image image = Image.getInstance(DroolsDocsBuilder.class.getResource("guvnor-webapp.png")); // TODO this image never existed image.setAlignment(Image.RIGHT); image.scaleAbsolute(100, 30); Rectangle page = document.getPageSize(); PdfPTable head = new PdfPTable(2); PdfPCell cell1 = new PdfPCell(image); cell1.setHorizontalAlignment(Element.ALIGN_LEFT); cell1.setBorder(0); head.addCell(cell1); PdfPCell cell2 = new PdfPCell(new Phrase(currentDate, DroolsDocsComponentFactory.HEADER_FOOTER_TEXT)); cell2.setHorizontalAlignment(Element.ALIGN_RIGHT); cell2.setBorder(0); head.addCell(cell2); head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:org.eclipse.birt.report.engine.emitter.postscript.truetypefont.TrueTypeFont.java
License:Open Source License
/** * Reads a <CODE>String</CODE> from the font file as bytes using the * Cp1252 encoding.//ww w. ja v a 2 s .c om * * @param length * the length of bytes to read * @return the <CODE>String</CODE> read * @throws IOException * the font file could not be read */ protected String readStandardString(int length) throws IOException { byte buf[] = new byte[length]; rf.readFully(buf); try { return new String(buf, WINANSI); } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:org.eclipse.birt.report.engine.emitter.postscript.truetypefont.TrueTypeFont.java
License:Open Source License
private String getStandardString(byte[] source, int index, int length) { assert (source.length >= index + length); try {// ww w . j ava 2s .c o m return new String(source, index, length, WINANSI); } catch (UnsupportedEncodingException e) { throw new ExceptionConverter(e); } }
From source file:org.kuali.kfs.module.purap.pdf.BulkReceivingPdf.java
License:Open Source License
/** * Overrides the method in PdfPageEventHelper from itext to create and set the headerTable and set its logo image if * there is a logoImage to be used, creates and sets the nestedHeaderTable and its content. * /* w ww. jav a2s .com*/ * @param writer The PdfWriter for this document. * @param document The document. * @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ public void onOpenDocument(PdfWriter writer, Document document) { try { loadHeaderTable(); // initialization of the template tpl = writer.getDirectContent().createTemplate(100, 100); // initialization of the font helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false); } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:org.kuali.kfs.module.purap.pdf.PurchaseOrderPdf.java
License:Open Source License
/** * Overrides the method in PdfPageEventHelper from itext to create and set the headerTable and set its logo image if * there is a logoImage to be used, creates and sets the nestedHeaderTable and its content. * * @param writer The PdfWriter for this document. * @param document The document./*w w w .j av a2s. c om*/ * @see com.lowagie.text.pdf.PdfPageEventHelper#onOpenDocument(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */ @Override public void onOpenDocument(PdfWriter writer, Document document) { if (LOG.isDebugEnabled()) { LOG.debug("onOpenDocument() started. isRetransmit is " + isRetransmit); } try { float[] headerWidths = { 0.20f, 0.80f }; headerTable = new PdfPTable(headerWidths); headerTable.setWidthPercentage(100); headerTable.setHorizontalAlignment(Element.ALIGN_CENTER); headerTable.setSplitLate(false); headerTable.getDefaultCell().setBorderWidth(0); headerTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); headerTable.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER); Image logo = null; if (StringUtils.isNotBlank(logoImage)) { try { logo = Image.getInstance(logoImage); } catch (IOException e) { LOG.info("The logo image [" + logoImage + "] is not available. Defaulting to the default image."); } } if (logo == null) { // if we don't use images headerTable.addCell(new Phrase(new Chunk(""))); } else { logo.scalePercent(3, 3); headerTable.addCell(new Phrase(new Chunk(logo, 0, 0))); } // Nested table for titles, etc. float[] nestedHeaderWidths = { 0.70f, 0.30f }; nestedHeaderTable = new PdfPTable(nestedHeaderWidths); nestedHeaderTable.setSplitLate(false); PdfPCell cell; // New nestedHeaderTable row cell = new PdfPCell(new Paragraph(po.getBillingName(), ver_15_normal)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderWidth(0); nestedHeaderTable.addCell(cell); cell = new PdfPCell(new Paragraph(" ", ver_15_normal)); cell.setBorderWidth(0); nestedHeaderTable.addCell(cell); // New nestedHeaderTable row if (isRetransmit) { cell = new PdfPCell(new Paragraph(po.getRetransmitHeader(), ver_15_normal)); } else { cell = new PdfPCell(new Paragraph("PURCHASE ORDER", ver_15_normal)); } cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderWidth(0); nestedHeaderTable.addCell(cell); Paragraph p = new Paragraph(); p.add(new Chunk("PO Number: ", ver_11_normal)); p.add(new Chunk(po.getPurapDocumentIdentifier().toString(), cour_16_bold)); cell = new PdfPCell(p); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBorderWidth(0); nestedHeaderTable.addCell(cell); if (!po.getPurchaseOrderAutomaticIndicator()) { // Contract manager name goes on non-APOs. // New nestedHeaderTable row, spans both columns p = new Paragraph(); p.add(new Chunk("Contract Manager: ", ver_11_normal)); p.add(new Chunk(po.getContractManager().getContractManagerName(), cour_7_normal)); cell = new PdfPCell(p); cell.setColspan(2); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBorderWidth(0); nestedHeaderTable.addCell(cell); } // Add the nestedHeaderTable to the headerTable cell = new PdfPCell(nestedHeaderTable); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorderWidth(0); headerTable.addCell(cell); // initialization of the template tpl = writer.getDirectContent().createTemplate(100, 100); // initialization of the font helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false); } catch (Exception e) { throw new ExceptionConverter(e); } }