Example usage for com.itextpdf.text Document add

List of usage examples for com.itextpdf.text Document add

Introduction

In this page you can find the example usage for com.itextpdf.text Document add.

Prototype


public boolean add(Element element) throws DocumentException 

Source Link

Document

Adds an Element to the Document.

Usage

From source file:be.rheynaerde.poolsheets.AbstractPoolSheet.java

License:Open Source License

protected void buildTitle(Document document) throws DocumentException {
    //the title is placed in a one-column table of width 100%
    PdfPTable table = new PdfPTable(1);
    table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
    table.setWidthPercentage(100f);//from ww  w  .  j a  v a2  s . c  om

    //one row for the title
    PdfPCell cell = new PdfPCell(new Phrase(configuration.getTitle(), configuration.getTitleFont()));
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setPaddingBottom(10f);
    table.addCell(cell);

    //one row for the subtitle
    cell = new PdfPCell(new Phrase(configuration.getSubtitle(), configuration.getSubtitleFont()));
    cell.setBorder(Rectangle.NO_BORDER);
    cell.setBorder(Rectangle.TOP);
    table.addCell(cell);

    //make sure that there is some spacing between the title and the scores
    //20f just seems to work allright, but maybe something more configurable
    //would be better.
    table.setSpacingAfter(20f);
    document.add(table);
}

From source file:be.rheynaerde.poolsheets.AbstractPoolSheet.java

License:Open Source License

protected void buildBoutOrder(Document document) throws DocumentException {
    PdfPTable table = new PdfPTable(configuration.getBoutOrderColumns());
    BoutOrder mo = configuration.getBoutOrder();
    if (mo.getNrOfBouts() == 0)
        return;/*from   ww w . j av a 2s .  c om*/
    int rows = mo.getNrOfBouts() / configuration.getBoutOrderColumns();
    if (mo.getNrOfBouts() % configuration.getBoutOrderColumns() != 0)
        rows++;
    int shortComing = (rows - (mo.getNrOfBouts() % rows)) % rows;
    for (int i = 0; i < rows - shortComing; i++) {
        for (int j = 0; j < configuration.getBoutOrderColumns(); j++) {
            int boutNumber = j * rows + i;
            table.addCell(getBoutCell(mo, boutNumber));
        }
        for (int j = 0; j < configuration.getBoutOrderSpacing(); j++) {
            for (int k = 0; k < configuration.getBoutOrderColumns(); k++) {
                PdfPCell spacing = new PdfPCell();
                spacing.setBorder(Rectangle.NO_BORDER);
                spacing.setFixedHeight(configuration.getSquareCellSize());
                table.addCell(spacing);
            }
        }
    }
    for (int i = rows - shortComing; i < rows; i++) {
        for (int j = 0; j < configuration.getBoutOrderColumns() - 1; j++) {
            int boutNumber = j * rows + i;
            table.addCell(getBoutCell(mo, boutNumber));
        }
        PdfPCell cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        for (int j = 0; j < configuration.getBoutOrderSpacing(); j++) {
            for (int k = 0; k < configuration.getBoutOrderColumns(); k++) {
                PdfPCell spacing = new PdfPCell();
                spacing.setBorder(Rectangle.NO_BORDER);
                spacing.setFixedHeight(configuration.getSquareCellSize());
                table.addCell(spacing);
            }
        }
    }
    table.setSpacingBefore(20f);
    document.add(table);
}

From source file:be.rheynaerde.poolsheets.AbstractPufPoolSheet.java

License:Open Source License

@Override
protected void buildTable(Document document) throws DocumentException {
    //large table to take care of the layout
    PdfPTable largeTable = new PdfPTable(3);
    largeTable.setWidths(new int[] { 19, 1, 20 });
    final float largeTableHeight = configuration.getSquareCellSize() * (getNumberOfRows() + 1);
    PdfPCell cell1 = getEmptyCell();/*from w  w  w . j  a v a 2  s.  c  o  m*/
    cell1.setFixedHeight(largeTableHeight);
    cell1.addElement(getNameTable());
    largeTable.addCell(cell1);
    PdfPCell cellSpacer = getEmptyCell();
    cellSpacer.setFixedHeight(largeTableHeight);
    largeTable.addCell(cellSpacer);
    PdfPCell cell2 = getEmptyCell();
    cell2.setFixedHeight(largeTableHeight);
    cell2.addElement(getScoreTable());
    largeTable.addCell(cell2);
    document.add(largeTable);
}

From source file:be.rheynaerde.poolsheets.ClubPoolSheet.java

License:Open Source License

