Cell widths ignored if Table.setAllowAutoFit is set to true

Using the new 10.5 build tables saved in PDF format ignore the cell width settings defined. The column widths seem to be re-calculated by Aspose when saving in PDF/Word format if Table.setAllowAutofit is set to true.

This new regression cripples the PDF output to the point it cannot be used.

Word output seems to be affected as well if the total width of the table is smaller than the width of the window.

The attached files show the results of using 10.04 and 10.05 for both Word and PDF.

On further digging this seems to be caused by the allowAutofit method being deprecated. Removing the call to it results in the table cell widths being used BUT autofit to contents does not work as expected. Run the code below with 10.04 and 10.05 and you will observe differences.

This could be a matter of education so please advise what needs to be changed in code using Aspose 10.05 to take into account the changes made in this version. The release notes specify about the new API but I don’t find a single source of information to describe these changes.

import org.junit.Test;

import com.aspose.words.DocumentBuilder;
import com.aspose.words.LineStyle;

public class TestAsposeCellWidth
{
    @SuppressWarnings( "nls")
    @Test
    public void testCellWidth() throws Exception
    {
        DocumentBuilder docBuilder = new DocumentBuilder();
        com.aspose.words.Style style = docBuilder.getDocument().getStyles().get("Normal");
        if (style != null)
        {
            docBuilder.getParagraphFormat().setStyle(style);
        }

        docBuilder.clearRunAttrs();

        // begin table
        docBuilder.startTable();

        docBuilder.getParagraphFormat().clearFormatting();
        docBuilder.getCellFormat().clearFormatting();
        docBuilder.getRowFormat().clearFormatting();

        // The first row
        docBuilder.getRowFormat().setHeight(0);
        // if this is uncommented all cell widths are ignored
        // docBuilder.getRowFormat().setAllowAutoFit( true);
        
        // The first cell
        docBuilder.insertCell();
        docBuilder.getCellFormat().clearFormatting();
        docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
        docBuilder.getCellFormat().getBorders().setLineWidth(1);
        docBuilder.getCellFormat().setWidth( 100);

        docBuilder.getParagraphFormat().clearFormatting();
        docBuilder.write("Lorem ipsum dolor sit amet");

        // The second cell
        docBuilder.insertCell();
        docBuilder.getCellFormat().clearFormatting();
        docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
        docBuilder.getCellFormat().getBorders().setLineWidth(1);
        docBuilder.getCellFormat().setWidth( 350);

        docBuilder.getParagraphFormat().clearFormatting();
        docBuilder.write("Lorem ipsum dolor sit amet");

        // The third cell
        docBuilder.insertCell();
        docBuilder.getCellFormat().clearFormatting();
        docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
        docBuilder.getCellFormat().getBorders().setLineWidth(1);

        docBuilder.getParagraphFormat().clearFormatting();
        docBuilder.write("Lorem ipsum dolor sit amet");

        // The fourth cell
        docBuilder.insertCell();
        docBuilder.getCellFormat().clearFormatting();
        docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
        docBuilder.getCellFormat().getBorders().setLineWidth(1);

        docBuilder.getParagraphFormat().clearFormatting();
        docBuilder.write("Lorem ipsum dolor sit amet");

        // End the first row
        docBuilder.endRow();

        // end table
        docBuilder.endTable();

        docBuilder.getDocument().save("c:\testtable1.doc");
    }
}

I think I have a better understanding of what is happening. The issue seems to be with how cell widths and table.setAllowAutoFit ( the equivalent of Word’s “Resize to fit contents”) interact.
- if I set this value using the Aspose API to true then all cell witdhs are also clear which I believe is incorrect behavior
- if I set this value from Word after the document was generated I get the expected behavior: the cells resize but they still preserve their preferred widths

See attached documents for details. Test code is the same as above with a call to table.setAllowAutoFit( true) being added.

