ITextSharp: what alignment properties are used in PdfPCell?

When I use cell alignment to make it work:

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

But as soon as 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; 

Cause?

+6
source share
2 answers

You mix text mode with composite mode.

In the first code snippet, you are working in text mode. This means that the contents of the cell are considered only text, and the properties of the cell are respected, while the properties of elements added to the cell are ignored.

In the second code snippet, you are working in complex mode. The cell switches to composite mode the moment you use the AddElement() method. In this case, the properties of the cell are ignored. Instead, element properties are used.

For example: in text mode, the contents of a cell can have only one type of alignment. In complex 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.

+8
source

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); 

Thanks.

+4
source

Source: https://habr.com/ru/post/947759/


All Articles