Free Support Forum - aspose.com

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?

<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

<![endif]–><span style=“font-size:12.0pt;font-family:“Times New Roman”,“serif”;
mso-fareast-font-family:“Times New Roman”;mso-ansi-language:EN-US;mso-fareast-language:
EN-US;mso-bidi-language:AR-SA”>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: http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/keeping-tables-and-rows-from-breaking-across-pages.html

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: http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-migrate-to-aspose-words-10-5-or-higher.html

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

Thanks,