iTextSharp: which alignment properties are used in a PdfPCell?

北城以北 提交于 2019-12-19 17:48:10

问题


When I use the alignment of the cell so it works:

PdfPCell cell1 = new PdfPCell(new Phrase("Text" , Font));
cell1.HorizontalAlignment = 2;

But once the alignment does not work:

PdfPCell cell1 = new PdfPCell();
cell1.AddElement(new Phrase("Text 1", Font));
cell1.AddElement(new Phrase("Text 2", Font));
cell1.HorizontalAlignment = 2;

The reason?


回答1:


You are confusing text mode with composite mode.

In the first code snippet, you work in text mode. This means that the content of the cell is considered to be text only and the properties of the cell are respected, whereas the properties of the elements added to the cell are ignored.

In the second code snippet, you work in composite mode. A cell switches to composite mode the moment you use the AddElement() method. In this case, the properties of the cell are ignored. Instead the properties of the elements is used.

For instance: in text mode, the content of the cell can only have one type of alignment. In composite mode, you can have a paragraph that is left aligned, a paragraph that is centered, and a paragraph that is right aligned, all in the same cell.




回答2:


Now yes, it worked.

PdfPCell cell1 = new PdfPCell();
Paragraph p1 = new Paragraph("Text 1", Font);
p1.Alignment = Element.ALIGN_RIGHT;
Paragraph p2 = new Paragraph("Text 2", Font);
p2.Alignment = Element.ALIGN_RIGHT;

cell1.AddElement(p1);
cell1.AddElement(p2);

Thank you.



来源:https://stackoverflow.com/questions/17219319/itextsharp-which-alignment-properties-are-used-in-a-pdfpcell

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