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

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

Introduction

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

Prototype

public PdfPCell(PdfPCell cell) 

Source Link

Document

Constructs a deep copy of a PdfPCell.

Usage

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();
    }//  w w w. j a va  2 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   ww w . j av  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 exportRow(DataTable table, PdfPTable pdfTable, int rowIndex) {
    table.setRowIndex(rowIndex);/*from w w w. j  a  v a  2s .  co m*/

    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  ww  .j  a v a2  s .  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  w w  w.  ja v  a2s  . com*/
        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 addColumnValue(PdfPTable pdfTable, UIComponent component, Font font, String columnType) {
    String value = component == null ? "" : exportValue(FacesContext.getCurrentInstance(), component);
    PdfPCell cell = new PdfPCell(new Paragraph(value, font));

    if (facetBackground != null) {
        cell.setBackgroundColor(facetBackground);
    }//from w w  w  .  j a  va  2  s.c om
    if (columnType.equalsIgnoreCase("header")) {
        cell = addFacetAlignments(component, cell);
    } else {
        cell = addColumnAlignments(component, cell);
    }
    pdfTable.addCell(cell);
}

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

protected void addColumnValue(PdfPTable pdfTable, List<UIComponent> components, Font font, String columnType) {
    StringBuilder builder = new StringBuilder();

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

            if (value != null) {
                builder.append(value);//from   w  w  w  .  jav  a  2s . com
            }
        }
    }
    PdfPCell cell = new PdfPCell(new Paragraph(builder.toString(), font));
    for (UIComponent component : components) {
        cell = addColumnAlignments(component, cell);
    }
    if (columnType.equalsIgnoreCase("header")) {
        for (UIComponent component : components) {
            cell = addFacetAlignments(component, cell);
        }
    }
    pdfTable.addCell(cell);
}

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

License:Open Source License

