1

I'm using iText to create a PDF with a table. The table headers have 90 degree rotated text, which I'm adding using a CellEvent (code below). This works great, except when the table spans multiple pages the rotated cell header text flows off the top of the page.

I've tried setting cell.setFixedHeight(100) but it doesn't seem to affect the cell. I've tried this solution as well but I can't get the cell to display the resulting image with text at all.

    @Override
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {

    PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];

    try {
                    canvas.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, false), this.fontSize);
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
    }

    if (this.alignment == PdfPCell.ALIGN_CENTER) {
        this.left = ((position.getRight() - position.getLeft()) / 2 );
    }
    else if (this.alignment == PdfPCell.ALIGN_MIDDLE) {
        this.top = ((position.getTop() - position.getBottom()) / 2 );
    }
    canvas.showTextAligned(this.alignment, this.text, position.getLeft() + this.left, position.getTop() - this.top, this.rotation);
}

Here's what the cell header overflow looks like. In this example it should have the month and year (Mar 2016) displayed.

enter image description here

I'd like to have the cell to be arbitrary height dependent on the actual header text being used. Any ideas on how to solve this?

Community
  • 1
  • 1
JohnP
  • 125
  • 1
  • 5

1 Answers1

2

A cell event is triggered after a cell is drawn. You might already have suspected that much as iText passes a Rectangle object with the position to the cellLayout method. A PdfPCell object is passed, but it is to be used for read-only purposes only. As the position is fixed, you can't use the setFixedHeight() on it.

Looking at the screen shot, I am puzzled: why are you using a cell event to add content that is rotated by 90 degrees? The solution to your problem would be to use the setRotation() method:

PdfPCell cell = new PdfPCell(new Phrase("May 16, 2016"));
cell.setRotation(90);

Now the content will be rotated and the size of the cell will be adapted to the content. Please take a look at the RotatedCell example:

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    PdfPTable table = new PdfPTable(8);
    for (int i = 0; i < 8; i++) {
        PdfPCell cell =
            new PdfPCell(new Phrase(String.format("May %s, 2016", i + 15)));
        cell.setRotation(90);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);
    }
    for(int i = 0; i < 16; i++){
        table.addCell("hi");
    }
    document.add(table);
    document.close();
}

The result looks like this: rotated_cell.pdf

enter image description here

Note that the concepts horizontal and vertical are rotated to. If you want to center the rotated content horizontally, you have to center the vertical alignment for the content and the rotate the aligned content.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • this works perfectly for rotations on 90 degree increments and I will accept this answer. I was handling it with CellEvent due to a requirement for any angle of text but I think I'll see if 90 dgree rotation will work for all requirements. Thanks! – JohnP May 16 '16 at 21:47