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

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

Introduction

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

Prototype

public PdfPTable(PdfPTable table) 

Source Link

Document

Constructs a copy of a PdfPTable.

Usage

From source file:org.goodoldai.jeff.report.pdf.PDFDataChunkBuilder.java

License:Open Source License

/**
 * This is a private method that is used to insert three dimensional data
 * content into the PDF document./*from   w  w  w  .  j  a  va2  s  .  c o m*/
 *
 * @param data three dimensional data which needs to be transformed
 * @param document PDF document in which the transformed data will be
 * written
 */
private void inputThreeDimDataContent(ThreeDimData data, Document document) {

    //Create table with three columns and set some formatting options
    PdfPTable table = new PdfPTable(3);
    table.setWidthPercentage(80);
    table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
    table.setSpacingBefore(15.0F);
    table.setSpacingAfter(15.0F);

    //Create the header cells
    PdfPCell cell1 = new PdfPCell(new Paragraph(turnDimensionIntoHeader(data.getDimension1())));
    cell1.setBackgroundColor(Color.lightGray);
    table.addCell(cell1);

    PdfPCell cell2 = new PdfPCell(new Paragraph(turnDimensionIntoHeader(data.getDimension2())));
    cell2.setBackgroundColor(Color.lightGray);
    table.addCell(cell2);

    PdfPCell cell3 = new PdfPCell(new Paragraph(turnDimensionIntoHeader(data.getDimension3())));
    cell3.setBackgroundColor(Color.lightGray);
    table.addCell(cell3);

    //Create and insert data cells
    ArrayList<Triple> values = data.getValues();

    for (int i = 0; i < values.size(); i++) {
        Triple triple = (Triple) (values.get(i));
        table.addCell(new Paragraph(transformValue(triple.getValue1())));
        table.addCell(new Paragraph(transformValue(triple.getValue2())));
        table.addCell(new Paragraph(transformValue(triple.getValue3())));
    }

    try {
        document.add(table);
    } catch (DocumentException ex) {
        throw new ExplanationException(ex.getMessage());
    }

}

From source file:org.gtdfree.addons.PDFExportAddOn.java

License:Open Source License

