Example usage for com.lowagie.text.pdf PdfPCell setBackgroundColor

List of usage examples for com.lowagie.text.pdf PdfPCell setBackgroundColor

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfPCell setBackgroundColor.

Prototype


public void setBackgroundColor(Color backgroundColor) 

Source Link

Document

Sets the backgroundcolor of the rectangle.

Usage

From source file:ilarkesto.integration.itext.Cell.java

License:Open Source License

@Override
public Element getITextElement() {
    PdfPCell cell = new PdfPCell();

    cell.setBorderColorTop(getBorderTopColor());
    cell.setBorderColorBottom(getBorderBottomColor());
    cell.setBorderColorLeft(getBorderLeftColor());
    cell.setBorderColorRight(getBorderRightColor());
    cell.setBorderWidthTop(APdfBuilder.mmToPoints(getBorderTopWidth()));
    cell.setBorderWidthBottom(APdfBuilder.mmToPoints(getBorderBottomWidth()));
    cell.setBorderWidthLeft(APdfBuilder.mmToPoints(getBorderLeftWidth()));
    cell.setBorderWidthRight(APdfBuilder.mmToPoints(getBorderRightWidth()));
    cell.setUseBorderPadding(false);/*  w  w w .  j a  v  a 2s.c o  m*/

    cell.setPadding(0);
    cell.setPaddingTop(APdfBuilder.mmToPoints(getPaddingTop()));
    cell.setPaddingBottom(APdfBuilder.mmToPoints(getPaddingBottom()));
    cell.setPaddingLeft(APdfBuilder.mmToPoints(getPaddingLeft()));
    cell.setPaddingRight(APdfBuilder.mmToPoints(getPaddingRight()));

    cell.setBackgroundColor(getBackgroundColor());
    cell.setExtraParagraphSpace(0);
    cell.setIndent(0);

    cell.setColspan(getColspan());
    for (ItextElement element : elements)
        cell.addElement(element.getITextElement());
    return cell;
}

From source file:it.eng.spagobi.engines.qbe.crosstable.exporter.CrosstabPDFExporter.java

License:Mozilla Public License

/**
 * Build the matrix for the content of the crosstab
 * @param data/*from w ww  .  jav a 2  s. c  om*/
 * @throws JSONException
 */
private void buildDataMatrix(JSONArray data) throws JSONException {
    logger.debug("IN: building the crosstab content");
    PdfPCell cell;
    for (int i = 0; i < data.length(); i++) {
        JSONArray array = (JSONArray) data.get(i);
        List<PdfPCell> dataRow = new ArrayList<PdfPCell>();
        for (int j = 0; j < array.length(); j++) {
            String text = (String) array.get(j);
            //Check if a cell is a sum
            if (text.length() > 5 && text.substring(0, 5).equals("[sum]")) {
                text = text.substring(5);
                cell = new PdfPCell(new Phrase(getFormattedString(text, i, j), cellFont));
                cell.setBackgroundColor(sumBackgroundColor);
            } else {
                cell = new PdfPCell(new Phrase(getFormattedString(text, i, j), cellFont));
            }

            cell.setBorderColor(cellsBorderColor);
            dataRow.add(cell);
        }
        dataMatrix.add(dataRow);
    }
    logger.debug("OUT: built the crosstab content");
}

From source file:it.eng.spagobi.engines.qbe.crosstable.exporter.CrosstabPDFExporter.java

License:Mozilla Public License

/**
 * Builds the row headers. This method performs a depth first visit
 * of the row headers tree// www . j a v  a  2s. c  o m
 * @param siblings: a level (L) of headers
 * @return the cells from level L to the leafs
 * @throws JSONException
 * @throws BadElementException
 */
