Reputation: 115
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.
Upvotes: 10
Views: 53024
Reputation: 61
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
Upvotes: 2
Reputation: 1203
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);
Upvotes: 0
Reputation: 2583
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:
Hope it helps
Upvotes: 13
Reputation: 17004
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
Upvotes: 5
Reputation: 131
try this,
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
Upvotes: 8
Reputation: 1586
I know this is old question, but the proper sollution is to use cell.HorizontalAlignment = 1;
Here is a nice example
Upvotes: 2
Reputation: 2426
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);
Upvotes: 6