@Override
protected void buildTable(Document document) throws DocumentException {
    int columnCount = configuration.getNrOfPlayers() + configuration.getSummaryColumnCount() + 3;
    int nameCellWidth = 5;

    PdfPTable table = new PdfPTable(columnCount);
    table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
    table.setTotalWidth(//from   w w w  .j av a  2 s.  co m
            (configuration.getNrOfPlayers() + 1 + nameCellWidth + 0.1f + configuration.getSummaryColumnCount())
                    * configuration.getSquareCellSize());
    table.setLockedWidth(true);
    float[] widths = new float[columnCount];
    widths[0] = 1f * nameCellWidth;
    for (int i = 1; i < widths.length; i++) {
        widths[i] = 1f;
    }
    widths[widths.length - 1 - configuration.getSummaryColumnCount()] = 0.1f;
    table.setWidths(widths);

    PdfPCell cell = new PdfPCell(new Paragraph(configuration.getTitle(), configuration.getTitleFont()));
    cell.setColspan(columnCount);
    cell.setPaddingBottom(configuration.getSquareCellSize() / 2);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    table.addCell(cell);

    {
        table.addCell(getHeaderCell(bundle.getString("name")));
        table.addCell(getSolidCell());
        for (int i = 0; i < configuration.getNrOfPlayers(); i++) {
            table.addCell(getHeaderCell(Integer.toString(i + 1)));
        }
        table.addCell(new PdfPCell()); //spacer column
        for (int i = 0; i < configuration.getSummaryColumnCount(); i++) {
            table.addCell(getHeaderCell(configuration.getSummaryColumnName(i)));
        }
    }

    for (int i = 0; i < configuration.getNrOfPlayers(); i++) {
        table.addCell(configuration.getNamePlayer(i + 1) == null ? "" : configuration.getNamePlayer(i + 1));
        table.addCell(getHeaderCell(Integer.toString(i + 1)));
        for (int j = 0; j < i; j++) {
            String result = configuration.getResult(i + 1, j + 1);
            PdfPCell resultCell = new PdfPCell(new Phrase(result == null ? "" : result));
            resultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            resultCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(resultCell);
        }
        table.addCell(getSolidCell());
        for (int j = i + 1; j < configuration.getNrOfPlayers(); j++) {
            String result = configuration.getResult(i + 1, j + 1);
            PdfPCell resultCell = new PdfPCell(new Phrase(result == null ? "" : result));
            resultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            resultCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(resultCell);
        }
        table.addCell(new PdfPCell()); //spacer column
        for (int j = 0; j < configuration.getSummaryColumnCount(); j++) {
            String result = configuration.getSummaryColumnValue(i + 1, j);
            PdfPCell summaryCell = new PdfPCell(new Phrase(result == null ? "" : result));
            summaryCell.setHorizontalAlignment(Element.ALIGN_CENTER);
            summaryCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(summaryCell);
        }
    }

    document.add(table);
}

From source file:be.rheynaerde.poolsheets.StandardPoolSheet.java

License:Open Source License

protected void buildTable(Document document) throws DocumentException {
    int columnCount = 1 + 1 + configuration.getNrOfPlayers() + 1 + configuration.getSummaryColumnCount();
    // name + number + scores + spacer 
    //    + summary
    PdfPTable table = new PdfPTable(columnCount);
    table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);

    table.setTotalWidth((configuration.getNrOfPlayers() + 7 //the name column + extra space between grid and summary
                                                            //currently the name column just gets a width that is
                                                            //a multiple of the width of the other cells. Better
                                                            //would be to let this cell take up the remaining space.
            + configuration.getSummaryColumnCount()) * configuration.getSquareCellSize());
    table.setLockedWidth(true);/* w  w w.ja v  a2  s .co m*/
    float[] widths = new float[columnCount];
    widths[0] = 5f;
    for (int i = 1; i < widths.length; i++) {
        widths[i] = 1f;
    }
    widths[widths.length - 1 - configuration.getSummaryColumnCount()] = 0.1f;
    table.setWidths(widths);

    {
        table.addCell(getHeaderCell(bundle.getString("name")));
        table.addCell(getSolidCell());
        for (int i = 0; i < configuration.getNrOfPlayers(); i++) {
            table.addCell(getHeaderCell(Integer.toString(i + 1)));
        }
        table.addCell(new PdfPCell()); //spacer column
        for (int i = 0; i < configuration.getSummaryColumnCount(); i++) {
            table.addCell(getHeaderCell(configuration.getSummaryColumnName(i)));
        }
    }

    for (int i = 0; i < configuration.getNrOfPlayers(); i++) {
        table.addCell(configuration.getNamePlayer(i + 1) == null ? "" : configuration.getNamePlayer(i + 1));
        table.addCell(getHeaderCell(Integer.toString(i + 1)));
        for (int j = 0; j < i; j++) {
            String result = configuration.getResult(i + 1, j + 1);
            PdfPCell cell = new PdfPCell(new Phrase(result == null ? "" : result));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
        }
        table.addCell(getSolidCell());
        for (int j = i + 1; j < configuration.getNrOfPlayers(); j++) {
            String result = configuration.getResult(i + 1, j + 1);
            PdfPCell cell = new PdfPCell(new Phrase(result == null ? "" : result));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
        }
        table.addCell(new PdfPCell()); //spacer column
        for (int j = 0; j < configuration.getSummaryColumnCount(); j++) {
            String result = configuration.getSummaryColumnValue(i + 1, j);
            PdfPCell cell = new PdfPCell(new Phrase(result == null ? "" : result));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
        }
    }
    document.add(table);
}

