0

We have a vehicle "Description" that could be anywhere from a few words to a few paragraphs. It needs to not go beyond a certain height (like 100px or whatever). Perfect solution would allow the text box to auto-grow (i.e. be as small as possible, but be able to grow up to a max height). There doesn't seem to be a way to restrict the height of a Paragraph or Phrase or anything. I have been messing with ColumnText but I can't seem to figure out how to make a ColumnText go into the flow of the document, so the next element after the Description goes below it and not on top of it. I have also seen ct.SetTextMatrix(xPos, yPos), but that still doesn't get me a max height box. Have I just not found what I'm needing yet, or does it not exist in iTextSharp?

John Washam
  • 4,073
  • 4
  • 32
  • 43
  • So you want to cut content off if it overflows? I think you should be able to do that with a `PdfPTable` – Chris Haas Nov 21 '13 at 00:31
  • Do you want the text beyond your maximum hight be cut off? Or do you want a smaller font to be used to squeeze the content into the maximum height? – mkl Nov 21 '13 at 05:10
  • Good questions! My original thought was to have the content be cut off, but it may be nice as well to know if there's a way to use a smaller font to squeeze it. – John Washam Nov 21 '13 at 14:20
  • For the cut-off a table should do as @ChrisHaas suggested. For using smaller fonts I think you need `ColumnText` with simulation runs (to determine which font size works best). You may reserve the space in the document with a single cell and use `ColumnText` in a cell event. I don't usually create content, though, so there may be more elegant ways. – mkl Nov 21 '13 at 15:01
  • Ok, so PdfPTable and PdfPCell are working nicely for setting a specific height, which cuts off the text, if the text overflows the cell/table. But the problem remains that I can only set a **fixed** height. So if the text is only four words, the text box is huge, since the height is fixed. Is there no way to either set a `MaximumHeight` or calculate the height of the `PdfPCell` after the text is in it or something? – John Washam Nov 21 '13 at 16:19
  • 1
    See this for calculating table height before rendering: http://stackoverflow.com/a/7601244/231316 – Chris Haas Nov 22 '13 at 14:02

1 Answers1

2

Thank you so much, @Chris Haas! My solution was eventually found by the link he posted.

First, at the top of the page, we do the table height calculation:

PdfPTable tempTable = new PdfPTable(1);
tempTable.SetTotalWidth(new float[] { 540 }); //540 is width of PageSize.LETTER minus 36*2 for margins
string itemDescription = item.Description;
tempTable.AddCell(itemDescription);
float descriptionTableHeight = CalculatePdfPTableHeight(tempTable);

Then, the code for actually generating the PDF:

using (MemoryStream ms = new MemoryStream())
{
    using (Document document = new Document(PageSize.LETTER))
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, ms))
        {
            document.Open();

            //document properties
            float margin = 36f;
            document.SetMargins(margin, margin, margin, margin);

            document.NewPage();

            //description
            customFont = FontFactory.GetFont("Helvetica", 10);
            Phrase description = new Phrase(itemDescription, customFont);

            table = new PdfPTable(1);
            table.WidthPercentage = 100;
            cell = new PdfPCell(description);
            cell.Border = 0;
            float maxHeight = 98f;
            if (descriptionTableHeight > maxHeight)
                cell.FixedHeight = maxHeight;
            table.AddCell(cell);
            document.Add(table);
        }
    }
}

So since we now have the table's height, we can check to see if it is greater than a maximum value and set the FixedHeight of the cell if so. And since we are able to add the table to the document, it goes in the normal flow of the page.

Thanks to the commenters for the leads!

Community
  • 1
  • 1
John Washam
  • 4,073
  • 4
  • 32
  • 43