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

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

Introduction

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

Prototype

public void setHorizontalAlignment(int horizontalAlignment) 

Source Link

Document

Sets the horizontal alignment for the cell.

Usage

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected void tableFacet(FacesContext context, PdfPTable pdfTable, SubTable table, int columnCount,
        String facetType) {//from   ww w.  j ava  2  s  .c o m
    Map<String, UIComponent> map = table.getFacets();
    UIComponent component = map.get(facetType);
    if (component != null) {
        String headerValue = null;
        if (component instanceof HtmlCommandButton) {
            headerValue = exportValue(context, component);
        } else if (component instanceof HtmlCommandLink) {
            headerValue = exportValue(context, component);
        } else if (component instanceof UIPanel || component instanceof OutputPanel) {
            String header = "";
            for (UIComponent child : component.getChildren()) {
                headerValue = exportValue(context, child);
                header = header + headerValue;
            }
            PdfPCell cell = new PdfPCell(new Paragraph((header), this.facetFont));
            if (facetBackground != null) {
                cell.setBackgroundColor(facetBackground);
            }
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            //addColumnAlignments(component,cell);
            cell.setColspan(columnCount);
            pdfTable.addCell(cell);
            pdfTable.completeRow();
            return;
        } else {
            headerValue = exportFacetValue(context, component);
        }
        PdfPCell cell = new PdfPCell(new Paragraph((headerValue), this.facetFont));
        if (facetBackground != null) {
            cell.setBackgroundColor(facetBackground);
        }
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        // addColumnAlignments(component,cell);
        cell.setColspan(columnCount);
        pdfTable.addCell(cell);
        pdfTable.completeRow();

    }
}

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected void tableColumnGroup(PdfPTable pdfTable, DataTable table, String facetType) {
    ColumnGroup cg = table.getColumnGroup(facetType);
    List<UIComponent> headerComponentList = null;
    if (cg != null) {
        headerComponentList = cg.getChildren();
    }/*from  w w  w .  j a  v a2 s. c o m*/
    if (headerComponentList != null)
        for (UIComponent component : headerComponentList) {
            if (component instanceof Row) {
                Row row = (Row) component;
                for (UIComponent rowComponent : row.getChildren()) {
                    UIColumn column = (UIColumn) rowComponent;
                    String value = null;
                    if (facetType.equalsIgnoreCase("header")) {
                        value = column.getHeaderText();
                    } else
                        value = column.getFooterText();
                    int rowSpan = column.getRowspan();
                    int colSpan = column.getColspan();
                    PdfPCell cell = new PdfPCell(new Paragraph(value, this.facetFont));
                    if (facetBackground != null) {
                        cell.setBackgroundColor(facetBackground);
                    }
                    if (rowSpan > 1) {
                        cell.setVerticalAlignment(Element.ALIGN_CENTER);
                        cell.setRowspan(rowSpan);

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

                    }
                    // addColumnAlignments(component,cell);
                    if (facetType.equalsIgnoreCase("header")) {
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    }
                    pdfTable.addCell(cell);

                }
            }

        }
    pdfTable.completeRow();

}

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected void tableColumnGroup(PdfPTable pdfTable, SubTable table, String facetType) {
    ColumnGroup cg = table.getColumnGroup(facetType);
    List<UIComponent> headerComponentList = null;
    if (cg != null) {
        headerComponentList = cg.getChildren();
    }//from   w  ww.  jav  a 2 s.  co m
    if (headerComponentList != null)
        for (UIComponent component : headerComponentList) {
            if (component instanceof Row) {
                Row row = (Row) component;
                for (UIComponent rowComponent : row.getChildren()) {
                    UIColumn column = (UIColumn) rowComponent;
                    String value = null;
                    if (facetType.equalsIgnoreCase("header")) {
                        value = column.getHeaderText();
                    } else
                        value = column.getFooterText();
                    int rowSpan = column.getRowspan();
                    int colSpan = column.getColspan();
                    PdfPCell cell = new PdfPCell(new Paragraph(value, this.facetFont));
                    if (facetBackground != null) {
                        cell.setBackgroundColor(facetBackground);
                    }
                    if (rowSpan > 1) {
                        cell.setVerticalAlignment(Element.ALIGN_CENTER);
                        cell.setRowspan(rowSpan);

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

                    }
                    // addColumnAlignments(component,cell);
                    if (facetType.equalsIgnoreCase("header")) {
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    }
                    pdfTable.addCell(cell);

                }
            }

        }
    pdfTable.completeRow();

}

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected void exportRow(DataTable table, PdfPTable pdfTable, int rowIndex) {
    table.setRowIndex(rowIndex);//from ww w.  ja v  a2 s .c om

    if (!table.isRowAvailable()) {
        return;
    }

    exportCells(table, pdfTable);
    SummaryRow sr = table.getSummaryRow();

    if (sr != null && sr.isInView()) {
        for (UIComponent summaryComponent : sr.getChildren()) {
            UIColumn column = (UIColumn) summaryComponent;
            StringBuilder builder = new StringBuilder();

            for (UIComponent component : column.getChildren()) {
                if (component.isRendered()) {
                    String value = exportValue(FacesContext.getCurrentInstance(), component);

                    if (value != null) {
                        builder.append(value);
                    }
                }
            }
            int rowSpan = column.getRowspan();
            int colSpan = column.getColspan();
            PdfPCell cell = new PdfPCell(new Paragraph(builder.toString(), this.facetFont));
            if (facetBackground != null) {
                cell.setBackgroundColor(facetBackground);
            }
            if (rowSpan > 1) {
                cell.setVerticalAlignment(Element.ALIGN_CENTER);
                cell.setRowspan(rowSpan);

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

            }
            pdfTable.addCell(cell);
        }
    }
}

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected void addColumnFacets(DataTable table, PdfPTable pdfTable, ColumnType columnType) {
    for (UIColumn col : table.getColumns()) {

        if (col instanceof DynamicColumn) {
            ((DynamicColumn) col).applyStatelessModel();
        }/*w w  w  .j  av  a  2s .c o  m*/
        PdfPCell cell = null;
        if (col.isRendered() && col.isExportable()) {
            if (col.getHeaderText() != null && columnType.name().equalsIgnoreCase("header")) {
                cell = new PdfPCell(new Paragraph(col.getHeaderText(), this.facetFont));
                if (facetBackground != null) {
                    cell.setBackgroundColor(facetBackground);
                }
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                pdfTable.addCell(cell);
            } else if (col.getFooterText() != null && columnType.name().equalsIgnoreCase("footer")) {
                cell = new PdfPCell(new Paragraph(col.getFooterText(), this.facetFont));
                if (facetBackground != null) {
                    cell.setBackgroundColor(facetBackground);
                }
                pdfTable.addCell(cell);
            } else {
                addColumnValue(pdfTable, col.getFacet(columnType.facet()), this.facetFont, columnType.name());
            }
        }
    }
}

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected void addColumnFacets(SubTable table, PdfPTable pdfTable, ColumnType columnType) {
    for (UIColumn col : table.getColumns()) {

        if (col instanceof DynamicColumn) {
            ((DynamicColumn) col).applyStatelessModel();
        }/*from  ww w . j a v  a  2 s . c om*/
        PdfPCell cell = null;
        if (col.isRendered() && col.isExportable()) {
            if (col.getHeaderText() != null && columnType.name().equalsIgnoreCase("header")) {
                cell = new PdfPCell(new Paragraph(col.getHeaderText(), this.facetFont));
                if (facetBackground != null) {
                    cell.setBackgroundColor(facetBackground);
                }
                cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                pdfTable.addCell(cell);
            } else if (col.getFooterText() != null && columnType.name().equalsIgnoreCase("footer")) {
                cell = new PdfPCell(new Paragraph(col.getFooterText(), this.facetFont));
                if (facetBackground != null) {
                    cell.setBackgroundColor(facetBackground);
                }
                pdfTable.addCell(cell);
            } else {

                addColumnValue(pdfTable, col.getFacet(columnType.facet()), this.facetFont, columnType.name());
            }
        }
    }
}

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected PdfPCell addColumnAlignments(UIComponent component, PdfPCell cell) {
    if (component instanceof HtmlOutputText) {
        HtmlOutputText output = (HtmlOutputText) component;
        if (output.getStyle() != null && output.getStyle().contains("left")) {
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        }/*w  w w  .j a va  2s  . co m*/
        if (output.getStyle() != null && output.getStyle().contains("right")) {
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        }
        if (output.getStyle() != null && output.getStyle().contains("center")) {
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        }
    }
    return cell;
}