ASPOSE_AutoFit_TRUE.doc - generated with table.setAutoFit( true). All cell widths are removed
ASPOSE_AutoFit_FALSE.doc - generated with table.setAutoFit( false). All cell widths are preserved but the document exceeds page margin. Correct behavior.
ASPOSE_AutoFit_FALSE_WORD_Modified.doc - generated with table.setAutoFit( false) then having the auto fit set from Word ( Table properties->options->Resize to fit content = true). The result fits the page as the columns are resized proportionally and preserving the preferred size. This is the expected behavior and what was happening in 10.04 and before.

For the sake of completeness see attached the results of using the code above with Aspose 10.04 and 10.05.

Hi

Thanks for your request. Yes, behavior of Aspose.Words was changed. However, this is not regression, this occurs because we introduced a new feature:
https://reference.aspose.com/words/java/com.aspose.words/table#autoFit(int)

I think, you will find this feature very useful in your case. Please let us know if you need more assistance. We are always glad to help you.

Best regards,

Hey Andrey,

looking at previous versions and how it works in Word this is a regression. I’m talking here of the setAllowAutofit. The method that was added to the Table object exists in RowFormat and was used with a similar effect.

Please note that Word tables have 2 properties that deal with autosize :
- auto size to contents - resizes the cells to allow the table to fit the page. In word this is set bu right clicking on the table and selecting “autofit to contents” from the context menu.
- resize to fit contents - when set to false allows Word to keep the exact widths of the cells by breaking words when no white space exists to do that. In Word this is set from the Table Properties dialog, Options, Resize to Fit contents

Setting “resize to fit contents” to true in Word and in any previous Aspose version does not clear the width of the cells. In some cases ( width larger than contents) it does not affect the width at all as it’s not needed. In Aspose 10.05 setting this property clear the cell widths indiscriminately which simply does not make sense and breaks the expected behavior.

See here for more details on the expected behavior.

To me it looks like in Aspose 10.05 two distinct properties were merged in a single property and that the “resize to fit contents” does more than it should do.

With the new way autosize and resize to fit contents work it is not possible to achieve the correct results achieved with previous versions, including 10.04 and older.

A simple test is to try getting a 200 point column that has a single
short word in it ( like “test”) and also has “resize to fit contents”
to true. You can do it in Word and you can do it in Aspose 10.04 or older but you can no longer do it in 10.05 ( the column width will be cleared in the resulting document). See the example documents and code snippets I attached above.

Regards,
Dragos

Hi Dragos,

Thanks for your inquiry.

When you apply AutoFit settings this will automatically clear the preferred width from the table and cells. This behaviour appears identical to what happens in Microsoft Word as well (try creating a single celled table with some text and choosing AutoFit to Contents or take the output that you attached demonstrating AW10.4 and choose AutoFit to Contents. Both will appear the same as how they are exported using Aspose.Words 10.5.).

To produce the output that you are expecting you need to reapply the preferred width, please see the examples below, both should produce the correct output.

Technique #1

Table table = builder.StartTable();
builder.InsertCell();
table.AutoFit(AutoFitBehavior.AutoFitToContents);
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(200);
builder.Writeln("Test");
builder.EndTable();

Technique #2

Table table = builder.StartTable();
builder.InsertCell();
table.AllowAutoFit = true;
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(200);
builder.Writeln("Test");
builder.EndTable();

Thanks,

Thanks Adam. Setting the preferred width seems to help for the Word output. For PDF however tables are wider than the page width. Try creating a 4 column table with the following widths: 100, 350, auto, auto.

Regards,
Dragos

Hey Adam, Andrey,

the original issue reported in this thread are solved to a certain degree but I have discovered 2 other table related problems. I have created distinct threads for them to avoid confusion:
https://forum.aspose.com/t/tables-are-rendered-differently-in-word-and-pdf/57480/1
https://forum.aspose.com/t/hyperlinks-in-tables-break-resizing-algorithms-for-pdf-output/57467/1

Your assistance is highly appreciated.

Regards,
Dragos

Hi Dragos,

Thank you for additional information. It is perfect that you managed to resolve this issue. We will investigate the other issues and reply you shortly in the appropriate threads.

Best regards,