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

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

Introduction

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

Prototype

public void addElement(Element element) 

Source Link

Document

Adds an iText element to the cell.

Usage

From source file:de.ipbhalle.metfrag.tools.renderer.WritePDFTable.java

License:Open Source License

/**
 * Instantiates a new write pdf table. This is mainly for debugging the gasteiger marsili charges
 * /*w  w w  . j  ava2  s. com*/
 * @param odir the odir
 * @param width the width
 * @param height the height
 * @param chargeResults the charge results
 */
public WritePDFTable(String odir, int width, int height, List<ChargeResult> chargeResults) {

    this.width = width;
    this.height = height;

    try {
        File file = new File(odir);
        document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));

        float[] widths = new float[ncol];
        for (int i = 0; i < ncol; i += 3) {
            widths[i] = 2.5f;
            widths[i + 1] = 0.75f;
            widths[i + 2] = 0.75f;
        }
        table = new PdfPTable(widths);
        document.open();

        boolean drawPartialCharges = true;

        for (ChargeResult result : chargeResults) {

            if (drawPartialCharges) {
                PdfPCell cellBonds = new PdfPCell();
                PdfPCell cellBondsDist = new PdfPCell();
                Phrase phraseBonds = new Phrase();
                Phrase phraseBondsDist = new Phrase();

                com.lowagie.text.Image image = com.lowagie.text.Image
                        .getInstance(writeMOL2PNGFile(result.getOriginalMol()).getAbsolutePath());
                image.setAbsolutePosition(0, 0);
                table.addCell(image);

                String stringAtoms = "";
                String stringAtomsCharge = "";
                for (IAtom atom : result.getOriginalMol().atoms()) {
                    if (!atom.getSymbol().equals("H") && !atom.getSymbol().equals("C")) {
                        stringAtoms += atom.getSymbol() + (Integer.parseInt(atom.getID()) + 1) + "\n";
                        stringAtomsCharge += Math.round(atom.getCharge() * 100.0) / 100.0 + "\n";
                    }
                }

                addProperty(phraseBonds, stringAtoms);
                addProperty(phraseBondsDist, stringAtomsCharge);
                cellBonds.addElement(phraseBonds);
                cellBondsDist.addElement(phraseBondsDist);
                table.addCell(cellBonds);
                table.addCell(cellBondsDist);
                drawPartialCharges = false;
            }

            PdfPCell cellBonds = new PdfPCell();
            PdfPCell cellBondsDist = new PdfPCell();
            Phrase phraseBonds = new Phrase();
            Phrase phraseBondsDist = new Phrase();

            com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(
                    writeMOL2PNGFile(result.getOriginalMol(), result.getMolWithProton()).getAbsolutePath());
            image.setAbsolutePosition(0, 0);
            table.addCell(image);

            String stringPDFBonds = "";
            String stringPDFBondsDist = "";
            String[] lines = result.getChargeString().split("\n");
            for (int i = 0; i < lines.length; i++) {
                boolean carbonHydrogenBond = lines[i].matches("[A-Z]+[0-9]+-H[0-9]+.*");
                if (!carbonHydrogenBond) {
                    String[] linesArr = lines[i].split("\t");
                    stringPDFBondsDist += linesArr[1] + "\n";
                    stringPDFBonds += linesArr[0] + "\n";
                }
            }

            addProperty(phraseBonds, stringPDFBonds);
            addProperty(phraseBondsDist, stringPDFBondsDist);
            cellBonds.addElement(phraseBonds);
            cellBondsDist.addElement(phraseBondsDist);
            table.addCell(cellBonds);
            table.addCell(cellBondsDist);
        }

        document.add(table);
        document.close();

    } catch (Exception exc) {
        exc.printStackTrace();
    }
}

From source file:edu.harvard.mcz.imagecapture.encoder.LabelEncoder.java

License:Open Source License