From source file:be.roots.taconic.pricingguide.service.PDFServiceImpl.java

License:Open Source License

private byte[] createModelPages(Contact contact, List<Model> models, Toc tableOfContents)
        throws IOException, DocumentException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Document document = iTextUtil.createNewDocument();
    PdfWriter writer = PdfWriter.getInstance(document, bos);

    document.open();// w w  w .  ja va  2s  .  co m

    // create a PdfPTable for every model (so we can measure the height)
    final List<PdfPTable> modelPageTables = new ArrayList<>();
    Collections.sort(models);
    for (Model model : models) {
        if (model != null) {
            modelPageTables.add(createModelPage(contact, model, writer));
        }
    }

    // put the PdfPTable Models tables on PDF pages (multiple per page if possible)
    byte[] pages = new byte[] {};

    int height = 0;
    List<String> pageNames = new ArrayList<>();

    int i = 0;
    for (PdfPTable modelPageTable : modelPageTables) {

        // create a new pdf page, if necessary
        if (height != 0 && (height + modelPageTable.getTotalHeight()
                + 20 /* for the line separator */ ) >= iTextUtil.PAGE_HEIGHT) {

            writer.close();
            document.close();
            bos.close();

            byte[] page = bos.toByteArray();
            tableOfContents.addEntries(2, pageNames, page, true,
                    Toc.MODEL_SORT_PREFIX + "___" + IntUtil.format(++i));
            pages = iTextUtil.merge(pages, page);

            height = 0;
            pageNames.clear();
            bos = new ByteArrayOutputStream();
            document = iTextUtil.createNewDocument();
            writer = PdfWriter.getInstance(document, bos);

            document.open();

        } else if (height != 0) {
            // if not the first model on the page, draw a separator

            document.add(new Paragraph(
                    new Chunk(new LineSeparator(.25f, 80f, BaseColor.LIGHT_GRAY, Element.ALIGN_CENTER, -2))));
            document.add(new Paragraph(new Chunk("   ")));
        }

        // rerender the table (with a valid pdfWriter)
        document.add(createModelPage(contact, models.get(modelPageTables.indexOf(modelPageTable)), writer));
        height += modelPageTable.getTotalHeight();
        pageNames.add(models.get(modelPageTables.indexOf(modelPageTable)).getProductNameProcessed());

    }
    writer.close();
    document.close();

    byte[] page = bos.toByteArray();
    tableOfContents.addEntries(2, pageNames, page, true, Toc.MODEL_SORT_PREFIX + "___" + IntUtil.format(++i));
    pages = iTextUtil.merge(pages, page);

    return pages;

}

From source file:be.thomasmore.controller.InputBean.java

public void createPdf(String filename) throws DocumentException, IOException {
    // step 1/*from www. jav a2  s  .  co m*/
    Document document = new Document();
    // step 2
    PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    document.add(new Paragraph("Hello World!"));
    // step 5
    document.close();
}

From source file:be.thomasmore.controller.PdfController.java

public void createPdfKlas() {

    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext()
            .getRequestParameterMap();//from   w  w  w .java  2s. co m
    String KlasId = params.get("klasId");
    int id = Integer.parseInt(KlasId);
    Document document = new Document();
    Klas klas = service.getKlas(id);
    List<Klastest> klastesten = klas.getKlastestList();
    List<Test> testen = new ArrayList<Test>();

    HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext()
            .getResponse();
    res.setHeader("Content-Disposition", "attachement; filename=" + klas.getNummer() + "-resultaten.pdf");
    res.setContentType("application/pdf");

    try {
        PdfWriter.getInstance(document, res.getOutputStream());

        document.open();

        Font font = FontFactory.getFont("Calibri");
        Font fontbold = FontFactory.getFont("Calibri", Font.BOLD);

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

        PdfPCell cell1 = new PdfPCell(new Paragraph("Vak", font));
        PdfPCell cell2 = new PdfPCell(new Paragraph("Student", font));
        PdfPCell cell3 = new PdfPCell(new Paragraph("Behaald", font));

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

        for (Klastest klastest : klastesten) {
            Test test = klastest.getTestId();
            testen.add(service.getTest(test.getId()));
        }

        for (Test test : testen) {
            List<Score> scores = test.getScoreList();
            Vak vak = test.getVakId();
            for (Score score : scores) {
                Student student = score.getStudentId();
                PdfPCell cellVak = new PdfPCell(new Paragraph(vak.getNaam(), font));
                PdfPCell cellStudent = new PdfPCell(new Paragraph(student.getVoornaam(), font));
                PdfPCell cellScore = new PdfPCell(new Paragraph(score.getScore().toString(), font));

                table.addCell(cellVak);
                table.addCell(cellStudent);
                table.addCell(cellScore);
            }
        }

        document.add(new Phrase("Klas: ", font));
        document.add(new Phrase(klas.getNummer(), font));
        document.add(table);

        document.close();
    } catch (Exception e) {

    }

}

