itext7格式化问题/错误

时间:2016-09-09 14:21:05

标签: itext itext7

当我注意到格式错误时,我正在使用iText dot net library。我能够在一个展示这个问题的小项目中重现它。

每当我有一个或多个大于页面高度的网格并且我在网格上调用SetKeepTogether(true)时,我会看到多个表格以奇怪的方式重叠。调用SetKeepTogether(false)时,渲染问题不会发生,但是当表格超出页面边界时,我需要表格进入新页面。

在我的例子中,我声明了一个名为CAUSE_BUG的静态布尔值。设置为true时,它将生成有缺陷的PDF。设置为false时,它将生成几乎不适合页面的表格,并且不会出现渲染问题。

我是否以某种方式错误地使用了库?如果是这样,我将如何纠正这个问题?

    const bool CAUSE_BUG = true;

    const string DEST = "sample.pdf";
    const int COL_COUNT = 5;

    static void Main(string[] args)
    {
        FileInfo file = new FileInfo(DEST);
        file.Directory.Create();

        PdfWriter writer = new PdfWriter(DEST);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf, PageSize.Default.Rotate());

        document.Add(CreateTable(4));
        document.Add(CreateTable(8));
        if (CAUSE_BUG)
        {
            // The bug happens when one or more tables that are marked to keep together extend
            //  past the boundary of the page's height. 21 rows is just over the page height.
            document.Add(CreateTable(21));
            document.Add(CreateTable(21));
            document.Add(CreateTable(21));
        }
        else
        {
            // These grids should barely fit in a single page which will cause them to render nicely.
            document.Add(CreateTable(20));
            document.Add(CreateTable(20));
            document.Add(CreateTable(20));
        }

        document.Close();
    }

    private static Table CreateTable(int rows)
    {
        Table t = new Table(COL_COUNT);
        t.SetWidthPercent(100);

        Cell topCell = new Cell(1, COL_COUNT);
        topCell.Add("FULL WIDTH HEADER");
        topCell.SetBorder(Border.NO_BORDER);
        topCell.SetBold();
        t.AddHeaderCell(topCell);

        Cell threeWide = new Cell(1, 3);
        threeWide.Add("Three Wide");
        threeWide.SetBold();
        t.AddHeaderCell(threeWide);

        Cell twoWide = new Cell(1, 2);
        twoWide.Add("Two Wide");
        twoWide.SetBold();
        t.AddHeaderCell(twoWide);

        // Add dummy rows
        for (int rowIndex = 0; rowIndex < rows; rowIndex++)
        {
            for (int i = 0; i < COL_COUNT; i++)
            {
                t.AddCell(string.Format("R{0} C{0}", rowIndex, i));
            }
            t.StartNewRow();
        }

        // Setting this to false will fix the issue
        t.SetKeepTogether(true);

        return t;
    }

0 个答案:

没有答案