Example usage for com.lowagie.text.rtf.table RtfCell setVerticalAlignment

List of usage examples for com.lowagie.text.rtf.table RtfCell setVerticalAlignment

Introduction

In this page you can find the example usage for com.lowagie.text.rtf.table RtfCell setVerticalAlignment.

Prototype

public void setVerticalAlignment(int value) 

Source Link

Document

Sets the vertical alignment.

Usage

From source file:ro.nextreports.engine.exporter.RtfExporter.java

License:Apache License

private RtfCell renderRtfCell(BandElement bandElement, Object value, int gridRow, int gridColumn, int rowSpan,
        int colSpan, boolean image) {
    Map<String, Object> style = buildCellStyleMap(bandElement, value, gridRow, gridColumn, colSpan);
    String stringValue;/*from ww w  .ja v  a  2s .c o  m*/

    FontFactoryImp fact = new FontFactoryImp();
    Font fnt;
    if (bandElement != null) {
        String fontName = (String) style.get(StyleFormatConstants.FONT_NAME_KEY);
        int size = ((Float) style.get(StyleFormatConstants.FONT_SIZE)).intValue();
        fnt = getFont(fontName, size);
    } else {
        fnt = getFont(10);
    }

    RtfCell cell = null;
    boolean specialCell = false;
    if (image) {
        try {
            if (value == null) {
                cell = new RtfCell(new Phrase(IMAGE_NOT_FOUND));
            } else {
                ImageBandElement ibe = (ImageBandElement) bandElement;
                byte[] imageBytes = getImage((String) value, ibe.getWidth(), ibe.getHeight());
                cell = new RtfCell(Image.getInstance(imageBytes));
            }
        } catch (Exception e) {
            cell = new RtfCell(IMAGE_NOT_LOADED);
        }
        specialCell = true;
    } 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);
        try {
            cell = new RtfCell(ph);
        } catch (BadElementException e) {
            e.printStackTrace();
            cell = new RtfCell(hyperlink.getText());
        }
        specialCell = true;
    } else if (bandElement instanceof ReportBandElement) {
        Report report = ((ReportBandElement) bandElement).getReport();
        ExporterBean eb = null;
        try {
            eb = getSubreportExporterBean(report);
            RtfExporter subExporter = new RtfExporter(eb);
            subExporter.export();
            Table innerTable = subExporter.getTable();
            cell = new RtfCell(innerTable);
        } catch (Exception e) {
            cell = new RtfCell();
            e.printStackTrace();
        } finally {
            if ((eb != null) && (eb.getResult() != null)) {
                eb.getResult().close();
            }
        }
        specialCell = true;
    } else if (bandElement instanceof VariableBandElement) {
        VariableBandElement vbe = (VariableBandElement) bandElement;
        Variable var = VariableFactory.getVariable(vbe.getVariable());
        if (var instanceof PageNoVariable) {
            cell = new RtfCell();
            cell.add(new RtfPageNumber());
            cell.setBorderWidth(0);
            specialCell = true;
        }
    } else if (bandElement instanceof ExpressionBandElement) {
        // special case pageNo inside an expression
        // bandName is not important here (it is used for groupRow
        // computation)
        PrefixSuffix pf = interpretPageNo(bandElement);
        if (pf != null) {
            updateFont(fnt, style);
            cell = new RtfCell();
            if (!"".equals(pf.getPrefix())) {
                cell.add(new Phrase(pf.getPrefix(), fnt));
            }
            cell.add(new RtfPageNumber(fnt));
            if (!"".equals(pf.getSuffix())) {
                cell.add(new Phrase(pf.getSuffix(), fnt));
            }
            specialCell = true;
        }
    } else if (bandElement instanceof ImageColumnBandElement) {
        try {
            String v = StringUtil.getValueAsString(value, null);
            if (StringUtil.BLOB.equals(v)) {
                cell = new RtfCell(new Phrase(StringUtil.BLOB));
            } else {
                ImageColumnBandElement icbe = (ImageColumnBandElement) bandElement;
                byte[] imageD = StringUtil.decodeImage(v);
                byte[] imageBytes = getImage(imageD, icbe.getWidth(), icbe.getHeight());
                cell = new RtfCell(Image.getInstance(imageBytes));
            }
        } catch (Exception e) {
            cell = new RtfCell(IMAGE_NOT_LOADED);
        }
        specialCell = true;
    }
    if (!specialCell) {
        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 = "";
        }
        Phrase ph;
        if (stringValue.startsWith("<html>")) {
            StringReader reader = new StringReader(stringValue);
            List<Element> elems = new ArrayList<Element>();
            try {
                elems = HTMLWorker.parseToList(reader, new StyleSheet());
                ph = new Phrase();
                for (int i = 0; i < elems.size(); i++) {
                    Element elem = (Element) elems.get(i);
                    ph.add(elem);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ph = new Phrase(stringValue, fnt);
            }
        } else {
            ph = new Phrase(stringValue, fnt);
        }

        try {
            cell = new RtfCell(ph);
        } catch (BadElementException e) {
            e.printStackTrace();
            cell = new RtfCell(stringValue);
        }
    }

    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);

    if (colSpan > 1) {
        cell.setColspan(colSpan);
    }

    if (rowSpan > 1) {
        cell.setRowspan(rowSpan);
    }

    setCellStyle(fnt, style, cell);

    return cell;
}

