You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@poi.apache.org by Ashish Vala <av...@argusoft.com.INVALID> on 2023/01/31 12:56:00 UTC

Apache POI functionality breaks in JAVA 17

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.*

*Thanks & Regards,*
Ashish Vala
Group Lead - Technology
Cell: +91-951-051-9036
Web: www.argusoft.com

[image: facebook] <https://www.facebook.com/argusoft>[image: Linkedin]
<https://www.linkedin.com/company/argusoft>[image: Twitter]
<https://twitter.com/argusoft> [image: argusoft logo]