private static boolean parseContent(WikiPDFContext context, Wiki wiki, String content, Document document,
        PdfPCell cell, Connection db, ArrayList<Integer> wikiListTodo, ArrayList<Integer> wikiListDone,
        float cellWidth) throws Exception {

    LOG.debug("PARSING CONTENT: " + content);

    // Parse the wiki page
    int lastIndent = 0;
    boolean preTag = false;
    boolean pre = false;
    boolean code = false;
    boolean header = true;

    try {//www .  j a  va 2s  . co  m

        BufferedReader in = new BufferedReader(new StringReader(content));
        String line = null;

        ArrayList unorderedParents = null;
        List thisList = null;
        Paragraph codeParagraph = null;

        while ((line = in.readLine()) != null) {
            // Tables
            if (line.startsWith("|")) {
                // @todo Close all ordered lists, unordered lists, and paragraphs

                // Parse the table
                line = parseTable(context, wiki, line, document, db, wikiListTodo, wikiListDone, in);

                if (line == null) {
                    continue;
                }
            }

            // Forms
            if (line.startsWith("[{form")) {
                // @todo close any lists or paragraphs

                // parseForm operates over all the lines that make up the form,
                // it will have to look forward so it returns an unparsed line
                parseForm(context, db, in, line, document, cell);
                continue;
            }

            // Handle code blocks
            // @todo chunk the content similar to WikiToHTMLUtils otherwise inaccurate
            if (line.startsWith("<pre>") || line.startsWith("<code>")) {
                if (!code && !pre) {
                    if (line.startsWith("<pre>")) {
                        preTag = true;
                        pre = true;
                    } else if (line.startsWith("<code>")) {
                        code = true;
                    }
                    codeParagraph = new Paragraph("", codeFont);
                    codeParagraph.setSpacingBefore(10);

                    if (pre && line.length() > ("<pre>").length()) {
                        int endOfLine = line.length();
                        if (line.endsWith("</pre>")) {
                            endOfLine = line.indexOf("</pre>");
                        }
                        // This line has some extra content that needs to be added
                        codeParagraph.add(new Chunk(
                                line.substring(line.indexOf("<pre>") + 5, endOfLine) + Chunk.NEWLINE));
                    }
                    if (code && line.length() > ("<code>").length()) {
                        int endOfLine = line.length();
                        if (line.endsWith("</code>")) {
                            endOfLine = line.indexOf("</code>");
                        }
                        // This line has some extra content that needs to be added
                        codeParagraph.add(new Chunk(
                                line.substring(line.indexOf("<code>") + 6, endOfLine) + Chunk.NEWLINE));
                    }
                    // See if this is a single line block
                    if (preTag && line.endsWith("</pre>")) {
                        // This is a single line block, so finish processing it
                    } else if (code && line.endsWith("</code>")) {
                        // This is a single line block, so finish processing it
                    } else {
                        // There are more lines to process, so do that
                        continue;
                    }
                }
            }
            if (line.startsWith("</code>") || line.endsWith("</code>")) {
                if (code) {
                    code = false;
                    if (line.indexOf("</code>") > 0 && !line.startsWith("<code>")) {
                        // This line has some extra content that needs to be added
                        codeParagraph
                                .add(new Chunk(line.substring(0, line.indexOf("</code>")) + Chunk.NEWLINE));
                    }
                    // Draw the final content
                    PdfPTable codeTable = new PdfPTable(1);
                    codeTable.setWidthPercentage(100);
                    codeTable.setSpacingBefore(10);
                    PdfPCell codeCell = new PdfPCell(codeParagraph);
                    codeCell.setPadding(20);
                    codeCell.setBorderColor(new Color(100, 100, 100));
                    codeCell.setBackgroundColor(new Color(200, 200, 200));
                    //            codeCell.setNoWrap(true);
                    codeTable.addCell(codeCell);
                    LOG.debug("document.add(codeTable)");
                    document.add(codeTable);
                    continue;
                }
            }
            if (line.startsWith("</pre>") || line.endsWith("</pre>")) {
                if (pre) {
                    preTag = false;
                    pre = false;
                    if (line.indexOf("</pre>") > 0 && !line.startsWith("<pre>")) {
                        // This line has some extra content that needs to be added
                        codeParagraph.add(new Chunk(line.substring(0, line.indexOf("</pre>")) + Chunk.NEWLINE));
                    }
                    // Draw the final content
                    PdfPTable codeTable = new PdfPTable(1);
                    codeTable.setWidthPercentage(100);
                    codeTable.setSpacingBefore(10);
                    PdfPCell codeCell = new PdfPCell(codeParagraph);
                    codeCell.setPadding(20);
                    codeCell.setBorderColor(new Color(100, 100, 100));
                    codeCell.setBackgroundColor(new Color(200, 200, 200));
                    //            codeCell.setNoWrap(true);
                    codeTable.addCell(codeCell);
                    LOG.debug("document.add(codeTable)");
                    document.add(codeTable);
                    continue;
                }
            }
            if (code || preTag) {
                // Append the chunk
                codeParagraph.add(new Chunk(line + Chunk.NEWLINE));
                continue;
            }

            // Section
            if (line.startsWith("=") && line.endsWith("=")) {
                // @todo close any open lists or paragraphs

                int hCount = parseHCount(line, "=");
                if (hCount > 6) {
                    hCount = 6;
                }
                String section = line.substring(line.indexOf("=") + hCount, line.lastIndexOf("=") - hCount + 1);
                header = true;
                context.foundHeader(hCount);
                String headerAnchor = null;

                // Store the h2's with anchors for table of contents or index
                if (hCount == 2) {
                    headerAnchor = StringUtils.toHtmlValue(section).replace(" ", "_");
                    context.getHeaderAnchors().put(headerAnchor, section);
                }
                if (hCount == 3) {
                    Paragraph title = new Paragraph(section.trim(), section2Font);
                    title.setSpacingBefore(10);
                    if (cell != null) {
                        LOG.debug("phrase.add(title)");
                        cell.addElement(title);
                    } else {
                        LOG.debug("document.add(title)");
                        document.add(title);
                    }
                } else if (hCount > 3) {
                    Paragraph title = new Paragraph(section.trim(), section3Font);
                    title.setSpacingBefore(10);
                    if (cell != null) {
                        LOG.debug("phrase.add(title)");
                        cell.addElement(title);
                    } else {
                        LOG.debug("document.add(title)");
                        document.add(title);
                    }
                } else {
                    Paragraph title = new Paragraph(section.trim(), sectionFont);
                    title.setSpacingBefore(10);
                    if (cell != null) {
                        LOG.debug("phrase.add(title)");
                        cell.addElement(title);
                    } else {
                        LOG.debug("document.add(title)");
                        document.add(title);
                    }
                }
                continue;
            }
            if (header) {
                header = false;
                if (line.trim().equals("")) {
                    // remove the extra space a user may leave after a header
                    continue;
                }
            }

            // Determine if this is a bulleted list
            if (line.startsWith("*") || line.startsWith("#")) {
                // Initialize the list array
                if (unorderedParents == null) {
                    unorderedParents = new ArrayList();
                    //            if (phrase != null) {
                    //              LOG.debug("phrase.add(new Paragraph(Chunk.NEWLINE))");
                    //              phrase.add(new Paragraph(Chunk.NEWLINE));
                    //            } else {
                    //              LOG.debug("document.add(new Paragraph(Chunk.NEWLINE))");
                    //              document.add(new Paragraph(Chunk.NEWLINE));
                    //            }
                }
                // Get the indent level
                boolean ol = line.startsWith("#");
                int hCount = WikiPDFUtils.parseHCount(line, ol ? "#" : "*");
                // Determine a shift in the tree
                if (lastIndent == 0) {
                    if (ol) {
                        thisList = new List(ol, 20);
                    } else {
                        thisList = new List(ol, 10);
                        thisList.setListSymbol(
                                new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 12)));
                    }
                    thisList.setIndentationLeft(36);
                    thisList.setIndentationRight(36);
                    unorderedParents.add(thisList);
                } else {
                    if (hCount > lastIndent) {
                        if (ol) {
                            thisList = new List(ol, 20);
                        } else {
                            thisList = new List(ol, 10);
                            thisList.setListSymbol(
                                    new Chunk("\u2022", FontFactory.getFont(FontFactory.HELVETICA, 12)));
                        }
                        thisList.setIndentationLeft(36);
                        thisList.setIndentationRight(36);
                        ((List) unorderedParents.get(unorderedParents.size() - 1)).add(thisList);
                        unorderedParents.add(thisList);
                    } else if (hCount < lastIndent) {
                        unorderedParents.remove(unorderedParents.size() - 1);
                        thisList = (List) unorderedParents.get(unorderedParents.size() - 1);
                    }
                }
                lastIndent = hCount;
                // Append the item...
                Paragraph thisItem = new Paragraph();
                parseLine(context, line.substring(hCount).trim(), thisItem, db, wikiListTodo, cellWidth, cell);
                thisList.add(new ListItem(thisItem));
                continue;
            }
            // List is finished, so append it to the document before working on
            // other paragraphs
            if (unorderedParents != null) {
                if (cell != null) {
                    LOG.debug("phrase.add((List) unorderedParents.get(0))");
                    cell.addElement((List) unorderedParents.get(0));
                } else {
                    LOG.debug("document.add((List) unorderedParents.get(0))");
                    document.add((List) unorderedParents.get(0));
                }
                unorderedParents = null;
                thisList = null;
                lastIndent = 0;
            }

            // Otherwise a simple paragraph
            Paragraph paragraph = new Paragraph();
            parseLine(context, line, paragraph, db, wikiListTodo, cellWidth, cell);
            if (cell != null) {
                LOG.debug("phrase.add(paragraph)");
                if (cell.getHorizontalAlignment() == Element.ALIGN_CENTER) {
                    paragraph.setAlignment(Element.ALIGN_CENTER);
                }
                paragraph.setSpacingBefore(5);
                cell.addElement(paragraph);
            } else {
                LOG.debug("document.add(paragraph)");
                paragraph.setSpacingBefore(5);
                document.add(paragraph);
            }
        }

        // Cleanup now that the lines are finished
        if (pre || code) {
            PdfPTable codeTable = new PdfPTable(1);
            codeTable.setWidthPercentage(100);
            codeTable.setSpacingBefore(10);
            PdfPCell codeCell = new PdfPCell(codeParagraph);
            codeCell.setPadding(20);
            codeCell.setBorderColor(new Color(100, 100, 100));
            codeCell.setBackgroundColor(new Color(200, 200, 200));
            //        codeCell.setNoWrap(true);
            codeTable.addCell(codeCell);
            LOG.debug("document.add(codeTable)");
            document.add(codeTable);
        }
        if (unorderedParents != null) {
            if (cell != null) {
                LOG.debug("phrase.add((List) unorderedParents.get(0))");
                cell.addElement((List) unorderedParents.get(0));
            } else {
                LOG.debug("document.add((List) unorderedParents.get(0))");
                document.add((List) unorderedParents.get(0));
            }
        }
        in.close();
    } catch (Exception e) {
        LOG.error("parseContent", e);
    }
    return true;
}

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 w  w  w.j a va  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;
}

