Set Alignment of existing table

Hi aspose team.

I’m using aspose.words 6.0.0 and I want to Align an existing table to center, which is in the header part

of the document. I import the header node from a template file, but in destination the table alignment

become left, and I want to set it to center. I read your solutions for using Builder.RowFormat.Alignment,

but I think it’s for inserting something, not configuring an existing one.

Thanks for your attention

Hi

Thanks for your inquiry. To center the table you should loop over all rows in this table and specify alignment of each row. For instance, see the following cod:

// Open document.
Document doc = new Document(@"Test001\in.doc");
// Get table that should be centered. for example the first one.
Table table = doc.FirstSection.Body.Tables[0];
// Loop over all rows in table and set alignment.
foreach(Row row in table.Rows)
row.RowFormat.Alignment = RowAlignment.Center;
// Save result.
doc.Save(@"Test001\out.doc");

Hope this helps. Please let me know if you need more assistance, I will be glad to help you.
Best regards,

Thanks a lot alexy,For your useful and soon reply. It worked correctly.

And could you tell me what should I do if I want to set the alignment of table cells to center.

I want to know the difference, because it seems that this code will affect on table rows’ content, but
it just aligns the table.

Hi

Thanks for your inquiry. If you need to center content in the table, you need to specify alignment of paragraphs inside the table. For instance, see the following code:

// Open document.
Document doc = new Document(@"Test001\in.doc");
// Get table. for example the first one.
Table table = doc.FirstSection.Body.Tables[0];
// Loop over all paragraphs in the table and cetrer them.
Node[] paragraphs = table.GetChildNodes(NodeType.Paragraph, true).ToArray();
foreach(Paragraph par in paragraphs)
par.ParagraphFormat.Alignment = ParagraphAlignment.Center;
// Save result.
doc.Save(@"Test001\out.doc");

Hope this helps.
Best regards,

Thanks a lot again. I got it.