@Override
public void export(GTDModel model, ActionsCollection collection, OutputStream out,
        ExportAddOn.ExportOrder order, FileFilter ff, boolean compact) throws Exception {

    fontSelector = new FontSelector();
    fontSelectorB = new FontSelector();
    fontSelectorB2 = new FontSelector();
    fontSelectorB4 = new FontSelector();

    baseFont = fontModel.getBaseFont();/*w ww  . j ava 2s.  c o  m*/

    for (BaseFont bf : fontModel.getFonts()) {
        fontSelector.addFont(new Font(bf, baseFontSize));
        fontSelectorB.addFont(new Font(bf, baseFontSize, Font.BOLD));
        fontSelectorB2.addFont(new Font(bf, baseFontSize + 2, Font.BOLD));
        fontSelectorB4.addFont(new Font(bf, baseFontSize + 4, Font.BOLD));
    }

    boolean emptyH2 = false;
    boolean emptyH3 = false;

    PdfPTable actionTable = null;

    Document doc = new Document();

    if (sizeSet) {
        doc.setPageSize(pageSize);
    }
    if (marginSet) {
        doc.setMargins(marginLeft, marginRight, marginTop, marginBottom);
    }

    //System.out.println("PDF size "+doc.getPageSize().toString());
    //System.out.println("PDF m "+marginLeft+" "+marginRight+" "+marginTop+" "+marginBottom);

    @SuppressWarnings("unused")
    PdfWriter pw = PdfWriter.getInstance(doc, out);

    doc.addCreationDate();
    doc.addTitle("GTD-Free PDF");
    doc.addSubject("GTD-Free data exported as PDF");

    HeaderFooter footer = new HeaderFooter(newParagraph(), true);
    footer.setAlignment(HeaderFooter.ALIGN_CENTER);
    footer.setBorder(HeaderFooter.TOP);
    doc.setFooter(footer);

    doc.open();

    Phrase ch = newTitle("GTD-Free Data");
    Paragraph p = new Paragraph(ch);
    p.setAlignment(Paragraph.ALIGN_CENTER);

    PdfPTable t = new PdfPTable(1);
    t.setWidthPercentage(100f);
    PdfPCell c = newCell(p);
    c.setBorder(Table.BOTTOM);
    c.setBorderWidth(2.5f);
    c.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
    c.setPadding(5f);
    t.addCell(c);
    doc.add(t);

    Iterator<Object> it = collection.iterator(order);

    while (it.hasNext()) {
        Object o = it.next();

        if (o == ActionsCollection.ACTIONS_WITHOUT_PROJECT) {

            if (order == ExportAddOn.ExportOrder.FoldersProjectsActions) {

                doc.add(newSubSection(ActionsCollection.ACTIONS_WITHOUT_PROJECT));

                emptyH2 = false;
                emptyH3 = true;
            } else {

                doc.add(newSection(ActionsCollection.ACTIONS_WITHOUT_PROJECT));

                emptyH2 = true;
                emptyH3 = false;
            }

            continue;
        }

        if (o instanceof Folder) {

            Folder f = (Folder) o;

            if (actionTable != null) {
                doc.add(actionTable);
                actionTable = null;
            }

            if (f.isProject()) {

                if (order == ExportAddOn.ExportOrder.ProjectsActions
                        || order == ExportAddOn.ExportOrder.ProjectsFoldersActions) {

                    if (emptyH2 || emptyH3) {
                        p = newParagraph(NONE_DOT);
                        doc.add(p);
                    }

                    doc.add(newSection(f.getName()));

                    if (compact) {
                        if (f.getDescription() != null && f.getDescription().length() > 0) {
                            p = newParagraph(f.getDescription());
                            p.setIndentationLeft(10f);
                            p.setIndentationRight(10f);
                            doc.add(p);
                        }
                    } else {
                        t = new PdfPTable(2);
                        t.setKeepTogether(true);
                        t.setSpacingBefore(5f);
                        t.setWidthPercentage(66f);
                        t.setWidths(new float[] { 0.33f, 0.66f });

                        c = newCell("ID");
                        t.addCell(c);
                        c = newCell(newStrongParagraph(String.valueOf(f.getId())));
                        t.addCell(c);
                        c = newCell("Type");
                        t.addCell(c);
                        c = newCell(newStrongParagraph("Project"));
                        t.addCell(c);
                        c = newCell("Open");
                        t.addCell(c);
                        c = newCell(newStrongParagraph(String.valueOf(f.getOpenCount())));
                        t.addCell(c);
                        c = newCell("All");
                        t.addCell(c);
                        c = newCell(newStrongParagraph(String.valueOf(f.size())));
                        t.addCell(c);
                        c = newCell("Description");
                        t.addCell(c);
                        c = newDescriptionCell(f.getDescription());
                        t.addCell(c);

                        doc.add(t);
                    }

                    emptyH2 = true;
                    emptyH3 = false;

                } else {

                    if (emptyH3) {
                        p = newParagraph(NONE_DOT);
                        doc.add(p);
                    }

                    doc.add(newSubSection("Project:" + " " + f.getName()));

                    emptyH2 = false;
                    emptyH3 = true;
                }

                continue;

            }
            if (order == ExportAddOn.ExportOrder.FoldersActions
                    || order == ExportAddOn.ExportOrder.FoldersProjectsActions) {

                if (emptyH2 || emptyH3) {
                    p = newParagraph(NONE_DOT);
                    doc.add(p);
                }

                doc.add(newSection(f.getName()));

                if (compact) {
                    if (f.getDescription() != null && f.getDescription().length() > 0) {
                        p = newParagraph(f.getDescription());
                        p.setIndentationLeft(10f);
                        p.setIndentationRight(10f);
                        doc.add(p);
                    }
                } else {

                    t = new PdfPTable(2);
                    t.setKeepTogether(true);
                    t.setSpacingBefore(5f);
                    t.setWidthPercentage(66f);
                    t.setWidths(new float[] { 0.33f, 0.66f });

                    c = newCell("ID");
                    t.addCell(c);
                    c = newCell(newStrongParagraph(String.valueOf(f.getId())));
                    t.addCell(c);

                    String type = "";
                    if (f.isAction()) {
                        type = "Action list";
                    } else if (f.isInBucket()) {
                        type = "In-Bucket";
                    } else if (f.isQueue()) {
                        type = "Next action queue";
                    } else if (f.isReference()) {
                        type = "Reference list";
                    } else if (f.isSomeday()) {
                        type = "Someday/Maybe list";
                    } else if (f.isBuildIn()) {
                        type = "Default list";
                    }

                    c = newCell("Type");
                    t.addCell(c);
                    c = newCell(newStrongParagraph(type));
                    t.addCell(c);
                    c = newCell("Open");
                    t.addCell(c);
                    c = newCell(newStrongParagraph(String.valueOf(f.getOpenCount())));
                    t.addCell(c);
                    c = newCell("All");
                    t.addCell(c);
                    c = newCell(newStrongParagraph(String.valueOf(f.size())));
                    t.addCell(c);
                    c = newCell("Description");
                    t.addCell(c);
                    c = newDescriptionCell(f.getDescription());
                    t.addCell(c);

                    doc.add(t);
                }

                emptyH2 = true;
                emptyH3 = false;

            } else {

                if (emptyH3) {
                    p = newParagraph(NONE_DOT);
                    doc.add(p);
                }

                doc.add(newSubSection("List:" + " " + f.getName()));

                emptyH2 = false;
                emptyH3 = true;

            }

            continue;

        }

        if (o instanceof Action) {
            emptyH2 = false;
            emptyH3 = false;

            Action a = (Action) o;

            if (compact) {

                if (actionTable == null) {
                    actionTable = new PdfPTable(5);
                    actionTable.setWidthPercentage(100f);
                    actionTable.setHeaderRows(1);
                    actionTable.setSpacingBefore(5f);
                    c = newHeaderCell("ID");
                    actionTable.addCell(c);
                    c = newHeaderCell("Pri.");
                    actionTable.addCell(c);
                    c = newHeaderCell("Description");
                    actionTable.addCell(c);
                    c = newHeaderCell("Reminder");
                    actionTable.addCell(c);
                    c = newHeaderCell(CHECK_RESOLVED);
                    actionTable.addCell(c);

                    float width = doc.getPageSize().getWidth() - doc.getPageSize().getBorderWidthLeft()
                            - doc.getPageSize().getBorderWidthRight();
                    int i = model.getLastActionID();
                    float step = baseFontSize - 1;
                    int steps = (int) Math.floor(Math.log10(i)) + 1;
                    // ID column
                    float col1 = 8 + steps * step;
                    // Priority column
                    float col2 = 4 + 3 * (baseFontSize + 4);
                    // Reminder column
                    float col4 = 10 + step * 11;
                    // Resolved column
                    float col5 = 8 + baseFontSize;
                    // Description column
                    float col3 = width - col1 - col2 - col4 - col5;
                    actionTable.setWidths(new float[] { col1, col2, col3, col4, col5 });

                }

                addSingleActionRow(a, actionTable);

            } else {
                addSingleActionTable(model, doc, a);
            }
        }

    }

    if (actionTable != null) {
        doc.add(actionTable);
        actionTable = null;
    }
    if (emptyH2 || emptyH3) {

        p = newParagraph(NONE_DOT);
        doc.add(p);
    }

    //w.writeCharacters("Exported: "+ApplicationHelper.toISODateTimeString(new Date()));

    doc.close();
}

