Bastian Salazar
Bastian Salazar

Reputation: 61

Center content of a itextsharp cell C#

I am working on a salary settlement format in C #, with itexsharp to generate the pdf file.

But I can not control the alignment of the contents of the PdfPTable / PdfPCell cells.

I have this code:

        /*datos del LA LIQUIDACIÓN*/
        //1° linea
        phrase.Font = new Font(FontFactory.GetFont("Arial", 10, Font.BOLD));
        phrase.Add("H A B E R E S");
        PdfPCell cell2 = new PdfPCell();
        cell2.Border = Rectangle.NO_BORDER;
        cell2.PaddingTop = -7;
        cell2.AddElement(phrase);
        cell2.Colspan = 3;
        cell2.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
        table2.AddCell(cell2);                
        phrase.Clear();

But it gives me this result:

enter image description here

The contents of the cell where it is for example the text "HABERES", I need that it is aligned to the center, but it is aligned to the left.

I have read this post in the past "Right aligning text in PdfPCell" , and I have done everything that is eaten, except to take a "different road", what nevers is mentioned ... what can I do then?

Upvotes: 0

Views: 5347

Answers (2)

Bastian Salazar
Bastian Salazar

Reputation: 61

This has worked for me, changing the composition mode, since I have not been able to change the alignment of the Phrase class, if it has worked with the Paragraph class.:

            /*datos del LA LIQUIDACIÓN*/
            //1° linea
            paragraph.Clear();
            paragraph.Font = new Font(FontFactory.GetFont("Arial", 10, Font.BOLD));
            paragraph.Alignment = Element.ALIGN_CENTER;//here is the change
            paragraph.Add("H A B E R E S");
            PdfPCell cell2 = new PdfPCell();
            cell2.Border = Rectangle.NO_BORDER;
            cell2.PaddingTop = -7;
            cell2.AddElement(paragraph);
            cell2.Colspan = 3;
            table2.AddCell(cell2);
            paragraph.Clear();

This Result:

enter image description here

Upvotes: 3

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

As documented on many places, there is a difference between text mode (where you set the alignment on the cell level: cell2.HorizontalAlignment = PdfPCell.ALIGN_CENTER;) and composite mode (in which case the alignment of the cell is ignored).

You are using composite mode, hence the line cell2.HorizontalAlignment = PdfPCell.ALIGN_CENTER; is ignored. You should either switch to text mode, or set the alignment at the level of the phrase.

The difference between text mode and composite mode exists regardless of the programming language you use. The answer remains.

You have a Phrase object:

Font font = FontFactory.GetFont("Arial", 10, Font.BOLD);
Phrase phrase = new Phrase("H A B E R E S", font);

Note that I changed your code, because you are creating a Font and a Phrase in a really awkward way.

You create a Cell to which you add the Phrase:

    PdfPCell cell2 = new PdfPCell();
    cell2.AddElement(phrase);
    table2.addCell(cell2);

As you are using the AddElement() method, you are working in composite mode. That is also awkward, because there is no reason why you'd need composite mode.

As documented, the following line is ignored:

 cell2.HorizontalAlignment = PdfPCell.ALIGN_CENTER;

That line only works in text mode.

To fix the problem, you have two options:

Option #1: go to text mode

Font font = FontFactory.GetFont("Arial", 10, Font.BOLD);
Phrase phrase = new Phrase("H A B E R E S", font);
PdfPCell cell2 = new PdfPCell(phrase);
cell2.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
table2.addCell(cell2);

Option #2: set the alignment at the element level, not the cell level

Font font = FontFactory.GetFont("Arial", 10, Font.BOLD);
Paragraph p = new Paragraph("H A B E R E S", font);
p.Alignment = Element.ALIGN_CENTER;
PdfPCell cell2 = new PdfPCell();
cell2.AddElement(p);
table2.addCell(cell2);

Note that I used a Paragraph in option 2. It doesn't really make sense to use a Phrase in composite mode.

SUMMARIZED: it really matters at which level you set the alignment.

  • In text mode, you set the alignment at the level of the cell, NOT at the level of its contents.
  • In composite mode, you set the alignment at the level of the contents, NOT at the level of the cell.

It's as simple as that.

Upvotes: 0

Related Questions