iText 5.5.4+中的PdfPTable是否有错误?

时间:2015-02-13 14:37:12

标签: java pdf itext

自使用表格和setComplete()方法以来,iText 5.5.4似乎存在一个错误,或者至少有默认行为的变化。

如果我使用iText 5.5.4运行下面的代码,生成的pdf将包含每页恰好20行的页面(除了最后一页,但只有一行)。与iText 5.5.5相同的结果。使用iText 5.5.3,每页的行数符合页面的高度,导致总页数较少。

以下是重现行为的示例程序:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfPTableTest {

    private File file;
    private Font font;

    private Random rand;

    public PdfPTableTest() {
        file = new File("out.pdf");
        rand = new Random();
        font = FontFactory.getFont(FontFactory.HELVETICA, BaseFont.WINANSI,
                BaseFont.NOT_EMBEDDED, 12, Font.NORMAL, BaseColor.BLACK, true);
    }

    public void run() {
        try (
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(fos)
            )
        {
            Document document = new Document(PageSize.A4.rotate());
            PdfWriter.getInstance(document, bos);
            document.addTitle("itext PdfPTable setComplete() test");
            document.addCreator("test");
            document.addAuthor("test");
            document.addSubject("test");

            document.open();

            float[] widths = new float[] {50f, 50f};
            PdfPTable table = new PdfPTable(widths.length);
            table.setComplete(false);
            table.setWidths(widths);
            table.setWidthPercentage(100);

            PdfPCell cell = new PdfPCell(new Phrase("Column #1", font));
            table.addCell(cell);
            cell = new PdfPCell(new Phrase("Column #2", font));
            table.addCell(cell);
            table.setHeaderRows(1);

            for (int i = 0; i < 250; i++) {
                cell = new PdfPCell(new Phrase("Table cell #" + i, font));
                table.addCell(cell);
                String s = getRandomText();
                cell = new PdfPCell(new Phrase(s, font));
                table.addCell(cell);
                if (i % 20 == 0) {
                    document.add(table);
                }
            }

            table.setComplete(true);
            document.add(table);
            document.close();

        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
    }

    public String getRandomText() {
        int length = rand.nextInt(100);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            if (i % 15 == 0) {
                sb.append(" ");
            }
            sb.append(Integer.valueOf(rand.nextInt(10)));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        PdfPTableTest pdfTest = new PdfPTableTest();
        pdfTest.run();
    }

}

如果我增加最大值随机生成的文本的长度为120个字符

int length = rand.nextInt(120);

然后还会抛出异常:

com.itextpdf.text.DocumentException: com.itextpdf.text.DocumentException: Infinite table loop; row content is larger than the page (for instance because you didn't scale an image).
    at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:809)
    at com.itextpdf.text.Document.add(Document.java:278)
    at de.itext.test.PdfPTableTest.run(PdfPTableTest.java:69)
    at de.itext.test.PdfPTableTest.main(PdfPTableTest.java:96)
Caused by: com.itextpdf.text.DocumentException: Infinite table loop; row content is larger than the page (for instance because you didn't scale an image).
    at com.itextpdf.text.pdf.PdfDocument.addPTable(PdfDocument.java:2679)
    at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:750)
    ... 3 more

在iText 5.5.3中没有例外,pdf看起来很好(导致单元格有3条单独的行)。

任何人都可以确认这是一个错误(将来会在未来版本中修复)吗?

0 个答案:

没有答案