-1

I've been working on this issue for a while now and I don't find any solution to it. I'm trying to replace a placeholderimage with another image, I found this code on another question and modified it a little but on execution the image field is blank with the text "cannot display image".

replaceImageById("rId5", "image1.jpeg", new File("src/main/resources/Bild1.png"), documentPart);

    public void replaceImageById(final String id,
                                 final String placeholderImageName,
                                 final File newImage, final MainDocumentPart document) throws Exception {
        Relationship rel = document.getRelationshipsPart().getRelationshipByID(id);

        BinaryPartAbstractImage imagePart = null;
        if (Arrays.asList(ContentTypes.EXTENSION_JPG_1, ContentTypes.EXTENSION_JPG_2).contains(FilenameUtils.getExtension(placeholderImageName).toLowerCase())) {
            imagePart = new ImageJpegPart(new PartName("/word/media/" + placeholderImageName));
        } else if (ContentTypes.EXTENSION_PNG.equals(FilenameUtils.getExtension(placeholderImageName).toLowerCase())) {
            imagePart = new ImagePngPart(new PartName("/word/media/" + placeholderImageName));
        }

        InputStream stream = new FileInputStream(newImage);
        imagePart.setBinaryData(stream);

        if (Arrays.asList(ContentTypes.EXTENSION_JPG_1, ContentTypes.EXTENSION_JPG_2).contains(FilenameUtils.getExtension(newImage.getName()).toLowerCase())) {
            imagePart.setContentType(new ContentType(ContentTypes.IMAGE_JPEG));
        } else if (ContentTypes.EXTENSION_PNG.equals(FilenameUtils.getExtension(newImage.getName()))) {
            imagePart.setContentType(new ContentType(ContentTypes.IMAGE_PNG));
        }

        imagePart.setRelationshipType(Namespaces.IMAGE);
        final String embedId = rel.getId();

        rel = document.addTargetPart(imagePart);
        rel.setId(embedId);
    }

I'm not really getting into docx4j and struggling to understand the relationship stuff, I guess thats where my issue might be.

Thanks in advance!

UPDATE I solved the replacing issue (I used the wrong relId) but now other images just randomly break. My target image is replaced but other images just stop working and show the "Image cant be displayed" text Also I noticed when checking the parts on http://webapp.docx4java.org/ only 2 image relationships are displayed but I have 5 images in the docx

Leevy
  • 1
  • 1
  • There's a typo in the first `else if`. It should probably read `ContentTypes.EXTENSION_PNG.equals(...)` and not `EXTENSION_JPG_1`. This will make the `imagePart` `null`. And execution of the code will crash with a `NullPointerException`. – Harald K Oct 11 '22 at 10:38

1 Answers1

0
    rel = document.addTargetPart(imagePart);
    rel.setId(embedId);

is bad.

In the code you posted, you are adding an image with AddPartBehaviour.OVERWRITE_IF_NAME_EXISTS then changing its relId so that you now have 2 rels with the same ID.

If you can use the same name (ie overwrite the existing part), you can use rel = document.addTargetPart(imagePart); /* don't change its ID */

The other approach would be to fetch the existing image part, then replace its contents using the setBinaryData method.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84