EPPlus
EPPlus copied to clipboard
Make the properties of width and height in the ExcelDrawing class
I have a excel template like this:
And I want to set the position of one of these images in the red rectangle. The images can have different sizes, but the width of the column is constant, so if I can know the width and height (implement the properties of these variables) of the drawing I can do this.
Or there is another way to to this?
Depending on the type Anchor type (the EditAs property) of you can use the From
, To
, Position
and Size
properties for sizing and positioning. For grouped drawings I think you should use Position
and Size
.
You can also use the SetPosition and SetSize Methods, but it might not work if you want to preserve the width.
Playing with these properties directly you might need to set the "ExcelPackage.DoAdjustDrawings" to false.
Thanks you. After your comments I tried and could solve the problem with this:
.Drawings.Item(aux).ChangeCellAnchor(eEditAs.OneCell) 'with this line now property Size is not null anymore (and I can access to the width of the imagen)
Dim ColOffset As Integer = (360 - .Drawings.Item(aux).Size.Width / 9525) / 2
Dim Col As Integer = Int(ColOffset / 40) 'integer columns where the draw should start
ColOffset = ColOffset - 40 * Col 'each column is 40 pixels
.Drawings.Item(aux).SetPosition(21, 10, 20 + Col, ColOffset) 'the initial row is 21 and the initial column is 20, row height is 20 pixels
The only thing I noticed is that the inputs RowOffsetPixels and ColumnOffsetPixels of the function SetPosition if they are negative and grater than the row height or column width, they can be in a row less than the initial row or column. But If they are positive and grater than the row height or column width, they only go to the inital row +1 and initial column +1.
You can also use the From
and Two
properties for coordinates when the cell anchor is set to TwoCell.
Yes but when I tried to use From
and To
combined with ExcelPackage.DoAdjustDrawings = false
and OneCell,
the drawing change the size.
I tried like this:
.Drawings.Item(aux).EditAs = eEditAs.OneCell
.Drawings.Item(aux).From.Row = 21
.Drawings.Item(aux).From.Column = 20
.Drawings.Item(aux).To.Row = 28
.Drawings.Item(aux).To.Column = 29
And the result was like this:
The size of the imagen change. My idea was to be in that area without changing the size and to be in the middle.