简体   繁体   中英

iText doesn't add table with one row

I'm creating a PDF document with a number of tables and when the table has only one row it doesn't add it to the document. I checked that the row is completed as I saw this in another post, and added the table.completeRow() just in case. However the only way it works is adding another empty row:

table.addCell(new Paragraph("", paragraphFontNormal));
table.completeRow()

I don't think this is a nice solution and I'd like to know what's wrong here. Can anyone help please?

This is the method:

public void createPdf(DateRange dates, HttpServletResponse response) throws DocumentException, IOException {

    boolean first = true;
    Document document = new Document(PageSize.A4.rotate());
    PdfWriter.getInstance(document, response.getOutputStream());
    document.open();

    List<Rejects> report = findAll(dates);

    for(Rejects rejects : report){
        if (first) {
            first = false;
            addGroupAdmin(rejects, document, dates);
        }else{
            if (rejects.getAdminGroup().equals(adminGroup)) {
                table.addCell(new Paragraph(rejects.getZone(), paragraphFontNormal));
                table.addCell(new Paragraph(dateFormat.format(rejects.getRejDate()), paragraphFontNormal));
                table.addCell(new Paragraph(rejects.getProcName(), paragraphFontNormal));
                table.addCell(new Paragraph(rejects.getKindId(), paragraphFontNormal));
                table.addCell(new Paragraph(rejects.getVarietyName(), paragraphFontNormal));
                table.addCell(new Paragraph(rejects.getSlrn(), paragraphFontNormal));
                table.addCell(new Paragraph(rejects.getRejReason(), paragraphFontNormal));
                table.addCell(new Paragraph(rejects.getAdminGroup(), paragraphFontNormal));
                table.addCell(new Paragraph(rejects.getDescription(), paragraphFontNormal));
            }else{

                /*** It doesn't work like this for one row***/  
                document.add(table);

                document.newPage();
                addGroupAdmin(rejects, document, dates);
            }
        }
    }

    /*** It works like this ***/
    table.addCell(new Paragraph("", paragraphFontNormal));
    table.completeRow();
    document.add(table);

    document.newPage();
    response.flushBuffer();
    document.close();
}

This is the addGroupAdmin method:

private void addGroupAdmin(Rejects rejects, Document document, DateRange dates) throws DocumentException {
        adminGroup = rejects.getAdminGroup();
        Paragraph headLine = new Paragraph("REJECTED APPLICATION LIST " + dateFormat.format(dates.getStartDate())
                + "-" + dateFormat.format(dates.getEndDate()) + " - " + rejects.getAdminGroupDesc() , paragraphFontBold);
        headLine.setAlignment(Element.ALIGN_CENTER);
        document.add(headLine);
        document.add( Chunk.NEWLINE );

        table = new PdfPTable(9);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        table.getDefaultCell().setMinimumHeight(20);
        table.getDefaultCell().setPaddingBottom(10);
        table.setWidthPercentage(900);
        table.setHeaderRows(1);
        table.setTotalWidth(new float[]{ 40, 60, 140, 30, 60, 90, 30, 90, 100 });
        table.setLockedWidth(true);

        table.addCell(new Paragraph(rejects.getZone(), paragraphFontNormal));
        table.addCell(new Paragraph(dateFormat.format(rejects.getRejDate()), paragraphFontNormal));
        table.addCell(new Paragraph(rejects.getProcName(), paragraphFontNormal));
        table.addCell(new Paragraph(rejects.getKindId(), paragraphFontNormal));
        table.addCell(new Paragraph(rejects.getVarietyName(), paragraphFontNormal));
        table.addCell(new Paragraph(rejects.getSlrn(), paragraphFontNormal));
        table.addCell(new Paragraph(rejects.getRejReason(), paragraphFontNormal));
        table.addCell(new Paragraph(rejects.getAdminGroup(), paragraphFontNormal));
        table.addCell(new Paragraph(rejects.getDescription(), paragraphFontNormal));
}

You set

table.setHeaderRows(1);

This means that the first row you add to the table is used as header row.

Thus, if

the table has only one row

then the table has a header row but no content rows. iText ignores tables without content rows. Thus,

it doesn't add it to the document.

So the solution is not to set HeaderRows in your addGroupAdmin method!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM