Hello team,

We are getting the padding issue with setting the image for SXSSWorkbook
for 5.2.3 apache poi version and java 17

On using setDx1() and setDy1() to give padding, the changes are not seen
for SXSSFWorkbook. A similar code is working for XSSFWorkbook. We
are currently using java version 17 and the latest apache poi version
5.2.3. The same issue is not seen in the versions of java 8 and 3.14 apache
poi. The below code is used to add padding in the cell for the setting
image in the workbook.

anchor.setDx1(centerPosPx * Units.EMU_PER_PIXEL);
anchor.setDy1(centerPosPy * Units.EMU_PER_PIXEL);

*Here I described my code to draw a logo image in a workbook using apache
poi 5.2.3, which works fine in the versions of java 8 and 3.14 apache poi.*

int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);

        CreationHelper helper = workbook.getCreationHelper();
        ClientAnchor anchor = helper.createClientAnchor();

        anchor.setCol1(colStart); //Col Start
        anchor.setRow1(rowStart); //Row Start
        anchor.setCol2(colEnd); //Col End
        anchor.setRow2(rowEnd); //Row End

        //create a picture anchored to Top-Left Corner
        Drawing drawing = sheet.createDrawingPatriarch();
        Picture pict = drawing.createPicture(anchor, pictureIdx);
        pict.resize(1);

        //get the picture width
        int pictWidthPx = (int) (pict.getImageDimension().width *
reSizeRatio);
        int pictHeightPx = (int) (pict.getImageDimension().height *
reSizeRatio);

        //get the cell width colStart to colEnd
        float cellWidthPx = 0f;
        for (int col = colStart; col <= (colEnd - 1); col++) {
            cellWidthPx += sheet.getColumnWidthInPixels(col);
        }

        //get the cell width colStart to colEnd
        float cellHeightPx = 0f;
        for (int row = rowStart; row <= (rowEnd - 1); row++) {
            cellHeightPx += (sheet.getRow(row).getHeightInPoints() *
Units.EMU_PER_POINT) / Units.EMU_PER_PIXEL;
        }

        //calculate the center position
        int centerPosPx = Math.round(cellWidthPx / 2f - (float) pictWidthPx
/ 2f);
        int centerPosPy = Math.round(cellHeightPx / 2f - (float)
pictHeightPx / 2f);
        if (centerPosPx < 0) {
            centerPosPx = 0;
        }
        if (centerPosPy < 0) {
            centerPosPy = 0;
        }

        //determine the new first anchor column dependent of the center
position
        //and the remaining pixels as Dx
        int anchorCol1 = 0;
        for (int col = colStart; col <= (colEnd - 1); col++) {
            if (Math.round(sheet.getColumnWidthInPixels(col)) <
centerPosPx) {
                centerPosPx -=
Math.round(sheet.getColumnWidthInPixels(col));
                anchorCol1 = col + 1;
            } else {
                break;
            }
        }

        //determine the new first anchor row dependent of the center
position
        //and the remaining pixels as Dx
        int anchorRow1 = 0;
        for (int row = rowStart; row <= (rowEnd - 1); row++) {
            float cellHeight = (sheet.getRow(row).getHeightInPoints() *
Units.EMU_PER_POINT) / Units.EMU_PER_PIXEL;
            if (Math.round(cellHeight) < centerPosPy) {
                centerPosPy -= Math.round(cellHeight);
                anchorRow1 = row + 1;
            } else {
                break;
            }

        }

        //set the new upper left anchor position
        anchor.setCol1(anchorCol1);
        anchor.setRow1(anchorRow1);

        //set the remaining pixels up to the center position as Dx in unit
EMU
        anchor.setDx1(centerPosPx * Units.EMU_PER_PIXEL);
        anchor.setDy1(centerPosPy * Units.EMU_PER_PIXEL);

        //resize the pictutre to original size again
        //this will determine the new bottom rigth anchor position
        anchor.setDx2(-anchor.getDx1());
        anchor.setDy2(-anchor.getDy1());
        pict.resize();


*The output of SXSSWorkbook's Logo:*
[image: SXSSWorkbook_logo.png]

*The output of XSSWorkbook's Logo:*
[image: XSSWorkbook_lgo.png]

*Above mentioned logos have the same code but workbook types are different
only. Please suggest to me regarding SXSSFWorkbook's draw image issue.*

Dhaval K

Business Relationship Manager

Argusoft
+91-7567877390
[email protected]
Book An Appointment <https://www.cloudhq.net/meeting/[email protected]>


On Thu, Jan 5, 2023 at 2:35 PM Nick Burch <[email protected]> wrote:

> On Thu, 5 Jan 2023, Dhaval Kaushik wrote:
> > Also, this error only comes on using tomcat and java 17 version. The
> > apache-poi version is 3.14. On upgrading to a higher version it breaks
> some
> > existing code which is not correct. So is apache-poi not compatible with
> > java 17 in tomcat?
>
> Apache POI 3.14 was released in 2016 -
> https://poi.apache.org/devel/history/changes-3x.html#3.14
>
> The fact that a version that is almost 7 years old, and predates Java 17
> by almost as long, doesn't work perfectly isn't a huge surprise. You need
> to use a more modern release
>
> The latest Apache POI release is 5.2.3, you should try that. There are a
> lot of bug fixes in the last 7 years, see
> https://poi.apache.org/changes.html
> for a summary of the recent ones
>
> Nick
>

Reply via email to