RowFormat setAllowAutoFit can not work as expected

Hi,

With Aspose 10.2.0, I have output one table with one row with one cell into a Word document, and set RowFormat AllowAutoFit to true. But the output result looks very weird. If I changed Aspose to older version 2.5, it worked well. I wonder it is a regression. Please reference the below code and outptu attachements:

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);
docBuilder.getRowFormat().setAllowAutoFit(true);

// The first cell
docBuilder.insertCell();
docBuilder.getCellFormat().clearFormatting();
docBuilder.getCellFormat().getBorders().setColor(Color.black);
docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
docBuilder.getCellFormat().getBorders().setLineWidth(1);
docBuilder.getParagraphFormat().clearFormatting();
docBuilder.write("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent vitae blandit lectus. Vestibulum nec dolor at leo consequat pellentesque. Ut euismod aliquet massa dapibus mattis. Nunc tristique eros nec quam feugiat vel euismod erat tincidunt. Suspendisse potenti. Quisque euismod vestibulum facilisis. Quisque tempus, lectus ut molestie fermentum, nisl purus mattis sapien, ut facilisis turpis nulla eget purus. Cras sit amet justo tortor. Curabitur sed tellus nec sem ultrices accumsan in vel est. Nunc feugiat laoreet purus, et mollis orci dictum a. Aliquam a ante risus. Integer feugiat posuere orci eu sagittis. Cras molestie diam et eros molestie interdum. Nam dignissim enim a velit fringilla a rhoncus ipsum iaculis. Cras varius orci et odio fermentum nec semper magna fermentum. Pellentesque tempor fermentum nibh ullamcorper consectetur.");

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

// end table
docBuilder.endTable();

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

Version 2.5 output that is expected:



Aspose 10.2.0 output looks weird:




Thank you.

Hi

Thanks for your request. Yes, this is known issue. Your request has been linked to the appropriate issue. We will let you know once it is resolved.

Maybe in your case as a temporary workaround, you can try using code suggested here:
https://docs.aspose.com/words/java/working-with-tables/

Hope this helps.

Best regards,

It seems like the workaround is for resize table auto to fit window, not for resize to fit content. Is there any other workaround?

Could I have the fix in the next coming release of July? Because our product will release a new version in Aug to have the latest Aspose Word Java library. If can not have the fix, possiblly we may not upgrade Aspose to the latest version, since there is a big regression introduced by this issue.

Thanks.

Hi Vincent,

Thanks for your request. Currently the issue is planned for August release. We will let you know if the issue is fixed earlier.

Please note, that all estimate we provide in the forum are rough estimates and you cannot 100% rely on them.

Best regards,

So is there any other workaround for resize to fit content we can temporary use? It is very urgent.

Thank you.

Hi

Thanks for your request. Unfortunately, there is no direct way to calculate width of text inside cell to adjust its width. However, nothing is impossible. You can approximately calculate width of each character as half of font size.

Also, in .NET you can try using System.Drawing to measure text. For instance, the following code approximately calculate width of text inside cell I suppose in Java there should be a similar approach:

///
/// Method calculate an aproximate width of content inside cell.
///
private double CalculateWidhtOfCellContent(Cell cell)
{
    double width = 0;
    foreach (Paragraph paragraph in cell.Paragraphs)
    {
        double intermWidth = 0;
        foreach (Run run in paragraph.Runs)
        {
            using (Bitmap bmp = new Bitmap(1, 1))
            {
                bmp.SetResolution(96, 96);
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    using (System.Drawing.Font f = new System.Drawing.Font(run.Font.Name, (float)run.Font.Size))
                    {
                        SizeF textSize = g.MeasureString(run.Text, f);
                        intermWidth += textSize.Width;
                    }
                }
            }
        }

        if (intermWidth > width)
            width = intermWidth;
    }

    return width;
}

Hope this code could be useful for you.

Best regards,

Hi,

I don’t this work around can work for me. Now this issue blocked our new release, please fix it as soon as possible.

Thanks.

Hi Vincent,

Thanks for your request. The issue is planned to be fixed before the August release. We will keep you informed and let you know once it is resolved.

Best regards,

Thank you, that is exciting.

I did a quick test on July release 10.3.0, and the issue did not get fixed.

Can I have the date on which you can fix the reported issue? We really need it
get fixed in Aug earlier, because our product latest release will use Aspose
10.x.0, and the issue has blocked our release. And if we release our product without a fix from you, our customers
will be unhappy seeing regressions.

Best regards.

Hi

Thanks for your request. The issue is scheduled to be fixed in the next version of Aspose.Words that comes out at the end of August. We will let you know once the fix is available.

Best regards,

How is the progress? Can it be fixed in Aug release?

Hi

Thanks for your request. Unfortunately, the issue is not closed yet. So I cannot promise that the fix will be included into the next version.

Best regards,

Hi Alexey,

As I mentioned before this issue has blocked our product release, it is a serious regression in both Aspose and our product. I will reiterate that we really need it get fixed in Aug release.

Thanks & regards.

Hi Vincent,

