Unable to vertically align text in table cell with itextsharp
15,456
Solution 1
You have a complex example that appears to be using nested tables and extension methods. As Alexis pointed out, the VerticalAlignment
is the correct property to use. Below is a full working example of this. I recommend getting rid of your extension method for now and just starting with this example.
//Our test file to output
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
//Standard PDF setup, nothing special here
using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
//Create our outer table with two columns
var outerTable = new PdfPTable(2);
//Create our inner table with just a single column
var innerTable = new PdfPTable(1);
//Add a middle-align cell to the new table
var innerTableCell = new PdfPCell(new Phrase("Inner"));
innerTableCell.VerticalAlignment = Element.ALIGN_MIDDLE;
innerTable.AddCell(innerTableCell);
//Add the inner table to the outer table
outerTable.AddCell(innerTable);
//Create and add a vertically longer second cell to the outer table
var outerTableCell = new PdfPCell(new Phrase("Hello\nWorld\nHello\nWorld"));
outerTable.AddCell(outerTableCell);
//Add the table to the document
doc.Add(outerTable);
doc.Close();
}
}
}
This code produces this PDF:
Solution 2
Use
cell.VerticalAlignment = Element.ALIGN_MIDDLE; // or ALIGN_TOP or ALIGN_BOTTOM
Also, you can set a default vertical alignment for all cells by setting
kitosKalbosTable.DefaultCell.VerticalAlignment
Related videos on Youtube
Author by
caked bake
Updated on June 04, 2022Comments
-
caked bake almost 2 years
I can not figure out how to vertically align text in table cell. horizontal alignment is ok. I use itextsharp to generate pdf. Alignment should be applied to cells in table kitosKalbosTable. Any help would be appreciated. here's my code:
var table = new PdfPTable(new float[] { 36, 1, 63 }); table.WidthPercentage = 100.0f; table.HorizontalAlignment = Element.ALIGN_LEFT; table.DefaultCell.Border = Rectangle.NO_BORDER; table.SplitRows = false; ......... PdfPTable kitosKalbosTable = new PdfPTable(new float[] {10, 30}); kitosKalbosTable.TotalWidth = 40f; kitosKalbosTable.SplitRows = false; kitosKalbosTable.AddCell("Kalba", FontType.SmallTimes, vAligment: Element.ALIGN_MIDDLE, hAligment: Element.ALIGN_CENTER); .......... table.AddCell(kitosKalbosTable); //method in other file public static PdfPCell CreateCell( string text, FontType? fontType = FontType.RegularTimes, int? rotation = null, int? colspan = null, int? rowspan = null, int? hAligment = null, int? vAligment = null, int? height = null, int? border = null, int[] disableBorders = null, int? paddinLeft = null, int? paddingRight = null, bool? splitLate = null) { var cell = new PdfPCell(); ............ if (vAligment.HasValue) { cell.VerticalAlignment = vAligment.Value; } return cell; }
-
Adam Jones over 9 yearsI've also tried both of these options and neither appear to do anything.
-
B. Clay Shannon-B. Crow Raven about 9 yearsYes, cellBla.VerticalAlignment = Element.ALIGN_MIDDLE does not work for me, either; nor does ALIGN_CENTER.
-
Alex C over 7 yearsThanks for that
DefaultCell
note - cleaned up my code quite a bit using that. -
Sam Saarian over 6 yearsThe top margin still will show some value even after both top and bottom margins you set to zero. This is the case for a single line phrase though. however you can set the cell absolute height and make the bottom margin "close" to top margin height so, middle alignment can be perfectly adjusted. If you carefully look at the Chris Haas example, you will notice that the first column text is not perfectly middled (the top has an extra height). It will be more obvious if you change the alignment to top or bottom.