List of usage examples for com.itextpdf.text FontFactoryImp FontFactoryImp
public FontFactoryImp()
From source file:com.masscustsoft.service.ToPdf.java
License:Open Source License
private void getChunks(Paragraph p, final Map it, String text) throws Exception { if (text == null) { text = (String) it.get("text"); }/* w w w . j a v a 2s. c o m*/ if (text != null) { if (text.startsWith("<") || text.endsWith(">")) { //treat as HTML Reader in = new StringReader(text); FontFactoryImp ffi = new FontFactoryImp() { @Override public Font getFont(String fontname, String encoding, boolean embedded, float size, int style, BaseColor color, boolean cached) { if (size == Font.UNDEFINED) size = getSize(it); if (style == Font.UNDEFINED) style = getStyle(it); if (color == null) color = getColor(it, "color"); return super.getFont(fontname, encoding, embedded, size, style, color, cached); } }; HashMap map = new HashMap(); map.put("font_factory", ffi); List list = DirectHtmlWorker.parse2List(in, null, null, map); for (int i = 0; i < list.size(); i++) { Object el = list.get(i); if (el instanceof Paragraph) { Paragraph pp = (Paragraph) el; applyFont(pp, it); p.add(pp); } } return; } List<BaseFont> base = new ArrayList<BaseFont>(); String st = text; StringBuffer buf = new StringBuffer(); for (int i = 0; i < st.length(); i++) { char c = st.charAt(i); if (!compatible(base, c)) { processMacro(buf.toString(), base.get(0), it, p); buf.delete(0, buf.length()); base.clear(); } buf.append(c); } if (buf.length() > 0) { processMacro(buf.toString(), base.get(0), it, p); } } }
From source file:ro.nextreports.engine.exporter.PdfExporter.java
License:Apache License
private PdfPCell renderPdfCell(BandElement bandElement, Object value, int gridRow, int rowSpan, int colSpan, boolean image, int column) { Map<String, Object> style = buildCellStyleMap(bandElement, value, gridRow, column, colSpan); FontFactoryImp fact = new FontFactoryImp(); com.itextpdf.text.Font fnt;//from w w w. ja v a 2 s . c o m if (bandElement != null) { fontName = (String) style.get(StyleFormatConstants.FONT_NAME_KEY); int size = ((Float) style.get(StyleFormatConstants.FONT_SIZE)).intValue(); fnt = getFont(size); } else { fnt = getFont(10); } PdfPCell cell; if (image) { if (value == null) { cell = new PdfPCell(new Phrase(IMAGE_NOT_FOUND)); } else { ImageBandElement ibe = (ImageBandElement) bandElement; try { byte[] imageBytes = getImage((String) value); cell = getImageCell(ibe, imageBytes, column, colSpan); } catch (Exception e) { cell = new PdfPCell(new Phrase(IMAGE_NOT_LOADED)); } } } else if (bandElement instanceof HyperlinkBandElement) { Hyperlink hyperlink = ((HyperlinkBandElement) bandElement).getHyperlink(); Anchor anchor = new Anchor(hyperlink.getText(), fnt); anchor.setReference(hyperlink.getUrl()); Phrase ph = new Phrase(); ph.add(anchor); cell = new PdfPCell(ph); } else if (bandElement instanceof ReportBandElement) { Report report = ((ReportBandElement) bandElement).getReport(); ExporterBean eb = null; try { eb = getSubreportExporterBean(report); PdfExporter subExporter = new PdfExporter(eb); subExporter.export(); PdfPTable innerTable = subExporter.getTable(); cell = new PdfPCell(innerTable); } catch (Exception e) { cell = new PdfPCell(); e.printStackTrace(); } finally { if ((eb != null) && (eb.getResult() != null)) { eb.getResult().close(); } } } else if ((bandElement instanceof VariableBandElement) && (VariableFactory .getVariable(((VariableBandElement) bandElement).getVariable()) instanceof TotalPageNoVariable)) { try { cell = new PdfPCell(Image.getInstance(total)); } catch (BadElementException e) { cell = new PdfPCell(new Phrase("NA")); } } else if (bandElement instanceof ImageColumnBandElement) { try { String v = StringUtil.getValueAsString(value, null); if (StringUtil.BLOB.equals(v)) { cell = new PdfPCell(new Phrase(StringUtil.BLOB)); } else { byte[] bytes = StringUtil.decodeImage(v); cell = getImageCell(bandElement, bytes, column, colSpan); } } catch (Exception e) { e.printStackTrace(); cell = new PdfPCell(new Phrase(IMAGE_NOT_LOADED)); } } else { String stringValue; if (style.containsKey(StyleFormatConstants.PATTERN)) { stringValue = StringUtil.getValueAsString(value, (String) style.get(StyleFormatConstants.PATTERN), getReportLanguage()); } else { stringValue = StringUtil.getValueAsString(value, null, getReportLanguage()); } if (stringValue == null) { stringValue = ""; } if (stringValue.startsWith("<html>")) { StringReader reader = new StringReader(stringValue); List<Element> elems = new ArrayList<Element>(); try { elems = HTMLWorker.parseToList(reader, new StyleSheet()); Phrase ph = new Phrase(); for (int i = 0; i < elems.size(); i++) { Element elem = (Element) elems.get(i); ph.add(elem); } cell = new PdfPCell(ph); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Phrase ph = new Phrase(stringValue, fnt); cell = new PdfPCell(ph); } } else { Phrase ph = new Phrase(stringValue, fnt); cell = new PdfPCell(ph); } } cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); cell.setUseDescender(true); // needed for a cell without padding cell.setMinimumHeight(MINIMUM_HEIGHT); // needed if there is a row in which all cells are empty if (bandElement != null) { cell.setRotation(bandElement.getTextRotation()); } if (colSpan > 1) { cell.setColspan(colSpan); } if (rowSpan > 1) { cell.setRowspan(rowSpan); } if (style != null) { updateFont(style, fnt); if (style.containsKey(StyleFormatConstants.BACKGROUND_COLOR)) { Color val = (Color) style.get(StyleFormatConstants.BACKGROUND_COLOR); cell.setBackgroundColor(new BaseColor(val)); } if (style.containsKey(StyleFormatConstants.HORIZONTAL_ALIGN_KEY)) { if (StyleFormatConstants.HORIZONTAL_ALIGN_LEFT .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) { cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); } if (StyleFormatConstants.HORIZONTAL_ALIGN_RIGHT .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) { cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); } if (StyleFormatConstants.HORIZONTAL_ALIGN_CENTER .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) { cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); } } if (style.containsKey(StyleFormatConstants.VERTICAL_ALIGN_KEY)) { if (StyleFormatConstants.VERTICAL_ALIGN_TOP .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) { cell.setVerticalAlignment(Element.ALIGN_TOP); } if (StyleFormatConstants.VERTICAL_ALIGN_MIDDLE .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) { cell.setVerticalAlignment(Element.ALIGN_MIDDLE); } if (StyleFormatConstants.VERTICAL_ALIGN_BOTTOM .equals(style.get(StyleFormatConstants.VERTICAL_ALIGN_KEY))) { cell.setVerticalAlignment(Element.ALIGN_BOTTOM); } } if (style.containsKey(StyleFormatConstants.PADDING_LEFT)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_LEFT); cell.setPaddingLeft(val); } if (style.containsKey(StyleFormatConstants.PADDING_RIGHT)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_RIGHT); cell.setPaddingRight(val); } if (style.containsKey(StyleFormatConstants.PADDING_TOP)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_TOP); cell.setPaddingTop(val); } if (style.containsKey(StyleFormatConstants.PADDING_BOTTOM)) { Float val = (Float) style.get(StyleFormatConstants.PADDING_BOTTOM); cell.setPaddingBottom(val); } cell.setBorderWidth(0); if (style.containsKey(StyleFormatConstants.BORDER_LEFT)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_LEFT); cell.setBorderWidthLeft(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_LEFT_COLOR); cell.setBorderColorLeft(new BaseColor(color)); } if (style.containsKey(StyleFormatConstants.BORDER_RIGHT)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_RIGHT); cell.setBorderWidthRight(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_RIGHT_COLOR); cell.setBorderColorRight(new BaseColor(color)); } if (style.containsKey(StyleFormatConstants.BORDER_TOP)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_TOP); cell.setBorderWidthTop(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_TOP_COLOR); cell.setBorderColorTop(new BaseColor(color)); } if (style.containsKey(StyleFormatConstants.BORDER_BOTTOM)) { Float val = (Float) style.get(StyleFormatConstants.BORDER_BOTTOM); cell.setBorderWidthBottom(val / 2); Color color = (Color) style.get(StyleFormatConstants.BORDER_BOTTOM_COLOR); cell.setBorderColorBottom(new BaseColor(color)); } // for subreports we use default no wrap if (cell.getTable() == null) { cell.setNoWrap(true); if (bandElement != null) { if (bandElement.isWrapText()) { cell.setNoWrap(false); } } } // to see a background image all cells must not have any background! if (bean.getReportLayout().getBackgroundImage() != null) { cell.setBackgroundColor(null); } } return cell; }