Thanks for your request. Yes, we understand the issue is important for you. But unfortunately, there are many other issues we have to work on. The issue you have reported is on track and should be resolved soon. I apologize for inconvenience. We will keep you informed regarding status of this issue and let you know once it is resolved.

Best regards,

Hi Vincent,

Just to let you know, auto fitting a cell to its content will be supported in the next version of Aspose.Words due out at the end of the month.

Thanks,

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

Hey all,

this issue is fixed on Aspose 10.05 for Word output but it is still an issue for PDF. See attached code to reproduce the issue on PDF:

@Test
public void testSingleCell() 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
    Table table = docBuilder.startTable();

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

    // The first row
    docBuilder.getRowFormat().setHeight(0);

    // The first 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, consectetur adipiscing elit.Praesent vitae blandit lectus.Vestibulum nec dolor at leo consequat pellentesque. Ut euismod aliquet massa dapibus mattis. Nunc tristique eros nec quam feugiat vel euismod erat tincidunt. Suspendisse potenti. Quisque euismod vestibulum facilisis. Quisque tempus, lectus ut molestie fermentum, nisl purus mattis sapien, ut facilisis turpis nulla eget purus. Cras sit amet justo tortor.Curabitur sed tellus nec sem ultrices accumsan in vel est. Nunc feugiat laoreet purus, et mollis orci dictum a.Aliquam a ante risus. Integer feugiat posuere orci eu sagittis. Cras molestie diam et eros molestie interdum.Nam dignissim enim a velit fringilla a rhoncus ipsum iaculis. Cras varius orci et odio fermentum nec semper magna fermentum. Pellentesque tempor fermentum nibh ullamcorper consectetur.");

    // none of the below helps
    // table.autoFit(AutoFitBehavior.AUTO_FIT_TO_CONTENTS);
    table.setAllowAutoFit(true);

    // end table
    docBuilder.endTable();


    docBuilder.getDocument().updateTableLayout();


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

Regards,
Dragos

Hey all,

one more example to reproduce this behavior is below. Two notes:

  • if you comment the code that adds the first table, the second table will be resize properly
  • if you set the preferred cell width on the second table ( uncomment that code) if you also invoke the autofit method ( with either Autofit to Window or Autofit to Contents) the width will be ignored on PDF
@Test
public void testIncorectResize() 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();

    docBuilder.writeln( "First table");
    // first table
    {
        // begin table
        Table table = docBuilder.startTable();
                    
        docBuilder.getParagraphFormat().clearFormatting();
        docBuilder.getCellFormat().clearFormatting();
        docBuilder.getRowFormat().clearFormatting();
                       
        // The first row
        docBuilder.getRowFormat().setHeight(0);   

        // The first cell
        docBuilder.insertCell();
                       
        docBuilder.getCellFormat().clearFormatting();   
        docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
        docBuilder.getCellFormat().getBorders().setLineWidth(1);
        docBuilder.getCellFormat().setWidth(  150);
        docBuilder.getCellFormat().setPreferredWidth( PreferredWidth.fromPoints( 150));
            
        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(  300);
        docBuilder.getCellFormat().setPreferredWidth( PreferredWidth.fromPoints( 300));
            
        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.getCellFormat().setWidth(  70);
        docBuilder.getCellFormat().setPreferredWidth( PreferredWidth.fromPoints(70));
                    
        docBuilder.getParagraphFormat().clearFormatting();
        docBuilder.write("Lorem ipsum dolor sit amet");
            
        // End the first row
        docBuilder.endRow();
            
        table.setAllowAutoFit( true);
            
        // end table
        docBuilder.endTable();
    }
        
    docBuilder.getDocument().updateTableLayout();
        
    // second table
    docBuilder.writeln( "");
    docBuilder.writeln( "Second table");
    {
        // begin table
        Table table = docBuilder.startTable();
                    
        docBuilder.getParagraphFormat().clearFormatting();
        docBuilder.getCellFormat().clearFormatting();
        docBuilder.getRowFormat().clearFormatting();
           
            
        // The first row
        docBuilder.getRowFormat().setHeight(0);   
    
        // The first cell
        docBuilder.insertCell();
        docBuilder.getCellFormat().clearFormatting();   
        docBuilder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
        docBuilder.getCellFormat().getBorders().setLineWidth(1);
        //docBuilder.getCellFormat().setWidth( 450);
        //docBuilder.getCellFormat().setPreferredWidth( PreferredWidth.fromPoints( 450));
            
        docBuilder.getParagraphFormat().clearFormatting();
        docBuilder.writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
            
        docBuilder.endRow();
            
        // none of the below helps
        table.autoFit( AutoFitBehavior.AUTO_FIT_TO_WINDOW);
        table.setAllowAutoFit( true);
            
        // end table
        docBuilder.endTable();
    }
        

    docBuilder.getDocument().updateTableLayout();
       
        
    docBuilder.getDocument().save("c:\\testtable_incorectresize.doc");
    docBuilder.getDocument().save("c:\\testtable_incorectresize.pdf");
}

Regards,
Dragos