1

I have a problem with the vertical alignment in a table. The text is too close to the bottom border:

enter image description here

My code looks like this:

nested = new PdfPTable(3);
nested.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
nested.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
nested.WidthPercentage = 100;
nested.AddCell(new Phrase("blablabla"));
nested.AddCell(new Phrase("blablabla"));
nested.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
nested.AddCell(new Phrase("Stand: " + 
pdfdoc.Add(nested);

Adding or removing the line DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE; doesn't have any effect.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Cezary88
  • 23
  • 7
  • i mean the vertical alignment, the text is nearly written down on the borderline not in the middle. I will replace my code with Align center thx! but there is no option for table with alignment. and nested.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE; doesn't work. – Cezary88 Jun 26 '16 at 10:11
  • I've updated your question. Now it can be answered. – Bruno Lowagie Jun 26 '16 at 10:20
  • sorry guys, that's my first post. I will do it better next time. I am using itextsharp 5.5.9. – Cezary88 Jun 26 '16 at 10:23
  • Check this one? - [SO Question](http://stackoverflow.com/questions/4096490/itextsharp-pdfpcell-verticalalignment-and-pdfpcell-horizontalalignment) – pijemcolu Jun 26 '16 at 10:26
  • @pijemcolu I didn't know that answer, but the (accepted) top answer doesn't solve the problem. You need [this answer](http://stackoverflow.com/a/34605724/1622493) which I just up-voted because that's more or less the solution to the problem. That answer is far from complete, which is the reason why I didn't mark this question as a "duplicate". – Bruno Lowagie Jun 26 '16 at 10:33
  • @BrunoLowagie - Fair enough, `cell.UseAscender = true;` who doesn't love oneliners. – pijemcolu Jun 26 '16 at 10:35

1 Answers1

0

You are creating PdfCell object with only one line of text. The height of the cell will be determined based on that line of text. The text will be aligned in the middle automatically. That explains why adding or removing DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE; has no effect: as far as iText is concerned, the text is already aligned in the middle.

You claim that this isn't true because it's your perception that the base line of the text is too close to the bottom. I understand that claim, but if your read my answer to the question How does a PdfPCell's height relate to the font size? you should understand which factors create that perception:

  1. The leading: the default font size is 12 pt; the default leading is 18 pt. A leading of 18 pt is kind of high and results in extra space above the base line. If you reduce the leading, you'll see that there's less space at the top.
  2. Different fonts have different ascender and descender values; the way you add the cells, iText won't take those values into account.

My suggestion: tell iText to use the ascender and descender of the font you're using:

nested.DefaultCell.UseAscender = true;
nested.DefaultCell.UseDescender = true;

You'll notice that the position of the text will already be much better. If it's not better, you may want to add some padding. All of this is, of course, explained in the official documentation where you'll find an example called Spacing.cs. Try this example and you'll see how the position of the content changes by tweaking values such as UseAscender, UseDescender, Padding, and so on.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165