0

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?

  • One of the reasons why we have rewritten iText from scratch with iText 7, is that fact that **many developers don't read the documentation**. As a result, they don't understand the difference between *text mode* and *composite mode*. iText 7 is much more fool-proof, which leads to the quesiton: why are you still using iText 5 instead of iText 7? – Bruno Lowagie Jul 29 '17 at 12:51
  • Dear Bruno Lowagie, its documentation is directed to java, itext and I suppose it is completely correct. But as you can see, I'm working on C # with itextsharp. I have used, to build my project, code I have found in various examples. Because itextsharp documentation is quite skewed, can you help me with this? – Bastian Salazar Jul 31 '17 at 13:16
  • There are some simple rules that every self-respecting C# developer can use: when a Java method starts with a lower case; make it an upper case. When a Java method starts with get or set; remove get or set, and use the properties approach. Please read the other answer, and apply what I say in that other answer. (To be continued...) – Bruno Lowagie Jul 31 '17 at 13:23
  • The other answer is about the 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`. Is there something you don't understand about this answer? – Bruno Lowagie Jul 31 '17 at 13:25
  • Thank you! I have changed the text mode, since I have not been able to change the alignment of the Phrase class, if it has worked with the Paragraph class. I will post the answer, please give a vote. Again thank you very much.. – Bastian Salazar Jul 31 '17 at 14:02
  • Wel, I have posted an answer that works, so no need to post your own answer. Just up-vote (and accept) mine. – Bruno Lowagie Jul 31 '17 at 14:04

2 Answers2

3

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

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
0

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165