bladerunner
bladerunner

Reputation: 889

itextsharp pdfpcell vertical align problem

How would I make two cells aligned vertically together. Currently, the second cell renders underneath the first cell. The first cell is an image and the second one is a text. Here is my code.

    private Document pdoc;
    Font font99 = FontFactory.GetFont("HELVETICA", 60);
    PdfPTable pdfRatingTable = new PdfPTable(2);
    PdfPCell pRatCell = null;
    pdfRatingTable.WidthPercentage = 100;
    pdfRatingTable.SetWidths(new int[] { 75, 25 });

    hImage = iTextSharp.text.Image.GetInstance(MapPath("~/Images/fyler3_Rating.jpg"));
    NewWidth = 338;
    MaxHeight = 18;
    if (hImage.Width <= NewWidth)
    {
        NewWidth = hImage.Width;
    }
    NewHeight = hImage.Height * NewWidth / hImage.Width;
    if (NewHeight > MaxHeight)
    {
        NewWidth = hImage.Width * MaxHeight / hImage.Height;
        NewHeight = MaxHeight;
    }

    ratio = hImage.Width / hImage.Height;
    hImage.ScaleAbsolute(NewWidth, NewHeight);
    pRatCell = new PdfPCell(hImage);
    pRatCell.Border = 0;
    pRatCell.PaddingLeft = 20f;
    pRatCell.HorizontalAlignment = Element.ALIGN_LEFT;
    pdfRatingTable.AddCell(pRatCell);

    pRatCell = new PdfPCell(new Phrase(new Chunk("405", font99)));
    pRatCell.HorizontalAlignment = Element.ALIGN_LEFT;
    pRatCell.Border = 0;
    pRatCell2.VerticalAlignment = Element.ALIGN_TOP;
    pdfRatingTable.AddCell(pRatCell);
    pdoc.Add(pdfRatingTable);

Upvotes: 4

Views: 7695

Answers (1)

Mark Storer
Mark Storer

Reputation: 15868

It looks like your image is wider than the max width of a cell in your table (plus a rather generous padding), so the next cell appears in the next row.

I suggest trying this with a much smaller image (or the same image at a smaller scale) to see if I'm right.


Or by "underneath" do you mean that they're both in the same row, but the text appears at the bottom of the cell while the image is in the middle with a 20 point pad around it so the text is entirely below the image?

IIRC, a Paragraph will take up the entire cell but a Chunk will obey the cells vertical and horizontal alignment settings. See the comments in my answer here.

Upvotes: 1

Related Questions