Apache POI 5.2.2 : How to draw a XSSFSimpleShape based on Offsets and Dimensions in Centimeters?

72 Views Asked by At

I want to draw shapes in Excel with Java, using measurements in Centimeter : offset in cm from left side, offset in cm from top side, width in cm, height in cm.

The Apache POI 5.2.2 looks good for this. I saw the XSSFClientAnchor class.

Several XSSFClientAnchor examples are available, but they deal with measurements in (point, characters, columns numbers, rows numbers). See e.g. this question, the getting started code

I tried several codes based on examples, but I very often get unexpected results. Could you please help to convert (offset, width and height) to (point, charachters, columns,rows) ? Thank you very much.

Additional info : In the past I programmed Excel VBA macros to draw shapes. I used this code to draw a shape, based on offsets and dimensions in centimeters. The function Shapes.AddShape(...) takes as input values in "points," which can be computed from "centimeters*28.34646".

VBA code :

Sub drawer1()
    Dim sShape1 As Shape

    dDeltaLeftCm = ConvertCmToPoint(100)
    dDeltaTopCm = 0
    dWidthCm = ConvertCmToPoint(50)
    dHeightCm = ConvertCmToPoint(10)
    
    Set sShape1 = Application.Sheets(1).Shapes.AddShape(msoShapeRectangle, dDeltaLeftCm, dDeltaTopCm, dWidthCm, dHeightCm)
End Sub

Function ConvertCmToPoint(ByVal cm As Double) As Double
    ConvertCmToPoint = cm * 28.34646
End Function

In Vba, my code behaved as expected.

0

There are 0 best solutions below