为什么下一页中的空白表行 - 似乎是iText 5.5.10中的一个错误

时间:2017-02-14 21:03:44

标签: java itext

根据要求,我必须显示一个部分,一些内容,然后只显示一个包含两列的表。生成表格时,在下一页中出现一个完整的空行是一件奇怪的事情。目前我正在使用 iText 5.5.10版本。它似乎是 itext 5.5.10版本中的错误。我认为由于表格内的单元格具有固定的高度,因此在生成时,最后一个单元格无法容纳在页面中,这就是它尝试创建空白单元格的原因。

见下图。

Problematic image

我在下面提供了完整的代码,可以在eclipse中运行以测试生成的pdf。

package com.itc.apollo.tables;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPHeaderCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class BlankCell {

    public static final String DEST = "results/tables/blankcell.pdf";

    private Paragraph createCellParagraph(String text, Font font, int align, boolean isLeftIndent) {

        Paragraph cellPara = new Paragraph(text, font);
        if (isLeftIndent)
            cellPara.setIndentationLeft(10);
        else
            cellPara.setIndentationRight(10);

        cellPara.setAlignment(align);

        return cellPara;

    }

    private PdfPCell createTableCell(int colspan) {
        PdfPCell tableCell = new PdfPCell();
        tableCell.setFixedHeight(22.5F);
        tableCell.setUseAscender(true);
        tableCell.setColspan(colspan);
        tableCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        tableCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);

        return tableCell;

    }

    private void insertCell(PdfPTable table, String text, int align, int colspan, Font font, boolean isLeftIndent, BaseColor color) {

        Paragraph para = createCellParagraph(text, font, align, isLeftIndent);

        PdfPCell tabCell = createTableCell(colspan);
        tabCell.addElement(para);

        if (null != color) {
            tabCell.setBackgroundColor(color);
        }

        table.addCell(tabCell);
    }

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    public static PdfPTable getBodyTable() {

        PdfPTable bodyTable = new PdfPTable(2);
        bodyTable.setWidthPercentage(100);
        bodyTable.setSplitLate(false);
        bodyTable.setSpacingAfter(0);
        bodyTable.setSpacingBefore(0);

        return bodyTable;
    }

    public static PdfPCell getHeaderCell(String headerName) {

        PdfPHeaderCell headCell = new PdfPHeaderCell();

        headCell.setColspan(2);
        headCell.setBorder(Rectangle.NO_BORDER);
        headCell.setPaddingLeft(22.5f);

        Font SECTION_HEADING_FONT = FontFactory.getFont("Open Sans", 14f, Font.NORMAL, new BaseColor(77, 55, 51));
        Paragraph headerParagraph = new Paragraph(headerName, SECTION_HEADING_FONT);

        headCell.addElement(headerParagraph);

        PdfPTable tab = createBlankTable();

        headCell.addElement(tab);

        return headCell;
    }

    private static PdfPTable createBlankTable() {
        PdfPTable table = new PdfPTable(1);
        table.setWidthPercentage(100);

        PdfPCell tableCell = new PdfPCell();
        tableCell.setBorder(0);
        tableCell.setFixedHeight(70.9f);
        tableCell.setUseAscender(true);
        tableCell.setUseDescender(true);
        tableCell.setColspan(1);
        table.addCell(tableCell);
        return table;
    }

    public void createPdf(String dest) throws IOException, DocumentException {

        Document document = new Document(PageSize.A4);
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();

        PdfPTable _tableLast = getBodyTable();

        PdfPCell headerCell = getHeaderCell("Objectives");

        _tableLast.addCell(headerCell);

        PdfPCell courseCell = new PdfPCell();
        courseCell.setBorder(Rectangle.NO_BORDER);
        courseCell.setColspan(2);

        PdfPTable table = new PdfPTable(3);
        table.setWidthPercentage(100);

        Font Core_Course_FONT = FontFactory.getFont("Open Sans", 12f, Font.NORMAL, new BaseColor(000, 000, 000));

        for (int i = 0; i < 80; i++) {

            if (i % 2 == 0) {
                insertCell(table, "Left Side - "+i, Element.ALIGN_LEFT, 2, Core_Course_FONT, true, new BaseColor(221, 211, 199));
                insertCell(table, "Right Side - "+i, Element.ALIGN_RIGHT, 1, Core_Course_FONT, false,new BaseColor(221, 211, 199));
            }
            else {
                insertCell(table, "Left Side - "+i, Element.ALIGN_LEFT, 2, Core_Course_FONT, true, null);
                insertCell(table, "Right Side - "+i, Element.ALIGN_RIGHT, 1, Core_Course_FONT, false, null);
            }


        }

        courseCell.addElement(table);
        _tableLast.addCell(courseCell);

        // document.add(table);

        document.add(_tableLast);
        document.close();
    }

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new BlankCell().createPdf(DEST);

        System.out.println("******************** PDF Created *********************");
    }

}

请帮我解决,并告诉我们哪个版本的iText将解决此类问题。

0 个答案:

没有答案