简体   繁体   English

iText不添加一行表格

[英]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. 我正在创建具有多个表的PDF文档,并且当表只有一行时,它不会将其添加到文档中。 I checked that the row is completed as I saw this in another post, and added the table.completeRow() just in case. 我在另一篇文章中看到了该行是否已完成,并添加了table.completeRow()以防万一。 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: 这是addGroupAdmin方法:

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. iText会忽略没有内容行的表。 Thus, 从而,

it doesn't add it to the document. 它不会将其添加到文档中。

So the solution is not to set HeaderRows in your addGroupAdmin method! 因此,解决方案不是在您的addGroupAdmin方法中设置HeaderRows

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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