From source file:ro.nextreports.engine.exporter.RtfExporter.java

License:Apache License

private void setCellStyle(Font fnt, Map<String, Object> style, RtfCell cell) {
    if (style != null) {
        updateFont(fnt, style);/*from   w  w w .j  a v a2s  . c o m*/
        if (style.containsKey(StyleFormatConstants.BACKGROUND_COLOR)) {
            Color val = (Color) style.get(StyleFormatConstants.BACKGROUND_COLOR);
            cell.setBackgroundColor(val);
        }
        if (style.containsKey(StyleFormatConstants.HORIZONTAL_ALIGN_KEY)) {
            if (StyleFormatConstants.HORIZONTAL_ALIGN_LEFT
                    .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) {
                cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            }
            if (StyleFormatConstants.HORIZONTAL_ALIGN_RIGHT
                    .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) {
                cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
            }
            if (StyleFormatConstants.HORIZONTAL_ALIGN_CENTER
                    .equals(style.get(StyleFormatConstants.HORIZONTAL_ALIGN_KEY))) {
                cell.setHorizontalAlignment(Element.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);

        Float val = Float.valueOf(1);
        RtfBorderGroup bg = new RtfBorderGroup();
        if (style.containsKey(StyleFormatConstants.BORDER_LEFT)) {
            val = (Float) style.get(StyleFormatConstants.BORDER_LEFT);
            Color color = (Color) style.get(StyleFormatConstants.BORDER_LEFT_COLOR);
            bg.addBorder(Rectangle.LEFT, RtfBorder.BORDER_SINGLE, val, color);
        }
        if (style.containsKey(StyleFormatConstants.BORDER_RIGHT)) {
            val = (Float) style.get(StyleFormatConstants.BORDER_RIGHT);
            Color color = (Color) style.get(StyleFormatConstants.BORDER_RIGHT_COLOR);
            bg.addBorder(Rectangle.RIGHT, RtfBorder.BORDER_SINGLE, val, color);
        }
        if (style.containsKey(StyleFormatConstants.BORDER_TOP)) {
            val = (Float) style.get(StyleFormatConstants.BORDER_TOP);
            Color color = (Color) style.get(StyleFormatConstants.BORDER_TOP_COLOR);
            bg.addBorder(Rectangle.TOP, RtfBorder.BORDER_SINGLE, val, color);
        }
        if (style.containsKey(StyleFormatConstants.BORDER_BOTTOM)) {
            val = (Float) style.get(StyleFormatConstants.BORDER_BOTTOM);
            Color color = (Color) style.get(StyleFormatConstants.BORDER_BOTTOM_COLOR);
            bg.addBorder(Rectangle.BOTTOM, RtfBorder.BORDER_SINGLE, val, color);
        }
        cell.setBorders(bg);

        // cell.setNoWrap(true);
        // if (bandElement != null) {
        // if (bandElement.isWrapText()) {
        // cell.setNoWrap(false);
        // }
        // }
    }
}