From source file:org.gtdfree.addons.PDFExportAddOn.java

License:Open Source License

/**
 * @param model/*from   w  ww. j  a  va 2  s.com*/
 * @param doc
 * @param a
 * @throws DocumentException
 * @throws IOException
 * @throws BadElementException
 */
private void addSingleActionTable(GTDModel model, Document doc, Action a)
        throws DocumentException, IOException, BadElementException {
    Chunk ch;
    PdfPTable t;
    PdfPCell c;
    t = new PdfPTable(3);
    t.setSpacingBefore(5f);
    t.setWidthPercentage(100f);

    Paragraph ph = newParagraph();
    ch = newChunk(ID);
    ph.add(ch);
    ch = newChunk(String.valueOf(a.getId()));
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    c = newCell(ph);
    t.addCell(c);

    ph = newParagraph();
    ch = newChunk(CREATED);
    ph.add(ch);
    ch = newChunk(ApplicationHelper.toISODateTimeString(a.getCreated()));
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    c = newCell(ph);
    t.addCell(c);

    ph = newParagraph();
    if (a.isOpen()) {
        ch = newChunk(OPEN);
        ch.getFont().setStyle(Font.BOLD);
        ch.getFont().setColor(COLOR_OPEN);
        ph.add(ch);
        ch = newOpenChunk();
        ph.add(ch);
    } else if (a.isResolved()) {
        ch = newChunk(RESOLVED);
        ch.getFont().setStyle(Font.BOLD);
        ch.getFont().setColor(COLOR_RESOLVED);
        ph.add(ch);
        ch = newResolvedChunk();
        ph.add(ch);
    } else if (a.isDeleted()) {
        ch = newChunk(DELETED);
        ch.getFont().setStyle(Font.BOLD);
        ch.getFont().setColor(COLOR_DELETED);
        ph.add(ch);
        ch = newDeletedChunk();
        ph.add(ch);
    } else {
        ch = newChunk(STALLED);
        ch.getFont().setStyle(Font.BOLD);
        ch.getFont().setColor(COLOR_DELETED);
        ph.add(ch);
        ch = newStalledChunk();
        ph.add(ch);
    }
    c = newCell(ph);
    c.setHorizontalAlignment(Cell.ALIGN_RIGHT);
    t.addCell(c);

    ph = newParagraph();
    ch = newChunk(PRIORITY);
    ph.add(ch);
    ch = newChunk(a.getPriority() == null ? NONE : a.getPriority().toString());
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    ch = newChunk(" ");
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);

    if (a.getPriority() == null || a.getPriority() == Priority.None) {
        ch = newNoneStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
    } else if (a.getPriority() == Priority.Low) {
        ch = newLowStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
    } else if (a.getPriority() == Priority.Medium) {
        ch = newLowStarChunk();
        ph.add(ch);
        ch = newMediumStarChunk();
        ph.add(ch);
        ch = newNoneStarChunk();
        ph.add(ch);
    } else if (a.getPriority() == Priority.High) {
        ch = newLowStarChunk();
        ph.add(ch);
        ch = newMediumStarChunk();
        ph.add(ch);
        ch = newHighStarChunk();
        ph.add(ch);
    }

    c = newCell(ph);
    t.addCell(c);

    ph = newParagraph();
    ch = newChunk(REMINDER);
    ph.add(ch);
    ch = newChunk(a.getRemind() != null ? ApplicationHelper.toISODateString(a.getRemind()) : NONE);
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    c = newCell(ph);
    t.addCell(c);

    ph = newParagraph();
    ch = newChunk(PROJECT);
    ph.add(ch);
    if (a.getProject() != null) {
        ch = newChunk(model.getProject(a.getProject()).getName());
    } else {
        ch = newChunk(NONE);
    }
    ch.getFont().setStyle(Font.BOLD);
    ph.add(ch);
    c = newCell(ph);
    t.addCell(c);

    c = newDescriptionCell(a.getDescription());
    c.setColspan(3);
    t.addCell(c);

    if (a.getUrl() != null) {

        ch = newChunk(a.getUrl().toString());
        ch.setAnchor(a.getUrl());
        ch.getFont().setColor(Color.BLUE);

        c = newCell(new Paragraph(ch));
        c.setColspan(3);
        t.addCell(c);
    }

    doc.add(t);
}

