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

Free Support Forum - aspose.com

Table AutoFit to window

Hi,


Is there any way to get a table generated using Aspose.Words to autofit to the width of the page? We are currently migrating document generation from being done through Word automation to using Aspose and this is a bit a of a show stopper!

Kind regards,

Will Holley

Hi Will,

We are currently looking into supporting auto fitting a table to page width. Your request for the feature has been noted and we will inform you of any developments.

In the mean time you can still achieve this by using code similar to what’s used in this thread:

https://forum.aspose.com/t/121936

Setting the cells width and AutoFit to true should cause the table to be auto fitted to the page width.

If you are using Java to program then check out the API reference for Autofitting here.

Please feel free to ask if you have any further enquires.

Thanks,

Thanks Adam,


Unfortunately this doesn’t work for me. When I open the document in Word (2010), the columns are really wide and overflow the page boundaries (see attachement).

The code I am using to generate the table in this instance is:


builder.StartTable();
builder.RowFormat.ClearFormatting();
builder.RowFormat.AllowAutoFit = true;
builder.RowFormat.AllowBreakAcrossPages = table.AllowPageBreaksInRows; // see #7651 for discussion
builder.ParagraphFormat.SpaceAfter = 0;

foreach (var row in table.Rows)
{
var gray = Color.FromArgb(0x7F, 0xE0, 0xE0, 0xE0);
var firstRow = row == table.Rows.FirstOrDefault();
var lastRow = row == table.Rows.LastOrDefault();

if (!lastRow)
{
    builder.RowFormat.Borders.Horizontal.LineWidth = 1;
}

builder.RowFormat.HeadingFormat = firstRow;                    
builder.RowFormat.Borders.Color = gray;</pre><pre>
try
{
    if (table.BackgroundColor == Color.Transparent && firstRow) 
    {
        builder.CellFormat.Shading.BackgroundPatternColor = gray; 
    } 
    else 
    { 
        builder.CellFormat.Shading.BackgroundPatternColor = table.BackgroundColor; 
    } 
                    
    for (int i = 0; i < row.Cells.Length; i++)
    {
        var cell = row.Cells[i];
        if (!firstRow)
        {
            builder.Bold = table.ColumnDefinitions[i].Bold;
        }
        builder.InsertCell();
        builder.CellFormat.ClearFormatting();

        if (cell.Width.HasValue)
        {
            builder.CellFormat.Width = cell.Width.Value;
        }
        else
        {
            // attempt to force autofit. see https://forum.aspose.com/t/121936
            builder.CellFormat.Width = 1000;
        }
                       
        if (cell.Background != null && !firstRow)
        {
            builder.CellFormat.Shading.BackgroundPatternColor = cell.Background.Value;
        }

        builder.ParagraphFormat.SpaceBefore = 2;</pre><pre>            // write the actual cell content
        WriteContainer(cell, builder);</pre><pre>        }
    builder.EndRow();
}
finally
{
    builder.CellFormat.Shading.BackgroundPatternColor = Color.Transparent;
}

}

builder.EndTable();


Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. I think the only workable workaround in your case will be calculating width of cells upon generating the table. For example see the following code:

DocumentBuilder builder = new DocumentBuilder();

// Calculate width of page.

PageSetup ps = builder.CurrentSection.PageSetup;

double pageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;

// We cal calculate width of each cell only if we know number of cells.

// Let suppose that each cell should have the same width.

const int culumnsCount = 5;

double colWidth = pageWidth/culumnsCount;

// Now we can generate simple table.

for (int rowIdx = 0; rowIdx < 10; rowIdx++ )

{

for(int colIdx=0; colIdx<culumnsCount; colIdx++)

{

builder.InsertCell();

builder.CellFormat.Width = colWidth;

builder.Write("this is some text in the cell");

}

builder.EndRow();

}

builder.EndTable();

// Save output document.

builder.Document.Save(@"Test001\out.doc");

Hope this could help you.

Best regards.

Thanks Alexey,


Unfortunately this solution is not good enough. Before we attempt to write our own table sizing algorithm to match what Word does, are there any plans to implement this in Aspose.Words? I see there have been repeated requests for this functionality over the years yet no sign of a feature?

This is something of a showstopper for us as we are attempting to replace Word Automation with Aspose and without accurate reproduction of tables we cannot do this.

Thanks,

Will

Hi Will,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. I am afraid that I cannot provide you a reliable estimate regarding this issue at the moment. Answering the second question – no, there were not many requests from the customers regarding this feature.

I suppose, mail merge with regions is the better option for generating tables in MS Word documents. In case of using this option, you can configure table using MS Word and then just fill it with data using Aspose.Words. Please follow the link to learn more about this feature:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/mail-merge-with-regions-explained.html

I hope this approach could be useful for you.

Best regards.

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


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