From source file:be.thomasmore.controller.PdfController.java

public void createPdfTest() {
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext()
            .getRequestParameterMap();/*  w w  w.ja  v a  2  s.c om*/
    String KlasId = params.get("klasId3");
    String TestId = params.get("testId");
    int klasId = Integer.parseInt(KlasId);
    int id = Integer.parseInt(TestId);
    Document document = new Document();
    Test test = service.getTest(id);
    List<Score> scores = test.getScoreList();
    Klas klas = service.getKlas(klasId);
    Vak vak = test.getVakId();

    HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext()
            .getResponse();
    res.setHeader("Content-Disposition", "attachement; filename=" + test.getBeschrijving() + "-resultaten.pdf");
    res.setContentType("application/pdf");

    try {
        PdfWriter.getInstance(document, res.getOutputStream());

        document.open();

        Font font = FontFactory.getFont("Calibri");
        Font fontbold = FontFactory.getFont("Calibri", Font.BOLD);

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

        PdfPCell cell1 = new PdfPCell(new Paragraph("Test", font));
        PdfPCell cell2 = new PdfPCell(new Paragraph("Student", font));
        PdfPCell cell3 = new PdfPCell(new Paragraph("Score", font));

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

        for (Score score : scores) {
            Student student = score.getStudentId();
            PdfPCell cellTest = new PdfPCell(new Paragraph(test.getBeschrijving(), font));
            PdfPCell cellStudent = new PdfPCell(new Paragraph(student.getVoornaam(), font));
            PdfPCell cellScore = new PdfPCell(new Paragraph(score.getScore().toString()));

            table.addCell(cellTest);
            table.addCell(cellStudent);
            table.addCell(cellScore);
        }

        document.add(new Phrase("Klas: ", font));
        document.add(new Phrase(klas.getNummer(), font));
        document.add(new Phrase("  Vak: ", font));
        document.add(new Phrase(vak.getNaam(), font));
        document.add(table);

        document.close();
    } catch (Exception e) {

    }

}

From source file:be.thomasmore.controller.PdfController.java

public void createPdfVak() {
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext()
            .getRequestParameterMap();//from   w  ww. j  a v a2  s.  com
    String KlasId = params.get("klasId2");
    String VakId = params.get("vakId");
    int klasId = Integer.parseInt(KlasId);
    int id = Integer.parseInt(VakId);
    Document document = new Document();
    Vak vak = service.getVak(id);
    List<Test> testen = vak.getTestList();
    Klas klas = service.getKlas(klasId);

    HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext()
            .getResponse();
    res.setHeader("Content-Disposition", "attachement; filename=" + vak.getNaam() + "-resultaten.pdf");
    res.setContentType("application/pdf");

    try {
        PdfWriter.getInstance(document, res.getOutputStream());

        document.open();

        Font font = FontFactory.getFont("Calibri");
        Font fontbold = FontFactory.getFont("Calibri", Font.BOLD);

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

        PdfPCell cell1 = new PdfPCell(new Paragraph("Test", font));
        PdfPCell cell2 = new PdfPCell(new Paragraph("Student", font));
        PdfPCell cell3 = new PdfPCell(new Paragraph("Score", font));

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

        for (Test test : testen) {
            List<Score> scores = test.getScoreList();
            for (Score score : scores) {
                Student student = score.getStudentId();
                PdfPCell cellTest = new PdfPCell(new Paragraph(test.getBeschrijving(), font));
                PdfPCell cellStudent = new PdfPCell(new Paragraph(student.getVoornaam(), font));
                PdfPCell cellScore = new PdfPCell(new Paragraph(score.getScore().toString()));

                table.addCell(cellTest);
                table.addCell(cellStudent);
                table.addCell(cellScore);
            }
        }

        document.add(new Phrase("Klas: ", font));
        document.add(new Phrase(klas.getNummer(), font));
        document.add(new Phrase("  Vak: ", font));
        document.add(new Phrase(vak.getNaam(), font));
        document.add(table);

        document.close();
    } catch (Exception e) {

    }
}