10

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.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
yogesh420
  • 115
  • 1
  • 1
  • 3
  • 2
    See Bruno's response here: http://stackoverflow.com/a/12580530/231316 Basically you need to use a `Paragraph` and set the `HorizontalAlignment` on that to `Element.ALIGN_CENTER`. – Chris Haas Aug 13 '13 at 15:46
  • Bruno Lowagie's deleted answer solved my problem, too. Here it is: http://stackoverflow.com/a/18220826/50358. – John Fisher Mar 18 '16 at 22:36

7 Answers7

14

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

Community
  • 1
  • 1
Patrick
  • 3,073
  • 2
  • 22
  • 60
8

try this,

cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
Muneeb Siddiqui
  • 131
  • 1
  • 3
6

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);
Manish Sharma
  • 2,406
  • 2
  • 16
  • 31
  • 2
    It din work... see comments.. i tired.. now i solved my problem by assigning cells phrase property to newly created phrase. – yogesh420 Aug 13 '13 at 19:15
5

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

Alex C
  • 16,624
  • 18
  • 66
  • 98
  • 1
    I did this method and it worked. Although everytime you want to declare something, you sould do what the `RELEVANT CODE STARTS HERE` – WTFZane Mar 22 '17 at 03:08
2

I know this is old question, but the proper sollution is to use cell.HorizontalAlignment = 1;

Here is a nice example

m3div0
  • 1,556
  • 3
  • 17
  • 32
  • 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. – Bruno Lowagie Oct 29 '14 at 08:37
  • 2
    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 – m3div0 Oct 30 '14 at 15:37
2

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

0

It is possible to set alignment for all table cells as example:

table.DefaultCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;

if you need other alignment for some of the table cells you can specify it separately -

Phrase phraseConstant = new Phrase("Name :", font);
PdfPCell cell = new PdfPCell(phraseConstant);
cell.HorizontalAlignment = 0;
table.AddCell(phraseConstant);
Sharunas Bielskis
  • 1,033
  • 1
  • 16
  • 25