Center a Table not working

Hi, I’m simply trying to center a table on my document using documentbuilder. I tried using the paragraphformat.alignment to center the table, but the table still ends up on the left. Any ideas?

Here’s my code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.CurrentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.StartTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.Write("This is row 1 cell 1");
// Insert a cell
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();

Hi
Thanks for your request. You should use the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.Write("This is row 1 cell 1");
// Insert a cell
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();
builder.EndTable();
// Set table alignment.
table.Alignment = TableAlignment.Center;
doc.Save(@"Test001\out.doc");
Or the following code if you are using old version of Aspose.Words:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.RowFormat.Alignment = RowAlignment.Center;
builder.StartTable();
// Insert a cell
builder.InsertCell();
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.Write("This is row 1 cell 1");
// Insert a cell
builder.InsertCell();
builder.Write("This is row 1 cell 2");
builder.EndRow();
builder.EndTable();
doc.Save(@"Test001\out.doc");

Best regards,

Thank you.

If you don’t mind, just one quick more question - is there a way for me to ensure a table does not drag over onto a second page? In other words, only one table to a page. Suppose the table is drawn too close to the bottom of a page that it will be drawn onto the second page … is there a way to grab the entire table and just put it onto the second page instead of half of it being on one page and half being on the next page?
Thanks so much.

Hi
Thanks for your request. I think, it would be enough to use KeepWithNext and AllowBreakAcrosPages options to achieve what you need. Yu should set this option for each paragraph in your table except the paragraphs in the last row of the table. Also, you should set AllowBreakAcrosPages to false for each row. In this case if there is not enough space on the page for the table, whole table will be moved to the next page.
Hope this helps.
Best regards,

Hi Sunil.

Thanks for your inquiry.

Additionally, the following article may be useful to you: https://docs.aspose.com/words/net/working-with-columns-and-rows/

If we can help you with anything else, please feel free to ask.
Thanks,

Thanks - but now that I’ve upgraded to the newest version of Words, things are not working right. For instance, it seems “builder.CellFormat.Width” no longer does anything. My cells are not the correct width - has something changed?

Document doc = new Document(path);
DocumentBuilder builder = new DocumentBuilder(doc);
Aspose.Words.Tables.Table myt; 
myt = builder.StartTable();
builder.InsertCell();
builder.CellFormat.Width = 30; //doesn’t work anymore - does nothing to set width = 30
builder.EndRow();
builder.EndTable();

Ok, it seems to set cell width this statement must be used:

builder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPercent(75);

As for stopping tables from being split across pages, oddly enough I used the code that was given, but used “KeepWithNext = true” for all rows, including the last row, in order to make it work. When I Used the code as given, it didn’t stop the table from splitting across pages. I removed line 3 from the following code and it worked just fine. Thanks.

foreach (Cell cell in table.GetChildNodes(NodeType.Cell, true))
    foreach (Paragraph para in cell.Paragraphs)
        if (!(cell.ParentRow.IsLastRow && para.IsEndOfCell))
            para.ParagraphFormat.KeepWithNext = true;

Hi there,

Yes, there were a few changes to the public API of tables in the latest versions of Aspose.Words. For full details, the following article will help you to fix any issues you are having with your table code:
https://docs.aspose.com/words/net/

In your case you need to call “Table.Autofit” with the FixedColumnWidths setting to restore the same behaviour as in previous versions.

Thanks,