0

I am getting the image position issue with setting the image for SXSSWorkbook. A similar code is working for XSSFWorkbook.

On using setDx1() and setDy1() to give position, the changes are not seen for SXSSFWorkbook.

    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;
    }

    //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();

I getting output as below :

enter image description here

But i need below as output, which we get in XSSFWorkbook but not in SXSSFWorkbook :

enter image description here

I used below dependencies :

   JAVA - 17
   TOMCAT - 9
   <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.3</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-full</artifactId>
        <version>5.2.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>4.1.2</version>
    </dependency>
Anant Doshi
  • 177
  • 1
  • 11

0 Answers0