From source file:org.gtdfree.addons.PDFExportAddOn.java

License:Open Source License

private Element newSection(String s) throws DocumentException, IOException {
    Phrase c = fontSelectorB2.process(s);
    Paragraph p = new Paragraph(c);

    PdfPTable t = new PdfPTable(1);
    t.setSpacingBefore(15f);/*from w w  w  . j av a  2  s . com*/
    t.setSpacingAfter(7f);
    t.setWidthPercentage(100f);

    PdfPCell ce = newCell(p);
    ce.setBorder(PdfPCell.BOTTOM);
    ce.setBorderWidth(1f);
    ce.setPaddingLeft(0);
    ce.setPaddingRight(0);

    t.addCell(ce);

    return t;
}

From source file:org.gtdfree.addons.PDFExportAddOn.java

License:Open Source License

private Element newSubSection(String s) throws DocumentException, IOException {
    Phrase c = fontSelectorB.process(s);
    Paragraph p = new Paragraph(c);

    PdfPTable t = new PdfPTable(1);
    t.setSpacingBefore(7f);/*w  w  w  . ja  v  a2 s.  com*/
    t.setSpacingAfter(5f);
    t.setWidthPercentage(100f);

    PdfPCell ce = newCell(p);
    ce.setBorder(PdfPCell.BOTTOM);
    ce.setBorderWidth(0.75f);
    ce.setPaddingLeft(0);
    ce.setPaddingRight(0);

    t.addCell(ce);

    return t;
}

From source file:org.jboss.as.quickstarts.ejbinwar.ejb.GreeterEJB.java

License:Apache License

