Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTextSharp: which alignment properties are used in a PdfPCell?

Tags:

c#

itextsharp

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?

like image 238
user2506112 Avatar asked Jun 20 '13 16:06

user2506112


2 Answers

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.

like image 111
Bruno Lowagie Avatar answered Nov 15 '22 01:11

Bruno Lowagie


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.

like image 45
user2506112 Avatar answered Nov 15 '22 01:11

user2506112