Reputation: 233
I am new to iTextSharp. In the below code setting VerticalAlignment for PdfPCell (I think in text mode) does Not do anything, text is still aligned to bottom of cell. According to multiple online examples, it should work, but does Not. Setting HorizontalAlignment works. Please help. Thank you
table = new PdfPTable(5);
Document document = new Document(PageSize.A4);
document.Open();
PdfPTable headerTable = new PdfPTable(3);
float[] headerWidths = { 2f, 5f, 2f };
headerTable.SetWidths(headerWidths);
myFont = FontFactory.GetFont("Arial", 8, Font.NORMAL);
PdfPCell myCell = setupHeaderCell("My Text", myFont);
// ADD headerTable to MainTable
// Creates a PdfPCell that accepts the headerTable as a parameter and then adds that cell to the main PdfPTable.
PdfPCell cellHeader = new PdfPCell(headerTable);
cellHeader.Border = PdfPCell.NO_BORDER;
// Sets the column span of the header cell to dataColumNb.
cellHeader.Colspan = 5;
// Adds the above header cell to the table.
table.AddCell(cellHeader);
................
}
private PdfPCell setupHeaderCell(string lineText, Font myFont)
{
var cl = new PdfPCell(new Phrase(lineText, myFont);
cl.VerticalAlignment = Element.ALIGN_MIDDLE; // does Not change vertical alignment
return cl;
}
Upvotes: 0
Views: 3255
Reputation: 55417
You've got nested tables so I'm not exactly sure what you're trying to do. The VerticalAlignment
is relative to the contents of the cell, not the cell itself, but I'm not sure if that's your problem. While troubleshooting this we need to simply things by removing the complexities and finding the least complex version that still produces the issue. I've tested the below code against iTextSharp 5.4.4 and it correctly vertically aligns the left-most column. Start with this, slowly add your logic and possible include some drawings and/or screenshots of what you expect and get.
var t2 = new PdfPTable(3);
float[] headerWidths = { 2f, 5f, 2f };
t2.SetWidths(headerWidths);
var cl = new PdfPCell(new Phrase("Test"));
cl.VerticalAlignment = Element.ALIGN_MIDDLE;
t2.AddCell(cl);
t2.AddCell("This\nIs\nMore\nText");
t2.AddCell("This\nIs\nMore\nText");
Upvotes: 2