public ByteArrayOutputStream generatePDFDocumentBytes(String selectedTariff) throws DocumentException {
    java.util.Set<String> users = getRestUsers(selectedTariff);

    Document doc = new Document();

    ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
    PdfWriter docWriter = null;//w w  w .  j  a v  a  2s  .com

    try {
        docWriter = PdfWriter.getInstance(doc, baosPDF);

        doc.addAuthor(this.getClass().getName());
        doc.addCreationDate();
        doc.addProducer();
        doc.addCreator(this.getClass().getName());
        doc.addTitle(selectedTariff + " clients");
        doc.addKeywords("pdf, itext, Java, ecare, http");

        doc.setPageSize(PageSize.LETTER);

        HeaderFooter footer = new HeaderFooter(new Phrase("E-Care report"), false);

        doc.setFooter(footer);

        doc.open();

        doc.add(new Paragraph(selectedTariff + " clients"));
        doc.add(new Paragraph("\n"));
        doc.add(new Paragraph("\n"));

        PdfPTable table = new PdfPTable(4); // 3 columns.

        PdfPCell cell1 = new PdfPCell(new Paragraph("Name"));
        PdfPCell cell2 = new PdfPCell(new Paragraph("Surname"));
        PdfPCell cell3 = new PdfPCell(new Paragraph("Address"));
        PdfPCell cell4 = new PdfPCell(new Paragraph("Email"));

        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        table.addCell(cell4);

        for (Iterator<String> it = users.iterator(); it.hasNext();) {
            String user = it.next();

            table.addCell(new PdfPCell(new Paragraph(user.split(" ")[0])));
            table.addCell(new PdfPCell(new Paragraph(user.split(" ")[1])));
            table.addCell(new PdfPCell(new Paragraph(user.split(" ")[2])));
            table.addCell(new PdfPCell(new Paragraph(user.split(" ")[3])));
        }

        doc.add(table);

    } catch (DocumentException dex) {
        baosPDF.reset();
        throw dex;
    } finally {
        if (doc != null) {
            doc.close();
        }
        if (docWriter != null) {
            docWriter.close();
        }
    }

    if (baosPDF.size() < 1) {
        throw new DocumentException("document has " + baosPDF.size() + " bytes");
    }
    return baosPDF;

}

From source file:org.jcryptool.visual.euclid.FileExporter.java

License:Open Source License

public void exportToPDF() {
    table = new PdfPTable(5);
    try {//from   w  ww.j a  v a  2s  . c  o m
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        PdfPCell cell = new PdfPCell();
        cell.setColspan(5);
        float[] width = { 1.2f, 3f, 3f, 3f, 3f };
        table.setTotalWidth(width);
        table.addCell("Index");
        table.addCell("Quotient");
        table.addCell("Remainder");
        table.addCell("x");
        table.addCell("y");

        int size = euclid.length;
        String pValue = euclid[2];
        String qValue = euclid[7];
        String yValue = euclid[size - 7];
        String xValue = euclid[size - 6];
        String resultValue = euclid[size - 8];

        for (int i = 0; i < size; i++) {
            table.addCell(new Phrase(euclid[i]));
            System.out.println("3 " + i);
        }

        document.add(new Paragraph("Extended Euclidian"));
        document.add(new Paragraph("gcd(" + pValue + "," + qValue + ")"));
        document.add(new Paragraph("\n"));
        document.add(table);
        document.add(new Paragraph("\n"));

        if (xValue.charAt(0) == '-') {
            xValue = "( " + xValue + " )";
        }
        if (yValue.charAt(0) == '-') {
            yValue = "( " + yValue + " )";
        }
        String tmpValue = " = " + pValue + " * " + yValue + " + " + qValue + " * " + xValue;
        String resultValueString = tmpValue + " = " + resultValue;

        document.add(new Paragraph(
                "The Greatest Common Divisor of " + pValue + " and " + qValue + " is " + resultValue + "."));
        document.add(new Paragraph("gcd(p,q) = p * x + q * y", fontValuesItalic));
        document.add(new Paragraph("gcd(" + pValue + "," + qValue + ")" + resultValueString, fontValuesItalic));
        document.close();

    } catch (FileNotFoundException e) {
        LogUtil.logError(e);
    } catch (DocumentException e) {
        LogUtil.logError(e);
    }
}

From source file:org.jcryptool.visual.xeuclidean.export.FileExporter.java

License:Open Source License

