Does anyone know how to left align a iTextSharp table?
Asked
Active
Viewed 2.9k times
3 Answers
20
You can use the PdfPTable
's HorizontalAlignment
property.
Here's a C# test method you can use to experiment:
private void TestTableCreation() {
using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create)) {
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfPTable table = new PdfPTable(4);
table.WidthPercentage = 50.0f;
// Options: Element.ALIGN_LEFT (or 0), Element.ALIGN_CENTER (1), Element.ALIGN_RIGHT (2).
table.HorizontalAlignment = Element.ALIGN_LEFT;
for (int i = 1; i <= 20; i++) {
PdfPCell cell = new PdfPCell(new Phrase(String.Format("Cell # {0}", i)));
cell.FixedHeight = 30.0f;
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.AddCell(cell);
}
doc.Add(table);
doc.Close();
}
}

Jay Riggs
- 53,046
- 9
- 139
- 151
1
There are two ways of doing it:
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.HorizontalAlignment = 0;

letiagoalves
- 11,224
- 4
- 40
- 66

vijay
- 31
- 4