Search code examples
javaandroidpdf-generationitextcell

How to get the Content added to the cell that doesn't fit the height of the cell?


Working with iText and using table cells.

I have 25 cells (columns) in a table.

The content of each column is rotated 90 degree and is required to be fitted to the height of each cell.

In some cases when the length of the content exceeds the height of the cell, not all the content is visible (Only the part of content is visible that is fitted to the height of the cell, the rest is dropped). I want to get that dropped content and want to show it in the next adjacent cell.

The following code is used -

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);
        }
    }

So if the value of locs.get(i) is greater then the height of the cell (cell height is fixed to 110f in the above code), some content that doesn't fit gets dropped to be shown in the view.

How to get that content and show it to the adjacent cell ?


Solution

  • The method used that solved the purpose is the following:

    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);
        }
    }
    

    So setting the colspan to 2 ensures that if the contents exceeds the length of the first column then move the remaining contents to the next column of the cell (One cell having two columns now after adding the colspan of 2).

    If anyone knows the better way to do the same thing then you are welcome!