public void exportToPDF() {
    table = new PdfPTable(5);
    try {/*from w w  w . j  a  v  a 2  s .c  o  m*/
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        PdfPCell cell = new PdfPCell();
        cell.setColspan(5);
        float[] width = { 1.2f, 3f, 3f, 3f, 3f };
        table.setTotalWidth(width);
        table.addCell("Index");
        table.addCell("Quotient");
        table.addCell("Remainder");
        table.addCell("X");
        table.addCell("Y");

        String pValue = rTmp.get(0).toString();
        String qValue = rTmp.get(1).toString();
        String yValue = yTmp.lastElement().toString();
        String xValue = xTmp.lastElement().toString();
        String resultValue = rTmp.get(qTmp.size() - 2).toString();
        String resultValueString;

        int size = qTmp.size();
        for (int index = 0; index < size; index++) {
            table.addCell(new Phrase(String.valueOf(index), fontValues));
            if (index == 0 || index == qTmp.size() - 1) {
                table.addCell(new Phrase("", fontValues));
            } else {
                table.addCell(new Phrase(qTmp.get(index).toString(), fontValues));
            }
            table.addCell(new Phrase(rTmp.get(index).toString(), fontValues));
            table.addCell(new Phrase(xTmp.get(index).toString(), fontValues));
            table.addCell(new Phrase(yTmp.get(index).toString(), fontValues));
        }

        document.add(new Paragraph("Extended Euclidian"));
        document.add(new Paragraph("gcd(" + pValue + "," + qValue + ")"));
        document.add(new Paragraph("\n"));
        document.add(table);
        document.add(new Paragraph("\n"));

        if (xTmp.lastElement().compareTo(BigInteger.ZERO) < 0) {
            xValue = "( " + xValue + " )";
        }
        if (yTmp.lastElement().compareTo(BigInteger.ZERO) < 0) {
            yValue = "( " + yValue + " )";
        }
        String tmpValue = " = " + pValue + " * " + yValue + " + " + qValue + " * " + xValue;
        resultValueString = tmpValue + " = " + resultValue;

        document.add(new Paragraph(
                "The Greates Common Divisor of " + pValue + " and " + qValue + " is " + resultValue + "."));
        document.add(new Paragraph("gcd(p,q) = p * x + q * y", fontValuesItalic));
        document.add(new Paragraph("gcd(" + rTmp.get(0) + "," + rTmp.get(1) + ")" + resultValueString,
                fontValuesItalic));
        document.close();

    } catch (FileNotFoundException e) {
        LogUtil.logError(e);
    } catch (DocumentException e) {
        LogUtil.logError(e);
    }
}

From source file:org.jmesa.view.pdfp.PdfPView.java

License:Apache License

@Override
public PdfPTable render() {

    PdfPTable pdfpTable = new PdfPTable(getTable().getRow().getColumns().size());
    pdfpTable.setSpacingBefore(3);/* w w  w.  ja v a 2 s  .c om*/

    Row row = getTable().getRow();

    List<Column> columns = row.getColumns();

    // build table headers
    for (Iterator<Column> iter = columns.iterator(); iter.hasNext();) {
        Column column = iter.next();
        PdfPCell cell = new PdfPCell(new Paragraph(column.getTitle(), getHeaderCellFont()));
        cell.setPadding(3.0f);
        cell.setBackgroundColor(getHeaderBackgroundColor());
        pdfpTable.addCell(cell);
    }

    // build table body
    Collection<?> items = getCoreContext().getPageItems();
    int rowcount = 0;
    for (Object item : items) {
        rowcount++;

        columns = row.getColumns();

        for (Iterator<Column> iter = columns.iterator(); iter.hasNext();) {
            Column column = iter.next();

            String property = column.getProperty();
            Object value = column.getCellEditor().getValue(item, property, rowcount);
            PdfPCell cell = new PdfPCell(
                    new Paragraph(value == null ? "" : String.valueOf(value), getCellFont()));
            cell.setPadding(3.0f);

            if (isRowEven(rowcount)) {
                cell.setBackgroundColor(getEvenCellBackgroundColor());
            } else {
                cell.setBackgroundColor(getOddCellBackgroundColor());
            }

            pdfpTable.addCell(cell);
        }
    }

    return pdfpTable;
}

From source file:org.jsondoc.springmvc.pdf.PdfExportView.java

License:Open Source License

