Itextsharp pdfpcell vertical alignment problem

How would I make two cells aligned vertically together. Currently, the second cell is displayed below the first cell. The first cell is the image, and the second is the 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); 
+4
source share
1 answer

It looks like your image is wider than the maximum cell width in your table (plus a rather generous spacer), so the next cell will appear on the next line.

I suggest trying this with a much smaller image (or the same image on a smaller scale) to make sure I'm right.


Or by β€œbottom” do you mean that they are both on the same line, but the text appears at the bottom of the cell and the image is in the middle with a 20-point area around it, so the text is completely below the image?

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

+1
source

Source: https://habr.com/ru/post/1338283/


All Articles