We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

AutoFit Table to Document Window


Can you please suggest me some trick to fit the table to the width od
the document? I think You still did not implement some method or
property so this task could be performed.

Faster respond will be very apreciated.

Thank You in advance.

MS Word 2003 has the following setting for this Table | Autofit | Autofit to window.
To make it work in Aspose.Words try the following approach - set RowFormat.AllowAutofit to true and make the cell widths intentionally too big. Here is an example:

builder.RowFormat.AllowAutoFit = true;
builder.Font.Size = 12;
// intentionally setting cell width too big to allow Word autofit to do the adjustment
builder.CellFormat.Width = WordConvert.PixelToPoint(1000);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; 
builder.Write("1st Cell");
builder.Write("2nd cell");
builder.Write("3rd cell");

I also ran into the same problem. This approach (very wide cells) does not work, the table does not auto-size at all (try tables with more than 1 row!) If the cells are a just little bit too wide, all rows are autosized correctly, except the first one. (all using Word 2003)
I found some differences in the view mode - if the template document was saved in Normal View, the table is not auto-sized correctly, if saved in Print Layout View, Word does autosize it.
I feel that the Table object should have a TableFormat property which allows the programmer to set the width of the table, the alignment, the indent and the text wrapping.
It would also be very helpful if the DocBuilder at some time returns the Table object it created (e.g. as a return value of StartTable() or EndTable() )

Thanks for your proposals on improvement of our API.
I have logged your suggestion on adding TableFormat property to our defect base as Issue #907.
Your suggestion on returning the table node reference in DocumentBuilder StartTable and EndTable methods is logged to our defect base as Issue #906.
We will discuss and maybe add this features in one of our next releases.

To extend the ide of the DocumentBuilder returning the Table, it might be good to let it return all the objects it creates (where appropriate), e.g. on Write() it returns the Run (I guess it is that what it creates), on WriteLn() it returns the Paragraph, on InsertImage()… etc.
While we are in the request mode :slight_smile: … another request would be to have easy access to the “surroundings” of every object, i.e. the Paragraph, the Bookmark, the Section, etc. where an object is in (where applicable). I needed to get te section where a bookmark is in, to size the table to the margins of the section. I iterate through all sections of the document, and check if my bookmark is in that section… I recently found out that GetAncestor() is there to provide this functionality, but it would be easier to use and to understand have a EnclosingSection property on the Bookmark (or something similar)

Your point is taken :slight_smile: But please mind that changes to API is not something that can be taken lightly. We need time to discuss it and analyze all pros and cons.

We have introduced some of the proposed API changes in the latest Aspose.Words 3.6 which is now available for download.
Most of DocumentBuilder node creation methods (InsertXXX, StartXXX, EndXXX) now return the reference to the newly created node.
We are also considering adding return values to the Write and Writeln methods. In the next release maybe.
Please mind that these methods can create several nodes at once, e.g. Write(“Hello\rHello”) will create two runs and one paragraph.
Current suggestion is that Write method will return reference to the last of the newly created runs and Writeln will return reference to the last of the newly created paragraphs. That is logged as Issue #959 in our defect base.
If you have any other suggestions we will be grateful if you post them here.
Best Regards,

Has there been any further resolution on this topic: Auto Fitting tables? I would be happy with simply being able to set the width of the table.

Sadly no. TableFormat which would allow that is not available yet. If you provide a more detailed explanation of your task, perhaps I’ll be able to suggest a workaround.

Just want my newly inserted table to stretch to the width of the page.

Alas, in this case all I can suggest is to wait for advanced table formatting offered by Aspose.Words.

As Dmitry have already pointed out, we have not implemented Autofit table to our API yet. But you can easily create this method yourself. For this you need to know the page content width:

DocumentBuilder builder = new DocumentBuilder(doc);
Section currentSection = builder.CurrentSection;
PageSetup pageSetup = currentSection.PageSetup;
double availableWidth = pageSetup.PageWidth - pageSetup.LeftMargin - pageSetup.RightMargin;

Then you need to adjust the Cell.CellFormat.Width of the cells in each row. You can do it when building the table. For example,

// Set first cell to 1/3 of the page width.
builder.CellFormat.Width = tableWidth / 3;
// Set the second cell to 2/3 of the page width.
builder.CellFormat.Width = tableWidth * 2 / 3;
builder.Write("text 2");

Or you can wtite a specialized method that will iterate over all rows in the given table and will recalculate width of every cell in the row proportionally so that it would fit the page width.
Hope that helps,

Is this issue resolved in the latest API? I also want to AutoFit an existing table to the window size.


Thanks for your inquiry. Unfortunately, the issue is still unresolved.
Best regards.

The issues you have found earlier (filed as WORDSNET-352;WORDSNET-581) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.