I'm hitting an issue with inserting multiple images (more than 10) to
either a group or a slide. I've written the following test case that fails
and shows the problem:
public void testCreateMultiplePictures() throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFGroupShape group1 = slide.createGroup();
for (int i = 0; i < 20; i++) {
byte[] data1 = new byte[]{1, 2, (byte)(i/8)};
int elementIndex = ppt.addPicture(data1,
XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape picture = group1.createPicture(elementIndex);
assertEquals("image" + (elementIndex + 1) + ".png",
picture.getPictureData().getFileName());
}
}
I can fix it, but I don't like the way I fixed it. What I'm finding is that
in the OPCPackage.getPartsByName method, the resulting list comes back in
alphabetically sorted name order (not index order). So, once you have an
image called image10.png, it shows up in the resulting list at a different
index and throws everything off. A hack that made things work was to modify
the createPicture function in both XSLFGroupShape and XSLFSheet, to look
like the following (this is from the XSLFGroupShape):
public XSLFPictureShape createPicture(int pictureIndex){
List<PackagePart> pics = _sheet.getPackagePart().getPackage()
.getPartsByName(Pattern.compile("/ppt/media/image" +
(pictureIndex + 1) + ".*?"));
PackagePart pic = pics.get(0);
PackageRelationship rel = _sheet.getPackagePart().addRelationship(
pic.getPartName(), TargetMode.INTERNAL,
XSLFRelation.IMAGES.getRelation());
XSLFPictureShape sh = getDrawing().createPicture(rel.getId());
sh.resize();
_shapes.add(sh);
return sh;
}
Not sure if this is known problem, though I didn't find anything in
bugzilla about it.
Has anyone hit this one before? Should I report this as a bug?
Thanks,
Mike