From source file:com.bytecode.customexporter.PDFCustomExporter.java

protected PdfPCell addFacetAlignments(UIComponent component, PdfPCell cell) {
    if (component instanceof HtmlOutputText) {
        HtmlOutputText output = (HtmlOutputText) component;
        if (output.getStyle() != null && output.getStyle().contains("left")) {
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        } else if (output.getStyle() != null && output.getStyle().contains("right")) {
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        } else {/*from  w  ww . ja v a2s.c  o m*/
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        }
    }
    return cell;
}

From source file:com.concursive.connect.web.modules.wiki.utils.WikiPDFUtils.java

License:Open Source License

private static String parseTable(WikiPDFContext context, Wiki wiki, String line, Document document,
        Connection db, ArrayList<Integer> wikiListTodo, ArrayList<Integer> wikiListDone, BufferedReader in)
        throws Exception {
    if (line == null) {
        return null;
    }/*  ww w . j  ava2  s. co m*/
    PdfPTable pdfTable = null;
    int columnCount = 0;
    int rowCount = 0;

    // Keep track of the table's custom styles
    HashMap<Integer, String> cStyle = new HashMap<Integer, String>();

    while (line != null && (line.startsWith("|") || line.startsWith("!"))) {

        // Build a complete line
        String lineToParse = line;
        while (!line.endsWith("|")) {
            line = in.readLine();
            if (line == null) {
                // there is an error in the line to process
                return null;
            }
            if (line.startsWith("!")) {
                lineToParse += CRLF + line.substring(1);
            }
        }
        line = lineToParse;

        // Determine if the row can output
        boolean canOutput = true;

        ++rowCount;

        String cellType = null;
        Scanner sc = null;
        if (line.startsWith("||") && line.endsWith("||")) {
            cellType = "th";
            sc = new Scanner(line).useDelimiter("[|][|]");
            //        sc = new Scanner(line.substring(2, line.length() - 2)).useDelimiter("[|][|]");
        } else if (line.startsWith("|")) {
            cellType = "td";
            sc = new Scanner(line.substring(1, line.length() - 1)).useDelimiter("\\|(?=[^\\]]*(?:\\[|$))");
        }

        if (sc != null) {

            if (rowCount == 1) {
                // Count the columns, get the specified widths too...
                while (sc.hasNext()) {
                    ++columnCount;
                    sc.next();
                }
                // Reset the scanner now that the columns have been counted
                if (line.startsWith("||") && line.endsWith("||")) {
                    sc = new Scanner(line).useDelimiter("[|][|]");
                } else if (line.startsWith("|")) {
                    sc = new Scanner(line.substring(1, line.length() - 1))
                            .useDelimiter("\\|(?=[^\\]]*(?:\\[|$))");
                }

                // Start the table
                pdfTable = new PdfPTable(columnCount);
                //pdfTable.setWidthPercentage(100);
                pdfTable.setHorizontalAlignment(Element.ALIGN_LEFT);
                pdfTable.setSpacingBefore(10);
                pdfTable.setWidthPercentage(100);
                pdfTable.setKeepTogether(true);
            }

            // Determine the column span
            int colSpan = 1;
            // Determine the cell being output
            int cellCount = 0;

            while (sc.hasNext()) {
                String cellData = sc.next();
                if (cellData.length() == 0) {
                    ++colSpan;
                    continue;
                }

                // Track the cell count being output
                ++cellCount;

                if (rowCount == 1) {
                    // Parse and validate the style input
                    LOG.debug("Checking style value: " + cellData);
                    if (cellData.startsWith("{") && cellData.endsWith("}")) {
                        String[] style = cellData.substring(1, cellData.length() - 1).split(":");
                        String attribute = style[0].trim();
                        String value = style[1].trim();
                        // Determine the width of each column and store it
                        if ("width".equals(attribute)) {
                            // Validate the width style
                            if (StringUtils.hasAllowedOnly("0123456789%.", value)) {
                                cStyle.put(cellCount, attribute + ": " + value + ";");
                            }
                        } else {
                            LOG.debug("Unsupported style: " + cellData);
                        }
                        canOutput = false;
                    }
                }

                // Output the header
                if (canOutput) {

                    PdfPCell cell = new PdfPCell();
                    cell.setPadding(10);
                    cell.setBorderColor(new Color(100, 100, 100));
                    if ("th".equals(cellType)) {
                        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
                    }
                    if (colSpan > 1) {
                        cell.setColspan(colSpan);
                    }

                    // Output the data
                    if (" ".equals(cellData) || "".equals(cellData)) {
                        // Put a blank space in blank cells for output consistency
                        cell.addElement(new Chunk(" "));
                        LOG.debug("   OUTPUTTING A BLANK");
                    } else {
                        // Output the cell as a complete wiki
                        float cellWidth = (100.0f / columnCount);
                        parseContent(context, wiki, cellData, document, cell, db, wikiListTodo, wikiListDone,
                                cellWidth);
                        LOG.debug("   OUTPUTTING CONTENT");
                    }
                    pdfTable.addCell(cell);
                }
            }
        }
        // read another line to see if it's part of the table
        line = in.readLine();
    }
    if (pdfTable != null) {
        LOG.debug("document.add(pdfTable)");
        document.add(pdfTable);
        //          document.add(Chunk.NEWLINE);
    }
    return line;
}