From source file:com.crm.webapp.util.PDFCustomExporter.java

License:Apache License

protected PdfPTable exportPDFTable(FacesContext context, DataList list, boolean pageOnly, String encoding) {
    if (!("-".equalsIgnoreCase(encoding))) {
        createCustomFonts(encoding);/*from   w w  w .  j a  va  2 s  .com*/
    }

    int first = list.getFirst();
    int rowCount = list.getRowCount();
    int rowsToExport = first + list.getRows();

    PdfPTable pdfTable = new PdfPTable(1);
    if (list.getHeader() != null) {
        String value = exportValue(FacesContext.getCurrentInstance(), list.getHeader());
        PdfPCell cell = new PdfPCell(new Paragraph((value), this.facetFont));
        if (facetBackground != null) {
            cell.setBackgroundColor(facetBackground);
        }

        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfTable.addCell(cell);
        pdfTable.completeRow();
    }

    StringBuilder builder = new StringBuilder();
    String output = null;

    if (pageOnly) {
        output = exportPageOnly(first, list, rowsToExport, builder);
    } else {
        output = exportAll(list, rowCount, builder);
    }

    pdfTable.addCell(new Paragraph(output, cellFont));
    pdfTable.completeRow();

    if (list.getFooter() != null) {
        String value = exportValue(FacesContext.getCurrentInstance(), list.getFooter());
        PdfPCell cell = new PdfPCell(new Paragraph((value), this.facetFont));
        if (facetBackground != null) {
            cell.setBackgroundColor(facetBackground);
        }

        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        pdfTable.addCell(cell);
        pdfTable.completeRow();
    }

    return pdfTable;
}