Free Support Forum - aspose.com

Image gets resized when "Textwrap" is enabled

Hi!

Please find attached a sample application (VB.Net) including all examples etc. that demonstrates the issue.

The general issue is, that images resize when autowrap of a cell in a row that is covered by the image is set to true.

Basically, what happens is:

1.) Open “input.xlsx”
2.) Add picture “input.png”
3.) Save the workbook as “output.xlsx”
4.) Set text wrap and text content of a cell
5.) Save the workbook as “output.xlsx”

If you run this code, you will see that the image is perfectly fine in the output! Now comment out the 3rd step (= the first save of the workbook) and run again - you will see the the image is (somehow) vertically stretched. This does NOT happen, if textwrap is not set, so it has something to do with the textwrap.

If I add the picture AFTER setting the textwrap settings (that is not included in the attached sample application), then the image is stretched as well.

I need a consistent behaviour, where - when I add a picture with a given heightscale and widthscale - it never changes its size and provides reliable output.

Thanks for your help,
Bernd
AsposeTester.zip (4.7 MB)

@bernd.podhradsky,

We were able to observe the issue but we need to look into it more. We have logged the issue in our database for investigation and for a fix. Once, we will have some news for you, we will update you in this topic.

This issue has been logged as

CELLSNET-46362 - Image gets resized when "Textwrap" is enabled

@bernd.podhradsky,

We evaluated your issue further and found it is not an issue with the APIs. You just need to follow certain order/sequence. Please call Worksheet.AutoFitRows() method and then add picture after it. See the following sample code for your reference:
e.g
Sample code:

........
 With lWorkbook.Worksheets.First()
            'Comment the next line out, to see what's happening
            'lWorkbook.Save("output18.9.2.xlsx")
            lStyle = .Cells.GetCell(1I, 2I).GetStyle()
            lStyle.IsTextWrapped = True
            .Cells.GetCell(1I, 2I).PutValue("This is a test text that is justed used to ensure that text needs to be wrapped.")
            .Cells.GetCell(1I, 2I).SetStyle(lStyle)
            .AutoFitRows()
            .Pictures.Add(1I, 1I, "input.png", 100I, 100I)
        End With 

Let us know your feedback.

Hi!

Thanks a lot - that seems to do the trick.

Any idea why saving the workbook changes anything in that regards?

Bernd

@bernd.podhradsky,

Did you update your code segment as suggested? If you still find the issue, kindly provide a sample console (runnable) application (you may zip the project prior attaching it here) to show the issue, we will check it soon.

PS. Please try our latest version/fix (e.g Aspose.Cells for .NET v18.9.x) if you are not already using it.

Hi!

When I add “AutoFitRows()” it works as expected! My question was referring to the fact that the following code snipped does not work:

 Dim lWorkbook As New Workbook("input.xlsx")
    Dim lStyle As Style = Nothing
    With lWorkbook.Worksheets.First()
        .Pictures.Add(1I, 1I, "input.png", 100I, 100I)
        lStyle = .Cells.GetCell(1I, 2I).GetStyle()
        lStyle.IsTextWrapped = True
        .Cells.GetCell(1I, 2I).PutValue("This is a test text that is justed used to ensure that text needs to be wrapped.")
        .Cells.GetCell(1I, 2I).SetStyle(lStyle)
    End With
    lWorkbook.Save("output.xlsx")
    Console.WriteLine("Done! Press any key!")
    Console.ReadKey()

whereas the following snipped produces the correct output:

 Dim lWorkbook As New Workbook("input.xlsx")
    Dim lStyle As Style = Nothing
    With lWorkbook.Worksheets.First()
        .Pictures.Add(1I, 1I, "input.png", 100I, 100I)
        lWorkbook.Save("output.xlsx")
        lStyle = .Cells.GetCell(1I, 2I).GetStyle()
        lStyle.IsTextWrapped = True
        .Cells.GetCell(1I, 2I).PutValue("This is a test text that is justed used to ensure that text needs to be wrapped.")
        .Cells.GetCell(1I, 2I).SetStyle(lStyle)
    End With
    lWorkbook.Save("output.xlsx")
    Console.WriteLine("Done! Press any key!")
    Console.ReadKey()

The only difference here is that - after adding the picture - I added the line

lWorkbook.Save("output.xlsx")"

So even when not using AutoFitRows() it works if I simply save the workbook after adding the picture, then add the table and then save it again. I was just wondering what’s the logic behind this and why it would work in that case?

Bernd

@bernd.podhradsky,

I noticed the behavior using your updated code segments. Well, one thing you need to follow: if you need to insert a picture into some cell, you would require to extend that row height using Cells.SetRowHeight() method or use auto-fit row operation. Saving the file (in the first iteration) might add certain attributes with the workbook and its objects, so it would also work fine.

We will further evaluate the scenario/case and may provide complete details (of internal modules) for your reference.

Hi!

Thanks for your answer.

As I previously stated, “AutoFitRows()” works for me now, so I can avoid the stretched images to occur. I just found it interesting that saving the workbook seems to also work, without having to add AutoFitRows and was just wonderung why exactly that was the case, because it seems as if saving the workbook - as you also suggest - updates some internal attributes/properties which avoid the issue, but only if I save it BEFORE setting the wordwrap property.

From my point of view everything’s working now as expected, it’s just not very clear to an API user why exactly a certain behaviour occurs/disappears when saving the workbook in between :).

Thanks for your great support,
Bernd

@bernd.podhradsky,

The default placement of the shape is set as MoveAndSize. So, if the shape (e.g image) is added, the size of the shape will be changed too when the row height is changed. It works the same way as MS Excel does.

When you add a shape in MS Excel, then change the row’s height and column’s width, the size of the shape will be changed too. To cope with it, you would only need to change the placement type to other than "move and size with the cells, the size will not be changed. See the sample code for your reference:
e.g
Sample code:

 Dim lWorkbook As New Workbook("e:\\test2\\input.xlsx")
        Dim lStyle As Style = Nothing
        With lWorkbook.Worksheets.First()
            .Pictures.Add(1I, 1I, "e:\\test2\\input.png", 100I, 100I)
            .Pictures(0).Placement = Drawing.PlacementType.Move
            lStyle = .Cells.GetCell(1I, 2I).GetStyle()
            lStyle.IsTextWrapped = True
            .Cells.GetCell(1I, 2I).PutValue("This is a test text that is justed used to ensure that text needs to be wrapped.")
            .Cells.GetCell(1I, 2I).SetStyle(lStyle)
        End With
        lWorkbook.Save("e:\\test2\\out1.xlsx")
        Console.WriteLine("Done! Press any key!")
        Console.ReadKey()

Hope, this helps a bit.