Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical alignment not working in composite mode

Tags:

itext

The following should work according to the documentation but it's not working for me. Am I missing something?

PdfPTable rs1 = new PdfPTable(1);
PdfPCell c = new PdfPCell();

Paragraph p = new Paragraph("some text to align");

c.AddElement(p);
c.VerticalAlignment = Element.ALIGN_MIDDLE;

rs1.AddCell(c);
rs1.AddCell("more text");

return rs1;
like image 967
Rod Avatar asked Feb 26 '23 15:02

Rod


1 Answers

The thing with iTextSharp is that it will behave differently depending on which constructor you use. This won't align the text:

PdfPCell c = new PdfPCell();
c.Add(new Phrase("Whatever"));
c.setHorizontalAlignment(Element.ALIGN_CENTER);

But this will:

PdfPCell c = new PdfPCell(new Phrase("Whatever"));
c.setHorizontalAlignment(Element.ALIGN_CENTER);

I don't know exactly why this is, it's got something to do with the cell being in 'text mode' if you add the phrase in the constructor versus 'composite mode' if you add it later (in which case each object is supposed to look after it's own alignment).

Some more info (in Java, but still applies) http://tutorials.jenkov.com/java-itext/table.html#cell-modes

like image 116
Mick Byrne Avatar answered Mar 01 '23 09:03

Mick Byrne