private List<PdfPCell> buildRowsHeaders(JSONArray siblings) throws JSONException, BadElementException {
    JSONArray childs;
    List<PdfPCell> rowNodes = new ArrayList<PdfPCell>();

    //For every node of the level..
    for (int i = 0; i < siblings.length(); i++) {
        JSONObject aNode = (JSONObject) siblings.get(i);
        String text = (String) aNode.opt(Node.CROSSTAB_NODE_JSON_DESCRIPTION);
        if (text == null) {
            // in case of calculated fields
            text = (String) aNode.get(CrossTab.CROSSTAB_NODE_JSON_KEY);
        }

        int descendants = aNode.getInt(CrosstabExporterUtility.CROSSTAB_JSON_DESCENDANTS_NUMBER);

        PdfPCell cell = new PdfPCell(new Phrase(text, cellFont));
        cell.setBackgroundColor(headersBackgroundColor);
        cell.setBorderColor(cellsBorderColor);

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

        //1) add the node name
        rowNodes.add(cell);

        //2) add the child node names
        childs = aNode.optJSONArray(CrossTab.CROSSTAB_NODE_JSON_CHILDS);
        if (childs != null && childs.length() > 0) {
            rowNodes.addAll(buildRowsHeaders(childs));
        } else {
            rowNodes.addAll(dataMatrix.remove(0));
        }
    }
    return rowNodes;
}

From source file:it.eng.spagobi.engines.qbe.crosstable.exporter.CrosstabPDFExporter.java

License:Mozilla Public License

/**
 * Builds cells for the column headers/*ww w  . j  a v a2s.  com*/
 * @param siblings the top level 
 * @return
 * @throws JSONException
 * @throws BadElementException
 */
private List<PdfPCell> buildColumnsHeader(JSONArray siblings, JSONArray rowHeadersDescription,
        int dataColumnNumber) throws JSONException, BadElementException {

    List<PdfPCell> cells = new ArrayList<PdfPCell>();

    List<JSONObject> columnNodes = getAllNodes(siblings);

    for (int i = 0; i < columnNodes.size(); i++) {
        //adds the row headers
        if (rowHeadersDescription != null && i == columnNodes.size() - dataColumnNumber) {
            for (int y = 0; y < rowHeadersDescription.length(); y++) {
                String text = rowHeadersDescription.getString(y);
                PdfPCell cell = new PdfPCell(new Phrase(text, cellFont));
                cell.setBorderColor(cellsBorderColor);
                cell.setBackgroundColor(headersBackgroundColor);
                cells.add(cell);
            }

        }
        JSONObject aNode = (JSONObject) columnNodes.get(i);
        String text = (String) aNode.get(Node.CROSSTAB_NODE_JSON_DESCRIPTION);
        int descendants = aNode.getInt(CrosstabExporterUtility.CROSSTAB_JSON_DESCENDANTS_NUMBER);

        PdfPCell cell = new PdfPCell(new Phrase(text, cellFont));
        cell.setBorderColor(cellsBorderColor);
        cell.setBackgroundColor(headersBackgroundColor);

        if (descendants > 1) {
            cell.setColspan(descendants);
        }
        cells.add(cell);
    }

    return cells;
}

From source file:it.eng.spagobi.engines.worksheet.exporter.DataSourceTablePDFExporter.java

License:Mozilla Public License

/**
 * Builds the header of the table..// w  ww. jav  a2 s.c  o m
 * It creates also the object table..
 * @param dataStore 
 * @return the table object
 * @throws BadElementException
 */
public PdfPTable buildTableHeader(IDataStore dataStore) throws BadElementException {
    logger.debug("IN: building the headers of the table");
    IMetaData dataStoreMetaData = dataStore.getMetaData();
    int colunum = dataStoreMetaData.getFieldCount();
    List<String> columnsName = new ArrayList<String>();

    //reads the names of the visible table columns
    for (int j = 0; j < colunum; j++) {
        String fieldName = dataStoreMetaData.getFieldAlias(j);
        IFieldMetaData fieldMetaData = dataStoreMetaData.getFieldMeta(j);
        //           String format = (String) fieldMetaData.getProperty("format");
        String alias = (String) fieldMetaData.getAlias();

        if (alias != null && !alias.equals("")) {
            columnsName.add(alias);
        } else {
            columnsName.add(fieldName);
        }
    }

    PdfPTable table = new PdfPTable(colunum);

    //For each column builds a cell
    PdfPCell d = table.getDefaultCell();

    if (colunum < 4) {
        table.setWidthPercentage(colunum * 25);
    } else {
        table.setWidthPercentage(100);
    }

    for (int j = 0; j < colunum; j++) {
        PdfPCell cell = new PdfPCell(new Phrase(columnsName.get(j)));
        //cell.setHeader(true);
        cell.setBorderColor(cellsBorderColor);
        cell.setBackgroundColor(headerbackgroundColor);
        table.addCell(cell);
    }

    table.setHeaderRows(1);

    logger.debug("Out: built the headers of the table");
    return table;
}

