Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting XSSFPictures via the sheet DrawingPatriarch does not properly expose the PIctureData #180

Open
archine opened this issue Apr 17, 2023 · 6 comments

Comments

@archine
Copy link

archine commented Apr 17, 2023

As shown in the picture below
image

workbook.getAllPictures gets the image data, but it doesn't seem to match a cell. So I went through the sheet's getDrawingPatriarch to get the image, but I couldn't read the image

@pjfanning
Copy link
Owner

Can you provide a reproducible test case?

@archine
Copy link
Author

archine commented Apr 17, 2023

The process is as follows: excel cell inserts an image and reads this image when importing. I need to get the image data and the coordinates of the cell where the image is located. then. I did not find a correlation function with an anchor in the XlsxPictureData object

image

image

@archine
Copy link
Author

archine commented Apr 17, 2023

This method returns null. so, I can't get the data of the image, but I can get anchor and shapeName

    /**
     * Return picture data for this shape
     *
     * @return picture data for this shape
     */
    @Override
    public XSSFPictureData getPictureData() {
        String blipId = ctPicture.getBlipFill().getBlip().getEmbed();
        return  (XSSFPictureData)getDrawing().getRelationById(blipId);
    }

excel template I used for testing

image

@pjfanning
Copy link
Owner

pjfanning commented Apr 17, 2023

What I mean by a reproducible test case is providing an xlsx file that shows the issue and the code you are using. There are a couple of test cases in this project that show this code works for at least some xlsx files.

@archine
Copy link
Author

archine commented Apr 17, 2023

example:

public static void main(String[] args) throws FileNotFoundException {
        InputStream is = new FileInputStream("E:\\IdeaProjects\\demo\\test.xlsx");
        Workbook workbook = StreamingReader.builder()
                .rowCacheSize(100)
                .bufferSize(4096)
                .setReadShapes(true)
                .open(is);

        Sheet sheet = workbook.getSheetAt(0);
        Drawing<?> shapes = sheet.getDrawingPatriarch();
        for (Shape shape : shapes) {
            System.out.println();
            XSSFPicture picture = (XSSFPicture) shape;
            System.out.println("picture data => " + picture.getPictureData()); // is null

            System.out.println(picture.getClientAnchor().getRow1() + "-" + picture.getClientAnchor().getCol1()); // available
        }
    }

file:
test.xlsx

@pjfanning
Copy link
Owner

This approach has not been a priority. You must note that excel-streaming-reader does not offer 100% support for everything you can do with POI's XSSF code base.

It may be possible to get it to work but it doesn't work at the moment. This is the approach that has been prioritised and there are tests to show it working

      List<? extends PictureData> pictureList = workbook.getAllPictures();
      assertEquals(5, pictureList.size());
      for(PictureData picture : pictureList) {
        XlsxPictureData xlsxPictureData = (XlsxPictureData)picture;
        assertTrue("picture data is not empty", picture.getData().length > 0);
        assertArrayEquals(picture.getData(), IOUtils.toByteArray(xlsxPictureData.getInputStream()));
      }

So you can get the picture data that way. You can use the code that you are using to get the anchor details. At some stage in the future, I can look at seeing if the XSSFPictures retrieved via the DrawingPatriarch can be made to properly expose the picture data.

If you want to investigate the issue and submit a PR, I will take a look at it.

@pjfanning pjfanning changed the title ((XSSFPicture) e).getPictureData() is null Getting XSSFPictures via the sheet DrawingPatriarch does not properly expose the PIctureData Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants