Trying to add multiple images to XSSFGroupShape
. I have tried the following(modified for simplicity):
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
private void addImagesToGroupShape(Sheet sheet, List<XSSFGroupImage> groupImages) throws Exception{
// XSSFGroupShape dimension
int dx1 = 180975;
int dy1 = 409575;
int dx2 = 219075;
int dy2 = 638175;
int row1 = 9;
int col1 = 8;
int row2 = 38;
int col2 = 19;
XSSFDrawing drawing = (XSSFDrawing) sheet.createDrawingPatriarch();
XSSFClientAnchor anchor = drawing.createAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
XSSFShapeGroup shapeGroup = drawing.createGroup(anchor);
// XSSFGroupImage is a custom class
// Included below
for (XSSFGroupImage groupImage : groupImages) {
byte[] imageData = Base64.getDecoder().decode(groupImage.data().getBytes());
int picType = 1;
switch (groupImage.extension()) {
case "jpeg":
picType = Workbook.PICTURE_TYPE_JPEG;
break;
case "png":
picType = Workbook.PICTURE_TYPE_PNG;
break;
case "emf":
picType = Workbook.PICTURE_TYPE_EMF;
break;
default:
var msg = "Extension not supported";
throw new Exception(msg);
}
int pictureIndex = sheet.getWorkbook().addPicture(imageData, picType);
// shapeGroup.createPicture(anchor, pictureIndex);
CTPicture ctPic = shapeGroup.getCTGroupShape().insertNewPic(pictureIndex);
CTTransform2D ct2D = ctPic.addNewSpPr().addNewXfrm();
ct2D.addNewOff();
ct2D.addNewExt();
ct2D.getOff().setX(groupImage.offsetX());
ct2D.getOff().setY(groupImage.offsetY());
ct2D.getExt().setCx(Long.parseLong(groupImage.extentsCx()));
ct2D.getExt().setCy(Long.parseLong(groupImage.extentsCy()));
XSSFChildAnchor childAnchor = new XSSFChildAnchor(Integer.parseInt(groupImage.offsetX()),
Integer.parseInt(groupImage.offsetY()), Integer.parseInt(groupImage.extentsCx()),
Integer.parseInt(groupImage.extentsCy()));
// shapeGroup.getCTGroupShape().addNewPic();
// shapeGroup.createPicture(childAnchor, picIndex);
// ctPic.addNewBlipFill().addNewBlip().setEmbed("");
}
}
@Accessors(fluent = true)
public class XSSFGroupImage {
@Getter
@Setter
private String data;
@Getter
@Setter
private String extension;
@Getter
@Setter
private String offsetX;
@Getter
@Setter
private String offsetY;
@Getter
@Setter
private String extentsCx;
@Getter
@Setter
private String extentsCy;
}
Added images outside group and retrieved properties and data of embedded images from group shape.
I understand the fact that an image cannot attached without an anchor
. However, I couldn't find a way of adding an XSSFChildAnchor
inside the XSSFGroupShape
that would allow setting of properites of CTTransform2D
object.
I also tried to add the image to the Workbook
using int pictureIndex = sheet.getWorkbook().addPicture(imageData, picType)
and use that index to set the CTPicture
using shapeGroup.getCTGroupShape().insertNewPic(pictureIndex)
. However, by doing this the relationId
that would point to that image is not being set resulting in no image being displayed.