【问题标题】:Why doesn't iText add tables which are near the page bottom to the document?为什么 iText 不将靠近页面底部的表格添加到文档中?
【发布时间】:2011-10-19 22:29:33
【问题描述】:

我有很多小表,我将它们封装在一个环绕的 1x1 表中,并将环绕表上的 SplitRows 属性设置为 false。这样,我可以避免我的表格在到达页面底部时被拆分。当我到达一页的末尾,并且有一点文本空间,但对于下一个表格来说不够用时,iText 根本不添加表格,而是继续在列表中添加下一个表格。

如果当前页面没有足够的空间放置表格,id喜欢将其发送到下一个。我能做什么?

http://compgroups.net/comp.text.pdf/Avoid-page-breaks-in-PdfPTable-using-iText-1.2

这是我的代码:

public static void CreateMatrixProcentQuestionTable(ShowQuestionViewModel model, Document doc)
    {
        ShowMatrixQuestionViewModel sm = (ShowMatrixQuestionViewModel)model;

        Font fontsize = new Font(Font.FontFamily.HELVETICA, 9f);
        Font QuestionFont = new Font(Font.FontFamily.HELVETICA, 12f);

        PdfPTable table = new PdfPTable(sm.columns.Count + 2);

        // Tilføj spørgsmålet i en række for sig selv, ellers er der chance for at 
        // svarmulighederne ikke kommer med ved page breaks
        PdfPCell question = new PdfPCell(new Phrase(sm.Question_Wording + Environment.NewLine, QuestionFont));
        question.Border = Rectangle.NO_BORDER;
        question.Colspan = table.NumberOfColumns;
        table.AddCell(question);

        // Tilføj et mellemrum mellem spørgsmålet og svarmulighederne
        PdfPCell mellemrum = new PdfPCell(new Phrase(Environment.NewLine));
        mellemrum.Border = Rectangle.NO_BORDER;
        mellemrum.Colspan = table.NumberOfColumns;
        table.AddCell(mellemrum);

        // Tilføj rækker og kolonner 

        // Dette er den første tomme celle
        table.AddCell(new PdfPCell(new Phrase("", fontsize)));

        foreach (MatrixColumns column in sm.columns)
        {
            PdfPCell cell = new PdfPCell(new Phrase(column.Column_Description, fontsize));
            cell.HorizontalAlignment = 1;
            table.AddCell(cell);
        }

        PdfPCell ialt = new PdfPCell(new Phrase("I alt", fontsize));
        ialt.HorizontalAlignment = 1;
        table.AddCell(ialt);


        foreach (var pair in sm.columnrow)
        {
            MatrixRows row = pair.Key;

            PdfPCell rowcell = new PdfPCell(new Phrase(row.Row_Description == null ? "*" : row.Row_Description, fontsize));
            rowcell.HorizontalAlignment = 1;

            table.AddCell(rowcell);

            foreach (MatrixColumns column in pair.Value)
            {
                PdfPCell cell = new PdfPCell(new Phrase("%", fontsize));
                cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                table.AddCell(cell);
            }

            PdfPCell sumcell = new PdfPCell(new Phrase("100%", fontsize));
            sumcell.HorizontalAlignment = Element.ALIGN_RIGHT;
            table.AddCell(sumcell);

        }

        // Man laver en 1x1 table uden om den rigtige table, og sætter 
        // SplitRows = False. Dette gør at tabellen ikke bliver knækket over 
        // ved page breaks

        PdfPTable sorroundingTable = new PdfPTable(1);
        PdfPCell innerTable = new PdfPCell(table);

        innerTable.Border = Rectangle.NO_BORDER;
        sorroundingTable.AddCell(innerTable);

        sorroundingTable.SplitRows = false;

        doc.Add(sorroundingTable);
        doc.Add(new Phrase(Environment.NewLine));
    }

【问题讨论】:

    标签: java c# pdf itext


    【解决方案1】:

    这样就解决了问题:

    table.setKeepTogether(true)
    document.add(table)
    

    【讨论】:

    • 请接受您自己的答案。这将从未回答列表中删除该问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2020-11-03
    • 2015-04-19
    相关资源
    最近更新 更多