javaitext

last row is not created in table with itext 2.1.7 pdf


how are you,

I am designing a pdf with itext but I can not fill the entire table of items because I always miss one at the end.

It seems that with the last item does not add the next row any idea?

This is my original application with the item, in the part of CAPEC, everything I want to introduce it in cells with itext

CVE-2010-3972 When I add this directly to a document type object everything appears, but when I want to do it in a table that last item is not added

This is my code in java

private void capecReferences() throws DocumentException, IOException {
        PdfPTable tableCapec = new PdfPTable(2);
        tableCapec.setHeaderRows(0);
        tableCapec.setWidthPercentage(100);
        tableCapec.setTotalWidth(100);
        // Add headers
        tableCapec.addCell(createHeaderCellWithColor("CAPEC & References"));
        document.add(tableCapec);
        /*
         * CAPEC
         */
        if (cve.getCapec().size() > 0) {
            Paragraph capec = new Paragraph(new Chunk("\nCAPEC", captionFont));
            PdfPTable table = new PdfPTable(2);
            table.setHeaderRows(1);
            table.setWidthPercentage(100);
            table.setTotalWidth(100);
            Paragraph capecs = new Paragraph(new Chunk("",normalFont));
            for (Capec capecTmp : cve.getCapec()) {
                table.addCell(createCell(capecTmp.getName()));
                //capecs.add("\n"+capecTmp.getName());
                //document.add(table);
            }
            document.add(capec);
            //document.add(capecs);
            document.add(table);
            document.add(Chunk.NEWLINE);
        }
        /*
         * REFERENCES
         */
        if (cve.getReferences().size() > 0) {
            Paragraph references = new Paragraph(new Chunk("\nREFERENCES", captionFont));
            Paragraph r = new Paragraph();
            Anchor link;
            for (String reference : cve.getReferences()) {
                link = new Anchor(reference);
                link.setReference(reference);
                r.add(link);
            }
            document.add(references);
            document.add(r);
            document.add(Chunk.NEWLINE);
        }
        /*
         * SCPI
         */
        if (cve.getMapCveScip() != null) {
            Paragraph scpi = new Paragraph(new Chunk("\nSCPI", captionFont));
            Paragraph scpiID = new Paragraph(new Chunk("SCIP ID: ", normalFont));
            Paragraph scpiLink = new Paragraph(new Chunk("SCIP Link: ", normalFont));
            Anchor link2;
            scpiID.add(cve.getMapCveScip().getScipid());
            link2 = new Anchor(cve.getMapCveScip().getSciplink());
            link2.setReference(cve.getMapCveScip().getSciplink());
            scpiLink.add(link2);
            document.add(scpi);
            document.add(scpiID);
            document.add(scpiLink);
            document.add(Chunk.NEWLINE);
        }
        /*
         * Nessus
         */
        if (cve.getMapCveNessus() != null) {
            Paragraph nessus = new Paragraph(new Chunk("Nessus ",captionFont));
            document.add(nessus);       
            Paragraph nessusScriptId = new Paragraph("Nessus Script Id: ", normalFont);
            nessusScriptId.add(new Chunk(cve.getMapCveNessus().getNessusScriptId()));
            document.add(nessusScriptId);       
            Paragraph nessusScriptName = new Paragraph("Nessus Script Name: ", normalFont);
            nessusScriptName.add(new Chunk(cve.getMapCveNessus().getNessusScriptName()));
            document.add(nessusScriptName); 
            Paragraph nessusScriptFile = new Paragraph("Nessus Script File: ", normalFont);     
            nessusScriptFile.add(new Chunk(cve.getMapCveNessus().getNessusScriptFile()));       
            document.add(nessusScriptFile);
            Paragraph nessusScriptFamily = new Paragraph("Nessus Script Family: ", normalFont);
            nessusScriptFamily.add(new Chunk(cve.getMapCveNessus().getNessusScriptFamily()));
            document.add(nessusScriptFamily);
            document.add(Chunk.NEWLINE);
        }
    }

And this is the created pdf, This is missing from the table

Client-side Injection-induced Buffer Overflow

capecPDF


Solution

  • Your question text makes us assume the collection cve.getCapec() contains the 11 values from the top screenshot of your question including Client-side Injection-induced Buffer Overflow; in this loop you add them to a two-column table:

    /*
     * CAPEC
     */
    if (cve.getCapec().size() > 0) {
        Paragraph capec = new Paragraph(new Chunk("\nCAPEC", captionFont));
        PdfPTable table = new PdfPTable(2);
        table.setHeaderRows(1);
        table.setWidthPercentage(100);
        table.setTotalWidth(100);
        Paragraph capecs = new Paragraph(new Chunk("",normalFont));
        for (Capec capecTmp : cve.getCapec()) {
            table.addCell(createCell(capecTmp.getName()));
            //capecs.add("\n"+capecTmp.getName());
            //document.add(table);
        }
        document.add(capec);
        //document.add(capecs);
        document.add(table);
        document.add(Chunk.NEWLINE);
    }
    

    As there are 11 entries in the collection this means that the for the final row of the table you only add one cell. Thus, that row is incomplete and won't be drawn.

    You should either track whether the number of entries you added is even (e.g. by counting or by switching a boolean value); if it is not even, you should finally add another, empty cell.

    (Or if you want to rely on iText not drawing incomplete rows, simply always add an empty cell after the loop.)


    As an aside, if possible you should really update.