From source file:it.eng.spagobi.engines.worksheet.exporter.DataSourceTablePDFExporter.java

License:Mozilla Public License

/**
 * Build the content of the table/*w ww.  j a v  a 2 s .c  o m*/
 * @param dataStore
 * @param table the table with the headers
 * @throws BadElementException
 */
public void buildTableContent(IDataStore dataStore, PdfPTable table) throws BadElementException {
    logger.debug("IN: building the conetent of the table");
    boolean oddRows = true;
    PdfPCell cell;

    Iterator it = dataStore.iterator();

    IMetaData d = dataStore.getMetaData();

    while (it.hasNext()) {//for each record
        IRecord record = (IRecord) it.next();
        List fields = record.getFields();
        int length = fields.size();
        //build the row
        for (int fieldIndex = 0; fieldIndex < length; fieldIndex++) {
            IField f = (IField) fields.get(fieldIndex);
            IFieldMetaData fieldMetaData = d.getFieldMeta(fieldIndex);
            String decimalPrecision = (String) fieldMetaData.getProperty(IFieldMetaData.DECIMALPRECISION);
            if (f == null || f.getValue() == null) {
                cell = new PdfPCell(new Phrase(""));
            } else {
                Class c = d.getFieldType(fieldIndex);
                cell = new PdfPCell(new Phrase(formatPDFCell(c, f, decimalPrecision)));
                if (oddRows) {
                    cell.setBackgroundColor(oddrowsBackgroundColor);
                } else {
                    cell.setBackgroundColor(evenrowsBackgroundColor);
                }
            }
            cell.setBorderColor(cellsBorderColor);
            table.addCell(cell);

        }
        oddRows = !oddRows;
    }
    logger.debug("Out: built the conetent of the table");
}

From source file:jdbreport.model.io.pdf.itext2.PdfWriter.java

License:Apache License

