@Nkorbl If your requirement is simply replace the image in the existing image shape, you can simply use ImageData.setImage. For example see the following code:
Document doc = new Document("C:\\Temp\\input.docx");
NodeCollection<Shape> shapes = (NodeCollection<Shape>)doc.getChildNodes(NodeType.SHAPE, true);
for (Shape shape : shapes)
{
if (shape.hasImage())
shape.getImageData().setImage("C:\\Temp\\imageB.png");
}
doc.save("C:\\Temp\\out.docx");
@Nkorbl Unfortunately, I cannot reproduce the problem on my side. Here is the output produced on my side using the above code and the latest 24.1 version of Aspose.Words for Java: out.docx (11.9 KB)
@Nkorbl I have inspected your output document and there are no images inside. Do you postprocess the document somehow on your side? Could you please attach output document produced on your side using 24.1 version?
@alexey.noskov I attached files including a screenshot of what I see. Concerning postprocessing I’m not doing anything else there is no additionnal code.
@Nkorbl It is really strange. I still cannot reproduce the problem on my side. For some reason the images are not present in your output document. If look inside document there is no reference to image in blip fill:
@alexey.noskov Yes really weird it is basically a one line code I don’t get it. I just rebuilt a project in another computer but same result. (Office 2016)
By the way I’m using Netbeans as IDE but I don’t think that is a useful information…
@Nkorbl Could you please create a simple application that will allow us to reproduce the problem on our side and attach it here? What is your environment where the problem occurs?
@Nkorbl Thank you for additional information. The problem is reproducible on my side. It looks like the image cannot be accessed from the project folder. The code works fine if read the image into byte array. Please try modifying the code like this:
Document doc = new Document("input\\" + "input.docx");
NodeCollection<Shape> shapes = (NodeCollection<Shape>)doc.getChildNodes(NodeType.SHAPE, true);
final String imageFileName = "input\\" + "imageB.png";
byte[] imageBytes = Files.readAllBytes(Paths.get(imageFileName));
for (Shape shape : shapes)
{
if (shape.hasImage())
{
shape.getImageData().setImageBytes(imageBytes);
}
}
doc.save("output\\" + "output.docx");