@SuppressWarnings("hiding")
public static boolean printList(List<UnitTrayLabel> taxa) throws PrintFailedException {
    boolean result = false;
    UnitTrayLabel label = new UnitTrayLabel();
    LabelEncoder encoder = new LabelEncoder(label);
    Image image = encoder.getImage();
    int counter = 0;
    try {/*w  w  w.ja  v a 2 s.  co  m*/
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("labels.pdf"));
        document.setPageSize(PageSize.LETTER);
        document.open();

        PdfPTable table = new PdfPTable(4);
        table.setWidthPercentage(100f);
        //table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
        float[] cellWidths = { 30f, 20f, 30f, 20f };
        table.setWidths(cellWidths);

        UnitTrayLabelLifeCycle uls = new UnitTrayLabelLifeCycle();
        if (taxa == null) {
            taxa = uls.findAll();
        }
        Iterator<UnitTrayLabel> i = taxa.iterator();
        PdfPCell cell = null;
        PdfPCell cell_barcode = null;
        // Create two lists of 12 cells, the first 6 of each representing
        // the left hand column of 6 labels, the second 6 of each 
        // representing the right hand column.  
        // cells holds the text for each label, cells_barcode the barcode.
        ArrayList<PdfPCell> cells = new ArrayList<PdfPCell>(12);
        ArrayList<PdfPCell> cells_barcode = new ArrayList<PdfPCell>(12);
        for (int x = 0; x < 12; x++) {
            cells.add(null);
            cells_barcode.add(null);
        }
        int cellCounter = 0;
        while (i.hasNext()) {
            // Loop through all of the taxa (unit tray labels) found to print 
            label = i.next();
            for (int toPrint = 0; toPrint < label.getNumberToPrint(); toPrint++) {
                // For each taxon, loop through the number of requested copies 
                // Generate a text and a barcode cell for each, and add to array for page
                log.debug("Label " + toPrint + " of " + label.getNumberToPrint());
                cell = new PdfPCell();
                cell.setBorderColor(Color.LIGHT_GRAY);
                cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
                cell.disableBorderSide(PdfPCell.RIGHT);
                cell.setPaddingLeft(3);

                String higherNames = "";
                if (label.getTribe().trim().length() > 0) {
                    higherNames = label.getFamily() + ": " + label.getSubfamily() + ": " + label.getTribe();
                } else {
                    higherNames = label.getFamily() + ": " + label.getSubfamily();
                }
                Paragraph higher = new Paragraph();
                higher.setFont(new Font(Font.TIMES_ROMAN, 11, Font.NORMAL));
                higher.add(new Chunk(higherNames));
                cell.addElement(higher);

                Paragraph name = new Paragraph();
                Chunk genus = new Chunk(label.getGenus().trim() + " ");
                genus.setFont(new Font(Font.TIMES_ROMAN, 11, Font.ITALIC));
                Chunk species = new Chunk(label.getSpecificEpithet().trim());
                Chunk normal = null; // normal font prefix to preceed specific epithet (nr. <i>epithet</i>)
                if (label.getSpecificEpithet().contains(".") || label.getSpecificEpithet().contains("[")) {
                    if (label.getSpecificEpithet().startsWith("nr. ")) {
                        normal = new Chunk("nr. ");
                        normal.setFont(new Font(Font.TIMES_ROMAN, 11, Font.NORMAL));
                        species = new Chunk(label.getSpecificEpithet().trim().substring(4));
                        species.setFont(new Font(Font.TIMES_ROMAN, 11, Font.ITALIC));
                    } else {
                        species.setFont(new Font(Font.TIMES_ROMAN, 11, Font.NORMAL));
                    }
                } else {
                    species.setFont(new Font(Font.TIMES_ROMAN, 11, Font.ITALIC));
                }
                String s = "";
                if (label.getSubspecificEpithet().trim().length() > 0) {
                    s = " ";
                } else {
                    s = "";
                }
                Chunk subspecies = new Chunk(s + label.getSubspecificEpithet().trim());
                if (label.getSubspecificEpithet().contains(".")
                        || label.getSubspecificEpithet().contains("[")) {
                    subspecies.setFont(new Font(Font.TIMES_ROMAN, 11, Font.NORMAL));
                } else {
                    subspecies.setFont(new Font(Font.TIMES_ROMAN, 11, Font.ITALIC));
                }
                if (label.getInfraspecificRank().trim().length() > 0) {
                    s = " ";
                } else {
                    s = "";
                }
                Chunk infraRank = new Chunk(s + label.getInfraspecificRank().trim());
                infraRank.setFont(new Font(Font.TIMES_ROMAN, 11, Font.NORMAL));

                if (label.getInfraspecificEpithet().trim().length() > 0) {
                    s = " ";
                } else {
                    s = "";
                }
                Chunk infra = new Chunk(s + label.getInfraspecificEpithet().trim());
                infra.setFont(new Font(Font.TIMES_ROMAN, 11, Font.ITALIC));
                if (label.getUnNamedForm().trim().length() > 0) {
                    s = " ";
                } else {
                    s = "";
                }
                Chunk unNamed = new Chunk(s + label.getUnNamedForm().trim());
                unNamed.setFont(new Font(Font.TIMES_ROMAN, 11, Font.NORMAL));

                name.add(genus);
                if (normal != null) {
                    name.add(normal);
                }
                name.add(species);
                name.add(subspecies);
                name.add(infraRank);
                name.add(infra);
                name.add(unNamed);
                cell.addElement(name);

                Paragraph authorship = new Paragraph();
                authorship.setFont(new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
                if (label.getAuthorship() != null && label.getAuthorship().length() > 0) {
                    Chunk c_authorship = new Chunk(label.getAuthorship());
                    authorship.add(c_authorship);
                }
                cell.addElement(authorship);
                //cell.addElement(new Paragraph(" "));
                if (label.getDrawerNumber() != null && label.getDrawerNumber().length() > 0) {
                    Paragraph drawerNumber = new Paragraph();
                    drawerNumber.setFont(new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
                    Chunk c_drawerNumber = new Chunk(label.getDrawerNumber());
                    drawerNumber.add(c_drawerNumber);
                    cell.addElement(drawerNumber);
                } else {
                    if (label.getCollection() != null && label.getCollection().length() > 0) {
                        Paragraph collection = new Paragraph();
                        collection.setFont(new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
                        Chunk c_collection = new Chunk(label.getCollection());
                        collection.add(c_collection);
                        cell.addElement(collection);
                    }
                }

                cell_barcode = new PdfPCell();
                cell_barcode.setBorderColor(Color.LIGHT_GRAY);
                cell_barcode.disableBorderSide(PdfPCell.LEFT);
                cell_barcode.setVerticalAlignment(PdfPCell.ALIGN_TOP);

                encoder = new LabelEncoder(label);
                image = encoder.getImage();
                image.setAlignment(Image.ALIGN_TOP);
                cell_barcode.addElement(image);

                cells.add(cellCounter, cell);
                cells_barcode.add(cellCounter, cell_barcode);

                cellCounter++;
                // If we have hit a full set of 12 labels, add them to the document
                // in two columns, filling left column first, then right
                if (cellCounter == 12) {
                    // add a page of 12 cells in columns of two.
                    for (int x = 0; x < 6; x++) {
                        if (cells.get(x) == null) {
                            PdfPCell c = new PdfPCell();
                            c.setBorder(0);
                            table.addCell(c);
                            table.addCell(c);
                        } else {
                            table.addCell(cells.get(x));
                            table.addCell(cells_barcode.get(x));
                        }
                        if (cells.get(x + 6) == null) {
                            PdfPCell c = new PdfPCell();
                            c.setBorder(0);
                            table.addCell(c);
                            table.addCell(c);
                        } else {
                            table.addCell(cells.get(x + 6));
                            table.addCell(cells_barcode.get(x + 6));
                        }
                    }
                    // Reset to begin next page
                    cellCounter = 0;
                    document.add(table);
                    table = new PdfPTable(4);
                    table.setWidthPercentage(100f);
                    table.setWidths(cellWidths);
                    for (int x = 0; x < 12; x++) {
                        cells.set(x, null);
                        cells_barcode.set(x, null);
                    }
                }
            } // end loop through toPrint (for a taxon)
            counter++;
        } // end while results has next (for all taxa requested)
          // get any remaining cells in pairs
        for (int x = 0; x < 6; x++) {
            if (cells.get(x) == null) {
                PdfPCell c = new PdfPCell();
                c.setBorder(0);
                table.addCell(c);
                table.addCell(c);
            } else {
                table.addCell(cells.get(x));
                table.addCell(cells_barcode.get(x));
            }
            if (cells.get(x + 6) == null) {
                PdfPCell c = new PdfPCell();
                c.setBorder(0);
                table.addCell(c);
                table.addCell(c);
            } else {
                table.addCell(cells.get(x + 6));
                table.addCell(cells_barcode.get(x + 6));
            }
        }
        // add any remaining cells
        document.add(table);
        try {
            document.close();
        } catch (Exception e) {
            throw new PrintFailedException("No labels to print." + e.getMessage());
        }
        // Check to see if there was content in the document.
        if (counter == 0) {
            result = false;
        } else {
            // Printed to pdf ok.
            result = true;
            // Increment number printed.
            i = taxa.iterator();
            while (i.hasNext()) {
                label = i.next();
                for (int toPrint = 0; toPrint < label.getNumberToPrint(); toPrint++) {
                    label.setPrinted(label.getPrinted() + 1);
                }
                label.setNumberToPrint(0);
                try {
                    uls.attachDirty(label);
                } catch (SaveFailedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new PrintFailedException("File not found.");
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        throw new PrintFailedException("Error buiding PDF document.");
    } catch (OutOfMemoryError e) {
        System.out.println("Out of memory error. " + e.getMessage());
        System.out.println("Failed.  Too many labels.");
        throw new PrintFailedException("Ran out of memory, too many labels at once.");
    }
    return result;
}

From source file:fr.opensagres.xdocreport.itext.extension.ExtendedParagraph.java

License:Open Source License

protected PdfPTable createWrapperTable(PdfPCell wrapperCell, boolean addParagraph) {
    PdfPTable wrapperTable = null;/* w  ww .  j  ava2  s.co  m*/
    // wrap this paragraph into a table if necessary
    if (wrapperCell != null) {
        // background color or borders were set
        if (addParagraph) {
            wrapperCell.addElement(this);
        }
        wrapperTable = createTable(wrapperCell);
        if (getSpacingBefore() > 0.0f || getSpacingAfter() > 0.0f) {
            // top or bottom margin was set, promote it to enclosing table
            wrapperTable.setSpacingBefore(getSpacingBefore());
            setSpacingBefore(0.0f);
            wrapperTable.setSpacingAfter(getSpacingAfter());
            setSpacingAfter(0.0f);
        }
        if (getIndentationLeft() > 0.0f || getIndentationRight() > 0.0f) {
            // left or right margin was set, have to wrap the cell again
            PdfPCell outerCell = createCell();
            outerCell.setPaddingLeft(getIndentationLeft());
            setIndentationLeft(0.0f);
            outerCell.setPaddingRight(getIndentationRight());
            setIndentationRight(0.0f);
            outerCell.addElement(wrapperTable);
            wrapperTable = createTable(outerCell);
        }
    }
    return wrapperTable;
}

From source file:fr.opensagres.xdocreport.itext.extension.ExtendedSection.java

License:Open Source License

public static Paragraph constructTitle(IParagraphFactory factory, Paragraph ancestorTitle, ArrayList numbers,
        int numberDepth, int numberStyle, PdfPCell cell) {
    if (ancestorTitle != null && cell != null) {
        Paragraph newTitle = factory.createParagraph();
        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100f);/* w w  w.jav a2  s .c o m*/
        cell.addElement(ancestorTitle);
        table.addCell(cell);
        newTitle.add(table);
        return newTitle;
    }
    return ancestorTitle;
}

From source file:fr.opensagres.xdocreport.itext.extension.NestedTable.java

License:Open Source License

public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    try {//w  w w . j a  va 2 s  .  c o m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("NestedTable.pdf"));
        document.open();

        PdfPTable table = new PdfPTable(1);

        PdfPTable nestedTable = new PdfPTable(2);

        PdfPCell cell = new PdfPCell(nestedTable);

        PdfPCell cell1 = new PdfPCell();
        cell1.addElement(new Chunk("cell1"));
        nestedTable.addCell(cell1);

        PdfPCell cell2 = new PdfPCell();
        cell2.addElement(new Chunk("cell2"));
        nestedTable.addCell(cell2);

        table.addCell(cell);

        document.add(table);
    } catch (Exception de) {
        de.printStackTrace();
    }
    document.close();
}

From source file:fr.opensagres.xdocreport.itext.extension.NestedTable2.java

License:Open Source License

public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    try {//from w ww  . j  av  a2 s.  c o m
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("NestedTable2.pdf"));
        document.open();

        PdfPTable table = new PdfPTable(1);

        PdfPTable nestedTable = new PdfPTable(2);
        PdfPCell cell1 = new PdfPCell();
        cell1.addElement(new Chunk("cell1"));
        nestedTable.addCell(cell1);

        PdfPCell cell2 = new PdfPCell();
        cell2.addElement(new Chunk("cell2"));
        nestedTable.addCell(cell2);

        Paragraph paragraph = new Paragraph();
        paragraph.add(new Chunk("eeeeeeeeee"));
        paragraph.add(nestedTable);

        PdfPCell cell = new PdfPCell(paragraph);
        //cell.addElement( nestedTable );
        //cell.addElement( new Chunk("cell3") );
        //cell.
        table.addCell(cell);

        document.add(table);
    } catch (Exception de) {
        de.printStackTrace();
    }
    document.close();
}

From source file:fr.opensagres.xdocreport.itext.extension.SimpleTable.java

License:Open Source License

public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    try {/*from  www  . j  a  v a 2s  .co m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("SimpleTable.pdf"));

        document.open();

        PdfPTable table = new PdfPTable(2);
        PdfPCell cell;
        Paragraph p = new Paragraph("Text Text Text ");
        table.addCell("cell");
        table.addCell(p);
        table.addCell("cell");
        cell = new PdfPCell(p);

        p = new Paragraph("Text Text Text ");

        //Chunk c = new Chunk( "zzzzzzzzzz" );   

        //cell.setPadding( 0f );
        //cell.getColumn().setAdjustFirstLine( false );
        // make a room for borders
        //cell.setUseBorderPadding( false );

        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setBackgroundColor(Color.red);

        cell.addElement(p);

        cell.setUseAscender(true);
        //cell.addElement(c );
        // cell.setPaddingTop( 0f );
        // cell.setPaddingLeft( 20f );

        table.addCell(cell);
        //table.addCell( "cell" );
        document.add(table);
    } catch (Exception de) {
        de.printStackTrace();
    }
    document.close();
}

From source file:fr.opensagres.xdocreport.itext.extension.TableWithAlignment.java

License:Open Source License

public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    try {/*from  w  w w.  ja  v a2s .  co  m*/
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TableWithAlignment.pdf"));

        document.open();

        PdfPTable table = new PdfPTable(2);
        table.getDefaultCell().setUseAscender(true);
        table.getDefaultCell().setUseDescender(true);

        table.setWidths(new float[] { 5, 10 });
        table.setWidthPercentage(10);

        PdfPCell cell1 = new PdfPCell();
        cell1.addElement(new Chunk("cell1"));
        table.addCell(cell1);

        PdfPCell cell2 = new PdfPCell();
        cell2.addElement(new Chunk("cell2"));
        table.addCell(cell2);

        table.setSpacingBefore(-100);

        document.add(table);
    } catch (Exception de) {
        de.printStackTrace();
    }
    document.close();
}

From source file:fr.opensagres.xdocreport.itext.extension.TableWithVerticalAlignment.java

License:Open Source License

public static void main(String[] args) {
    Document document = new Document(PageSize.A4.rotate(), 10, 10, 10, 10);
    try {//from www.  j  av  a 2s.  c om
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("TableWithVerticalAlignment.pdf"));

        document.open();

        PdfPTable table = new PdfPTable(1);
        // PdfPCell cell1 = new PdfPCell();
        // cell1.setVerticalAlignment( Element.ALIGN_BOTTOM );
        // //cell1.setMinimumHeight( 100f );
        // cell1.addElement( new Chunk( "cell1" ) );
        // table.addCell( cell1 );

        PdfPCell cell2 = new PdfPCell();
        Paragraph p = new Paragraph();
        p.add(new Chunk("cellp&"));

        cell2.addElement(p);
        cell2.setVerticalAlignment(Element.ALIGN_BOTTOM);
        cell2.setMinimumHeight(38f);
        table.addCell(cell2);

        document.add(table);
    } catch (Exception de) {
        de.printStackTrace();
    }
    document.close();
}

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 .  ja v  a  2 s. c  om*/

    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;
}