private PdfPCell writeCell(ReportModel model, jdbreport.model.Cell srcCell, int row, int col)
        throws BadElementException, IOException, SaveReportException {

    CellStyle style = model.getStyles(srcCell.getStyleId());

    java.awt.Rectangle rect = model.getCellRect(row, col, true, true);

    float h = Math.round((float) Units.PT.setYPixels((int) rect.getHeight()));
    float w = Math.round((float) Units.PT.setXPixels((int) rect.getWidth()));

    PdfPCell pdfCell = null;

    if (srcCell.getPicture() != null) {
        java.awt.Image awtImage = srcCell.getPicture().getImage();
        com.lowagie.text.Image image = awtImageToImage(awtImage, srcCell, w, h);
        pdfCell = new PdfPCell(image);
    } else {/* ww  w  .  j ava2s  .  c o m*/

        String text = null;

        if (srcCell.getValue() instanceof CellValue<?>) {

            StringWriter strWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(strWriter);

            if (!((CellValue<?>) srcCell.getValue()).write(printWriter, model, row, col, this,
                    ReportBook.PDF)) {
                java.awt.Image awtImage = ((CellValue<?>) srcCell.getValue()).getAsImage(model, row, col);
                if (awtImage != null) {
                    com.lowagie.text.Image image = awtImageToImage(awtImage, srcCell, w, h);
                    pdfCell = new PdfPCell(image);
                }
            } else {
                text = strWriter.getBuffer().toString();
            }

        } else {
            if (jdbreport.model.Cell.TEXT_HTML.equals(srcCell.getContentType())) {
                pdfCell = new PdfPCell();
                writeHTMLText(model.getStyles(srcCell.getStyleId()), srcCell, pdfCell);
            } else {
                text = model.getCellText(srcCell);
            }
        }

        if (pdfCell == null) {
            pdfCell = new PdfPCell();
        }

        if (text != null && text.length() > 0) {
            com.lowagie.text.Font font;
            if (fonts.containsKey(style.getId())) {
                font = fonts.get(style.getId());
            } else {
                font = getFontMapper().styleToPdf(style);
                fonts.put(style.getId(), font);
            }
            Paragraph p;
            if (font != null) {
                p = new Paragraph(text, font);
            } else {
                p = new Paragraph(text);
            }
            pdfCell.setPhrase(p);
            pdfCell.setPadding(1);
            pdfCell.setLeading(0f, 1.1f);
        } else {
            pdfCell.setPadding(0);
        }
    }

    pdfCell.setFixedHeight(h);
    pdfCell.setBackgroundColor(style.getBackground());
    pdfCell.setHorizontalAlignment(toPdfHAlignment(style.getHorizontalAlignment()));
    pdfCell.setVerticalAlignment(toPdfVAlignment(style.getVerticalAlignment()));

    if (style.getAngle() != 0) {
        pdfCell.setRotation(roundAngle(style.getAngle()));
    }

    assignBorders(style, pdfCell);
    pdfCell.setNoWrap(!style.isWrapLine());
    if (srcCell.getColSpan() > 0) {
        pdfCell.setColspan(srcCell.getColSpan() + 1);
    }
    if (srcCell.getRowSpan() > 0) {
        pdfCell.setRowspan(srcCell.getRowSpan() + 1);
    }

    return pdfCell;
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static PdfPCell setCeldaPDFCarnetOpciones(String texto, int tipo, int tamanio, int estilo,
        int alineacion, int borde, int padding, int colspan, Color color) {
    PdfPCell celda = new PdfPCell(new Paragraph(texto, new Font(tipo, tamanio, estilo, Color.WHITE)));
    celda.setHorizontalAlignment(alineacion);
    celda.setVerticalAlignment(Element.ALIGN_TOP);
    celda.setBorderColor(Color.black);
    celda.setBackgroundColor(color);
    celda.setColspan(colspan);/*from   w w  w. j  a  v a 2  s .  com*/
    celda.setBorderWidth(borde);
    celda.setPadding(0);
    celda.setPaddingBottom(padding);
    return celda;
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static PdfPCell setCeldaPDF(String texto, int tipo, int tamanio, int estilo, int alineacion, int borde,
        Color color) {//  w w w.ja  va2 s  .co  m
    PdfPCell celda = new PdfPCell(new Paragraph(texto, new Font(tipo, tamanio, estilo)));
    celda.setHorizontalAlignment(alineacion);
    celda.setVerticalAlignment(Element.ALIGN_TOP);
    celda.setBorderColor(Color.LIGHT_GRAY);
    celda.setBorderWidth(borde);
    celda.setBackgroundColor(color);
    return celda;
}

From source file:jm.web.Addons.java

License:GNU General Public License

public static PdfPCell setCeldaPDF(String texto, int tipo, int tamanio, int estilo, int alineacion, int borde,
        Color color, int padding, int colspan) {
    PdfPCell celda = new PdfPCell(new Paragraph(texto, new Font(tipo, tamanio, estilo)));
    celda.setHorizontalAlignment(alineacion);
    celda.setPadding(padding);/*from w w w  . j  a v  a2s .  c o  m*/
    celda.setColspan(colspan);
    celda.setVerticalAlignment(Element.ALIGN_TOP);
    celda.setBorderColor(Color.LIGHT_GRAY);
    celda.setBorderWidth(borde);
    celda.setBackgroundColor(color);
    return celda;
}