From this answer regarding embedding an object into an Excel spreadsheet using Apache POI:
Workbook wb1 = new XSSFWorkbook();
Sheet sh = wb1.createSheet();
int picIdx = wb1.addPicture(getSamplePng(), Workbook.PICTURE_TYPE_PNG);
byte samplePPTX[] = getSamplePPT(true);
int oleIdx = wb1.addOlePackage(samplePPTX, "dummy.pptx", "dummy.pptx", "dummy.pptx");
Drawing<?> pat = sh.createDrawingPatriarch();
ClientAnchor anchor = pat.createAnchor(0, 0, 0, 0, 1, 1, 3, 6);
pat.createObjectData(anchor, oleIdx, picIdx);
This creates the embedded object with the image from getSamplePng()
as the preview image. This image is anchored to the cell at row = 1, column = 1, and stretches to row = 6, column = 3. Unless the image aspect ratio perfectly matches, this results in a distorted image.
The Picture
interface provides the resize
method, but there doesn't appear to be a way to get a Picture
instance from ObjectData
.
The following works if the image dimensions are within those of the cell it is being anchored to:
ClientAnchor anchor = drawing.createAnchor(0,
0,
imgDim.width * Units.EMU_PER_PIXEL,
imgDim.height * Units.EMU_PER_PIXEL,
columnIndex,
rowIndex,
columnIndex,
rowIndex);
This does not work if the image is larger than the cell dimensions; the image ends up stretched to the bounds of the cell.
I have also tried setAnchorType(AnchorType.MOVE_DONT_RESIZE)
, which doesn't appear to have any effect.
How can I size the preview image to its original size when using createObjectData
to embed an object?