Center content of a cell itextsharp c #

1

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 (file capture): The content of the cell where the text "HABERES" is for example, I need to be aligned to the center, but it is aligned to the left.

    
asked by Bastian Salazar 27.07.2017 в 18:39
source

1 answer

2

I got the answer, thanks to Bruno Lowagie who is one of the creators of the itext documentation. There are 2 ways to organize the content in these cases:  First organizing it at the cell level, which in this case does not work, since the cell uses the general alignment and eliminates the individualized alignment, therefore it is passed to the second mode.  Second, organizing it in a general or text level, for that in the Phrase class I have not found a definition to modify the property of horizontal alignment, so you can change it to the Paragraph class.

Replacing the code, with the following:

            /*datos del LA LIQUIDACIÓN*/
            //1° linea
            paragraph.Clear();//ahora utilizo la clase Paragraph 
            paragraph.Font = new Font(FontFactory.GetFont("Arial", 10, Font.BOLD));
            paragraph.Alignment = Element.ALIGN_CENTER;
            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();

You would get this result:

    
answered by 31.07.2017 / 16:25
source