【问题标题】:How to get the Content added to the cell that doesn't fit the height of the cell?如何将内容添加到不适合单元格高度的单元格中?
【发布时间】:2013-12-31 22:34:11
【问题描述】:

使用iText 并使用表格单元格。

我在一张表中有25 cells (columns)。

每列内容旋转90度,需要适合每个单元格的高度。

在某些情况下,当内容的长度超过单元格的高度时,并非所有内容都可见(Only the part of content is visible that is fitted to the height of the cell, the rest is dropped)。我想获取丢弃的内容并希望在下一个相邻单元格中显示它。

使用如下代码 -

PdfPCell cell;

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

        if(locs.get(i) == null)
        {
            cell = new PdfPCell();
            cell.setBorder(0);
            table.addCell(cell);
        }
        else
        {
            Font font = new Font(FontFamily.HELVETICA, 9, Font.BOLD, BaseColor.BLACK);  
            cell = new PdfPCell(new Phrase(locs.get(i), font));
            cell.setRotation(90);
            cell.setBorder(0);
            cell.setFixedHeight(110f);
            //cell.setMinimumHeight(10f);
            table.addCell(cell);
        }
    }

因此,如果 locs.get(i) 的值大于单元格的高度 (cell height is fixed to 110f in the above code),则某些不适合的内容将被丢弃以显示在视图中。

如何获取该内容并将其显示到相邻单元格?

【问题讨论】:

    标签: java android pdf-generation itext cell


    【解决方案1】:

    达到目的的方法如下:

    PdfPCell cell;
    
    for(int i = 0;i< 25;i++)
    {
    
        if(locs.get(i) != null)
        {
            Font font = new Font(FontFamily.HELVETICA, 9, Font.BOLD, BaseColor.BLACK);
    
    
            cell = new PdfPCell(new Phrase(locs.get(i), font));
            cell.setRotation(90);
            cell.setBorder(0);                                              
            cell.setFixedHeight(110f);
            cell.setColspan(2);               
            table.addCell(cell);
       }
        else
        {
            cell = new PdfPCell();
            cell.setBorder(0);
            table.addCell(cell);
        }
    }
    

    因此将 colspan 设置为 2 可确保如果内容超过第一列的长度,则将剩余内容移动到单元格的下一列 (One cell having two columns now after adding the colspan of 2)。

    如果有人知道做同样事情的更好方法,那么欢迎你!

    【讨论】:

    • 另一种方法是将文本添加到单元格事件中,而不是将其添加到单元格中。这样您就可以自己决定不适合的内容是否应该“溢出”。
    • @BrunoLowagie- 这是个好主意。感谢分享。
    猜你喜欢
    • 2012-08-29
    • 2016-12-09
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多