Problem foramatting table cell

Hi,

I want to format a table with horizontal center alignment for header row, and left alignment for content rows. But it only work for on the first cell on header row, code shown below:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartTable();

BorderCollection borders = builder.CellFormat.Borders;
borders.LineStyle = LineStyle.Single;
borders.LineWidth = 0.5;
borders.Color = System.Drawing.Color.Gray;

// Insert a table row header
Cell cell = builder.InsertCell();

builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; string s = "This is row 1 cell 1";

//cell.CellFormat.Width = s.Length;
cell.CellFormat.Width = 300;

builder.Write(s);

builder.InsertCell();

builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;  //not working

builder.Write("This is row 1 cell 2");

builder.EndRow();

//insert table content row
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;

// Insert a cell
cell = builder.InsertCell();
builder.Writeln("This is row 2 cell 1");

// Insert a cell
builder.InsertCell();
builder.Writeln("This is row 2 cell 2");
builder.EndRow();

builder.EndTable();

// Save the document.
doc.Save(MapPath("Inserting a Table.doc"));

Hello,
Thank you for your inquiry.
Please try this code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
BorderCollection borders = builder.CellFormat.Borders;
borders.LineStyle = LineStyle.Single;
borders.LineWidth = 0.5;
borders.Color = Color.Gray;
// Insert a table row header
Cell cell = builder.InsertCell();
cell.CellFormat.Width = 300;
builder.Write("Top Left");
builder.InsertCell();
builder.Write("Top Center");
builder.InsertCell();
builder.Write("Top Right");
builder.EndRow();
builder.InsertCell();
builder.Write("Center Left");
builder.InsertCell();
builder.Write("Center Center");
builder.InsertCell();
builder.Write("Center Right");
builder.EndRow();
builder.InsertCell();
builder.Write("Bottom Left");
builder.InsertCell();
builder.Write("Bottom Center");
builder.InsertCell();
builder.Write("Bottom Right");
builder.EndRow();
builder.EndTable();
// Formating table cells after table create.
table.Rows[0].Cells[0].CellFormat.VerticalAlignment = CellVerticalAlignment.Top;
table.Rows[0].Cells[0].FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
table.Rows[0].Cells[1].CellFormat.VerticalAlignment = CellVerticalAlignment.Top;
table.Rows[0].Cells[1].FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
table.Rows[0].Cells[2].CellFormat.VerticalAlignment = CellVerticalAlignment.Top;
table.Rows[0].Cells[2].FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Right;
table.Rows[1].Cells[0].CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
table.Rows[1].Cells[0].FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
table.Rows[1].Cells[1].CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
table.Rows[1].Cells[1].FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
table.Rows[1].Cells[2].CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
table.Rows[1].Cells[2].FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Right;
table.Rows[2].Cells[0].CellFormat.VerticalAlignment = CellVerticalAlignment.Bottom;
table.Rows[2].Cells[0].FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Left;
table.Rows[2].Cells[1].CellFormat.VerticalAlignment = CellVerticalAlignment.Bottom;
table.Rows[2].Cells[1].FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
table.Rows[2].Cells[2].CellFormat.VerticalAlignment = CellVerticalAlignment.Bottom;
table.Rows[2].Cells[2].FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Right;

Hope this helps.

Hi Steve,
Thanks for your request. The problem occurs because in your code you reset alignment before starting a new cell. I modified your code and now it works as expected:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartTable();
BorderCollection borders = builder.CellFormat.Borders;
borders.LineStyle = LineStyle.Single;
borders.LineWidth = 0.5;
borders.Color = System.Drawing.Color.Gray;
// Insert a table row header
Cell cell = builder.InsertCell();
cell.CellFormat.Width = 300;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
string s = "This is row 1 cell 1";
builder.Write(s);
builder.InsertCell();
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //not working
builder.Write("This is row 1 cell 2");
builder.EndRow();
// Insert a cell
builder.InsertCell();
// insert table content row
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.Writeln("This is row 2 cell 1");
// Insert a cell
builder.InsertCell();
builder.Writeln("This is row 2 cell 2");
builder.EndRow();
builder.EndTable();
// Save the document.
doc.Save(@"Test001\out.doc");

I highlighted part of code I moved after InsertCell.
Best regards,

Thanks for the helpful advice from both of you!