public File getPdfFile(String filename) {
    try {/*from   ww  w. j  av  a 2s  .c  o m*/
        File file = new File(filename + "-v" + jsonDoc.getVersion() + FILE_EXTENSION);
        FileOutputStream fileout = new FileOutputStream(file);
        Document document = new Document();
        PdfWriter.getInstance(document, fileout);

        // Header
        HeaderFooter header = new HeaderFooter(new Phrase("Copyright "
                + Calendar.getInstance().get(Calendar.YEAR) + " Paybay Networks - All rights reserved"), false);
        header.setBorder(Rectangle.NO_BORDER);
        header.setAlignment(Element.ALIGN_LEFT);
        document.setHeader(header);

        // Footer
        HeaderFooter footer = new HeaderFooter(new Phrase("Page "), true);
        footer.setBorder(Rectangle.NO_BORDER);
        footer.setAlignment(Element.ALIGN_CENTER);
        document.setFooter(footer);

        document.open();

        //init documentation
        apiDocs = buildApiDocList();
        apiMethodDocs = buildApiMethodDocList(apiDocs);

        Phrase baseUrl = new Phrase("Base url: " + jsonDoc.getBasePath());
        document.add(baseUrl);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);

        int pos = 1;
        for (ApiMethodDoc apiMethodDoc : apiMethodDocs) {
            Phrase phrase = new Phrase(/*"Description: " + */apiMethodDoc.getDescription());
            document.add(phrase);
            document.add(Chunk.NEWLINE);

            PdfPTable table = new PdfPTable(2);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
            table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            table.setWidthPercentage(100);

            table.setWidths(new int[] { 50, 200 });

            // HEADER CELL START TABLE
            table.addCell(ITextUtils.getHeaderCell("URL"));
            table.addCell(ITextUtils.getHeaderCell("<baseUrl> " + apiMethodDoc.getPath()));
            table.completeRow();

            // FIRST CELL
            table.addCell(ITextUtils.getCell("Http Method", 0));
            table.addCell(ITextUtils.getCell(apiMethodDoc.getVerb().name(), pos));
            pos++;
            table.completeRow();

            // PRODUCES
            if (!apiMethodDoc.getProduces().isEmpty()) {
                table.addCell(ITextUtils.getCell("Produces", 0));
                table.addCell(ITextUtils.getCell(buildApiMethodProduces(apiMethodDoc), pos));
                pos++;
                table.completeRow();
            }

            // CONSUMES
            if (!apiMethodDoc.getConsumes().isEmpty()) {
                table.addCell(ITextUtils.getCell("Consumes", 0));
                table.addCell(ITextUtils.getCell(buildApiMethodConsumes(apiMethodDoc), pos));
                pos++;
                table.completeRow();
            }

            // HEADERS
            if (!apiMethodDoc.getHeaders().isEmpty()) {
                table.addCell(ITextUtils.getCell("Request headers", 0));

                PdfPTable pathParamsTable = new PdfPTable(3);
                pathParamsTable.setWidths(new int[] { 30, 20, 40 });

                pathParamsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                pathParamsTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                for (ApiHeaderDoc apiHeaderDoc : apiMethodDoc.getHeaders()) {
                    PdfPCell boldCell = new PdfPCell();
                    Font fontbold = FontFactory.getFont("Times-Roman", 12, Font.BOLD);
                    boldCell.setPhrase(new Phrase(apiHeaderDoc.getName(), fontbold));
                    boldCell.getPhrase().setFont(new Font(Font.BOLD));
                    boldCell.setBorder(Rectangle.NO_BORDER);
                    pathParamsTable.addCell(boldCell);

                    PdfPCell paramCell = new PdfPCell();

                    StringBuilder builder = new StringBuilder();

                    for (String value : apiHeaderDoc.getAllowedvalues())
                        builder.append(value).append(", ");

                    paramCell.setPhrase(new Phrase("Allowed values: " + builder.toString()));
                    paramCell.setBorder(Rectangle.NO_BORDER);

                    pathParamsTable.addCell(paramCell);

                    paramCell.setPhrase(new Phrase(apiHeaderDoc.getDescription()));

                    pathParamsTable.addCell(paramCell);
                    pathParamsTable.completeRow();
                }

                PdfPCell bluBorderCell = new PdfPCell(pathParamsTable);
                bluBorderCell.setBorder(Rectangle.NO_BORDER);
                bluBorderCell.setBorderWidthRight(1f);
                bluBorderCell.setBorderColorRight(Colors.CELL_BORDER_COLOR);

                table.addCell(ITextUtils.setOddEvenStyle(bluBorderCell, pos));
                pos++;
                table.completeRow();
            }

            // PATH PARAMS
            if (!apiMethodDoc.getPathparameters().isEmpty()) {
                table.addCell(ITextUtils.getCell("Path params", 0));

                PdfPTable pathParamsTable = new PdfPTable(3);
                pathParamsTable.setWidths(new int[] { 30, 15, 40 });

                pathParamsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                pathParamsTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                for (ApiParamDoc apiParamDoc : apiMethodDoc.getPathparameters()) {
                    PdfPCell boldCell = new PdfPCell();
                    Font fontbold = FontFactory.getFont("Times-Roman", 12, Font.BOLD);
                    boldCell.setPhrase(new Phrase(apiParamDoc.getName(), fontbold));
                    boldCell.getPhrase().setFont(new Font(Font.BOLD));
                    boldCell.setBorder(Rectangle.NO_BORDER);
                    pathParamsTable.addCell(boldCell);

                    PdfPCell paramCell = new PdfPCell();
                    paramCell.setPhrase(new Phrase(apiParamDoc.getJsondocType().getOneLineText()));
                    paramCell.setBorder(Rectangle.NO_BORDER);

                    pathParamsTable.addCell(paramCell);

                    paramCell.setPhrase(new Phrase(apiParamDoc.getDescription()));

                    pathParamsTable.addCell(paramCell);
                    pathParamsTable.completeRow();
                }

                PdfPCell bluBorderCell = new PdfPCell(pathParamsTable);
                bluBorderCell.setBorder(Rectangle.NO_BORDER);
                bluBorderCell.setBorderWidthRight(1f);
                bluBorderCell.setBorderColorRight(Colors.CELL_BORDER_COLOR);

                table.addCell(ITextUtils.setOddEvenStyle(bluBorderCell, pos));
                pos++;
                table.completeRow();
            }

            // QUERY PARAMS
            if (!apiMethodDoc.getQueryparameters().isEmpty()) {
                table.addCell(ITextUtils.getCell("Query params", 0));

                PdfPTable queryParamsTable = new PdfPTable(3);
                queryParamsTable.setWidths(new int[] { 30, 15, 40 });

                queryParamsTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                queryParamsTable.getDefaultCell().setBorder(Rectangle.NO_BORDER);

                for (ApiParamDoc apiParamDoc : apiMethodDoc.getQueryparameters()) {
                    PdfPCell boldCell = new PdfPCell();
                    Font fontbold = FontFactory.getFont("Times-Roman", 12, Font.BOLD);
                    boldCell.setPhrase(new Phrase(apiParamDoc.getName(), fontbold));
                    boldCell.getPhrase().setFont(new Font(Font.BOLD));
                    boldCell.setBorder(Rectangle.NO_BORDER);
                    queryParamsTable.addCell(boldCell);

                    PdfPCell paramCell = new PdfPCell();
                    paramCell.setPhrase(new Phrase(apiParamDoc.getJsondocType().getOneLineText()));
                    paramCell.setBorder(Rectangle.NO_BORDER);

                    queryParamsTable.addCell(paramCell);

                    paramCell.setPhrase(new Phrase(
                            apiParamDoc.getDescription() + ", mandatory: " + apiParamDoc.getRequired()));

                    queryParamsTable.addCell(paramCell);
                    queryParamsTable.completeRow();
                }

                PdfPCell bluBorderCell = new PdfPCell(queryParamsTable);
                bluBorderCell.setBorder(Rectangle.NO_BORDER);
                bluBorderCell.setBorderWidthRight(1f);
                bluBorderCell.setBorderColorRight(Colors.CELL_BORDER_COLOR);

                table.addCell(ITextUtils.setOddEvenStyle(bluBorderCell, pos));
                pos++;
                table.completeRow();
            }

            // BODY OBJECT
            if (null != apiMethodDoc.getBodyobject()) {
                table.addCell(ITextUtils.getCell("Body object:", 0));
                String jsonObject = buildJsonFromTemplate(apiMethodDoc.getBodyobject().getJsondocTemplate());
                table.addCell(ITextUtils.getCell(jsonObject, pos));
                pos++;
                table.completeRow();
            }

            // RESPONSE OBJECT
            table.addCell(ITextUtils.getCell("Json response:", 0));
            table.addCell(
                    ITextUtils.getCell(apiMethodDoc.getResponse().getJsondocType().getOneLineText(), pos));
            pos++;
            table.completeRow();

            // RESPONSE STATUS CODE
            table.addCell(ITextUtils.getCell("Status code:", 0));
            table.addCell(ITextUtils.getCell(apiMethodDoc.getResponsestatuscode(), pos));
            pos++;
            table.completeRow();

            table.setSpacingAfter(10f);
            table.setSpacingBefore(5f);
            document.add(table);
        }

        document.close();
        return file;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    return null;
}