Format table

Hello,

I am sorta new to Aspose.words and had couple small problems with the table formatting. What I would like to do are 1) make the text in each cell align to center. 2) expand the table so it will fit the page width, rather than squeezing in the middle of the page.

My code is listed below and sample output is attached. Thank you for your help.

300 builder.RowFormat.Alignment = RowAlignment.Center;

301 builder.RowFormat.AllowAutoFit = true;

304

305

306

307 //builder.Font.Bold = true;

308 builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.SteelBlue;

309 builder.CellFormat.Borders.Color = System.Drawing.Color.White;

310 //builder.CellFormat.FitText = true;

311 builder.CellFormat.WrapText = false;

312 //builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;

313 builder.CellFormat.Borders.LineWidth = 2;

314 builder.CellFormat.Orientation = TextOrientation.Horizontal;

315 builder.Font.Color = System.Drawing.Color.White;

316

317 builder.InsertCell();

318 builder.Write("Reference Number");

319 builder.InsertCell();

320 builder.Write("Product");

321 builder.InsertCell();

322 builder.Write("Expected OCI Reclass");

323 builder.InsertCell();

324 builder.Write("Reclass Start Date");

325 builder.InsertCell();

326 builder.Write("Reclass End Date");

327 builder.EndRow();

328

329 builder.Font.Bold = false;

330 builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.MintCream;

331 builder.Font.Color = System.Drawing.Color.Black;

332

333 foreach (DataRow ociRow in dsOCI.Tables["OCIAmount"].Rows)

334 {

335 builder.InsertCell();

336 builder.Write(ociRow["tr_reference_nbr"].ToString());

337 builder.InsertCell();

338 builder.Write(ociRow["tq_dd_product"].ToString());

339 builder.InsertCell();

340 if (IsNumeric(ociRow["oci_reclass"]) == true)

341 {

342 builder.Write(String.Format("{0:#,##0;(#,##0)}", Decimal.Parse(ociRow["oci_reclass"].ToString())));

343 totalOCI = totalOCI + Decimal.Parse(ociRow["oci_reclass"].ToString());

344 }

345 else

346 {

347 builder.Write("N/A");

348 }

349 builder.InsertCell();

350 builder.Write(String.Format("{0: dd-MMM-yy}", DateTime.Parse(ociRow["FiscalYrEffTestStartDt"].ToString())));

351 builder.InsertCell();

352 builder.Write(String.Format("{0: dd-MMM-yy}", DateTime.Parse(ociRow["FiscalYrEffTestEndDt"].ToString())));

353 builder.EndRow();

354

355 }

1. To make text in the cell be center-aligned you need to set paragraph alignment to centered:

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

2. To make table fit the table width the best approach is to set cell width explicitly as fractional part of the page width:

// Calculate table width as total page width with left and right margins subtracted.

PageSetup pageSetup = builder.CurrentSection.PageSetup;

double tableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;

builder.InsertCell();

// Set first cell to 1/3 of the page width.

builder.CellFormat.Width = tableWidth / 3;

builder.InsertCell();

// Set second cell to 2/3 of the page width.

builder.CellFormat.Width = (tableWidth * 2) / 3;

Hope this helps,

Hello,

Thank you for the help. The first part worked. :-) I still have problem with the 2) part.

I see your point by finding page width and set the cell to fixed length. However the column width would be dynamic for the database. Is there a way to set the table expand to page width and let the individual column do the autofit/auto set the column width?

Thank you

Yes, there is another way using table autofit. Consider the following code:

builder.RowFormat.AllowAutoFit = true;

builder.InsertCell();

builder.Write("Text1");

builder.CellFormat.Width = 500;

builder.InsertCell();

builder.Write("Text2");

builder.CellFormat.Width = 1000;

Cell width are intentionally set to big values. Autofit will take care to reduce them proportionally to fit the page width.

Hope this helps,