Vertical alignment not working in composite mode

六眼飞鱼酱① 提交于 2019-12-23 11:56:44

问题


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;

回答1:


Ah. You need to set the PdfCell's vertical alignment, not the paragraph.

PdfPCell c = new PdfPCell();
c.setVerticalAlignment(Element.ALIGN_MIDDLE);
...



回答2:


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



来源:https://stackoverflow.com/questions/4403136/vertical-alignment-not-working-in-composite-mode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!