Horizontal text alignment in a PdfPCell

47,689

Solution 1

I tried all the above solutions and none worked. Then I tried this and that led me to the correct solution

PdfPCell c2 = new PdfPCell(new iTextSharp.text.Phrase("Text")) { HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER };
table.AddCell(c2);

I know it's not required from the OP but you can also have several words centered if separated with \n. Additionally you can also vertically center with Element.ALIGN_MIDDLE

With all that the code is:

PdfPCell c2 = new PdfPCell(new iTextSharp.text.Phrase("AAAAAAAAA\nBBBBBB")) { HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER, VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE };
table.AddCell(c2);

and the result:

enter image description here

Hope it helps

Solution 2

try this,

cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right

Solution 3

try this,

cell = New PdfPCell();
p = New Phrase("value");
cell.AddElement(p);
cell.HorizontalAlignment = Element.ALIGN_CENTER; //Tried with Element.Align_Center Also. Tried Adding this line before adding element also. 
table.AddCell(cell);

Solution 4

Per the comments, the correct answer (which I have just tested locally) is to create a paragraph element and add the alignment directive to the paragraph.

A working block of code which demonstrates this is:

        Font qFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLDOBLIQUE, 10);
        List<float> widths = new List<float>();
        for(int i = 0; i < qs.Selected.Items.Count; i++) {
            widths.Add((pageRectangle.Width - 250)/ qs.Selected.Items.Count);
        }

        PdfPTable table = new PdfPTable(qs.Selected.Items.Count);
        table.HorizontalAlignment = Element.ALIGN_CENTER;
        table.SetTotalWidth(widths.ToArray());

        foreach(System.Web.UI.WebControls.ListItem answer in qs.Selected.Items) {
            cell = new PdfPCell();
            cell.Border = Rectangle.NO_BORDER;
/******************** RELEVANT CODE STARTS HERE ***************************/
            Paragraph p = new Paragraph(answer.Text, aFont);
            p.Alignment = Element.ALIGN_CENTER;
            cell.AddElement(p);
            table.AddCell(cell);
/******************** RELEVANT CODE  ENDS  HERE ***************************/
        }

Credit for this should go to the user Bruno Lowagie but there seems to be some odd drama going on, complaints of downvotes on the correct answer and subsequent deletion of the same.

I'll eat the downvotes if it gets a clear answer posted in the right place at the right time.

Some things are more important than internet-points. <3

Solution 5

Finally

Change this :

 cell = New PdfPCell();
 p = New Phrase("value");
 cell.AddElement(p);
 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; 
 table.AddCell(cell);

with this :

PdfPCell cell = new PdfPCell(new Phrase("value"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;

Looks similar but different in result I don't know why

Share:
47,689
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I am using this code to align horizontally.

    cell = New PdfPCell();
    p = New Phrase("value");
    cell.AddElement(p);
    cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; //Tried with Element.Align_Center Also. Tried Adding this line before adding element also. 
    table.AddCell(cell);
    

    It's not working.

    I am creating a table with 5 columns in it and adding cells dynamically in runtime in a for loop with above code. I want all cells content to be centered.

  • Admin
    Admin almost 11 years
    It din work... see comments.. i tired.. now i solved my problem by assigning cells phrase property to newly created phrase.
  • Bruno Lowagie
    Bruno Lowagie over 9 years
    Somebody is downvoting correct answers. In your case, I understand, because you use 1 instead of Element.ALIGN_CENTER and you refer to an external example without showing a more substantial code snippet, but I don't understand why my answer is downvoted, nor why it isn't accepted.
  • m3div0
    m3div0 over 9 years
    I don't see any point of your comment, your explanation of why someone may downvote my answer is way unrelated to the question. Moreover I think that the purpose of an answer is to provide working sollution to the problem, which I did. And I don't realy care about the score or votes I am actually trying to help
  • WTFZane
    WTFZane about 7 years
    I did this method and it worked. Although everytime you want to declare something, you sould do what the RELEVANT CODE STARTS HERE
  • karique
    karique over 3 years
    initialize the cell with the Phrase an then set the alignment did the trick +1