From source file:com.concursive.connect.web.modules.wiki.utils.WikiPDFUtils.java

License:Open Source License

protected static String parseForm(WikiPDFContext context, Connection db, BufferedReader in, String line,
        Document document, PdfPCell cell) throws Exception {
    if (line == null) {
        return line;
    }/*from  ww  w .  j a  v  a  2 s  . c o m*/
    CustomForm form = WikiToHTMLUtils.retrieveForm(in, line);
    LOG.debug("parseForm");
    for (CustomFormGroup group : form) {
        LOG.debug(" group...");
        // Start the table

        PdfPTable pdfTable = new PdfPTable(2);
        pdfTable.setHorizontalAlignment(Element.ALIGN_LEFT);
        pdfTable.setSpacingBefore(10);
        //      pdfTable.setWidthPercentage(100);
        pdfTable.setKeepTogether(true);

        if (group.getDisplay() && StringUtils.hasText(group.getName())) {
            // output the 1st row with a colspan of 2
            if (StringUtils.hasText(group.getName())) {
                Paragraph groupParagraph = new Paragraph(group.getName());
                PdfPCell groupCell = new PdfPCell(groupParagraph);
                groupCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                groupCell.setColspan(2);
                groupCell.setPadding(20);
                groupCell.setBorderColor(new Color(100, 100, 100));
                groupCell.setBackgroundColor(new Color(200, 200, 200));
                groupCell.setNoWrap(true);
                pdfTable.addCell(groupCell);
            }
        }
        for (CustomFormField field : group) {
            LOG.debug("  field...");
            if (field.hasValue()) {
                // output the row (2 columns: label, value)
                Paragraph fieldLabelParagraph = new Paragraph(field.getLabel());
                PdfPCell fieldLabelCell = new PdfPCell(fieldLabelParagraph);
                fieldLabelCell.setPadding(20);
                fieldLabelCell.setBorderColor(new Color(100, 100, 100));
                //          fieldLabelCell.setNoWrap(true);
                pdfTable.addCell(fieldLabelCell);

                Paragraph fieldValueParagraph = new Paragraph(getFieldValue(context, field));
                PdfPCell fieldValueCell = new PdfPCell(fieldValueParagraph);
                fieldValueCell.setPadding(20);
                fieldValueCell.setBorderColor(new Color(100, 100, 100));
                //          fieldValueCell.setNoWrap(true);
                pdfTable.addCell(fieldValueCell);
            }
        }
        LOG.debug("document.add(pdfTable)");
        document.add(pdfTable);

    }
    return null;
}