(Java) replace image with another image

hello,
how will i changed the image in the document file with another image?..
and how will i set a paramter to the “image to be replaced” so that the it will be replaced by the right image…

i mean how will i set some paramteres so that replacing images with two “image placeholders” will be replaced by the right images that supposed to be replacing them…

e.g.
imageplaceholder1 will be replaced by image1 and imageplaceholder2 will be replaced by image2
and not imageplaceholder1 = image2 or imageplaceholder2 = image1

Please check the following thread in which it is described how to replace the image in the document:

<A href="</A></P> <P>Concerning finding the correct image there are several ways actually. The simplest way is to know the order of the image nodes in the template and substitute images in the correct order. Other way is to mark the images with a bookmark, then iterate through all images in the document and check what bookmark it belongs to, by searching <STRONG>BookmarkStart</STRONG> node using <STRONG>Node.PreviousSibling</STRONG> property.</P> <P>Yet another way of inserting images into a document is using textboxes with mail merges fields inside. That technique will work well for floating (not-inline) images.</P> <P>So I suggest that you try any of the above methods that you like most and let me know if you will need any assistance with any of them.</P> <P>Best regards,</P>

the thread seems to work for c# codes…
How will i iterate all of the images in a document and replacing the last image with another image?..

Other way is to mark
the images with a bookmark, then iterate through all images in the
document and check what bookmark it belongs to, by searching BookmarkStart node using Node.PreviousSibling property.

Can you tell me how to do it specifically?..
thanks…

Here is Java code to replace the last image in the document:

// Get all floating shapes in the document

NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);

Shape lastImage = null;

// Iterate through all floating shapes in the document

Iterator iterator = shapes.iterator();

while (iterator.hasNext())

{

Shape shape = (Shape) iterator.next();

// Check if the shape is an image

if (shape.getShapeType() == ShapeType.IMAGE)

lastImage = shape;

}

lastImage.getImageData().setImage("Aspose.Words.gif");

And here is the code to replace bookmarked image:

public void setBookmarkedImage(Document doc, String bookmarkName, String filename)

{

Bookmark bookmark = doc.getRange().getBookmarks().get(bookmarkName);

// Search nodes after bookmark start to find an image shape.

for (Node node = bookmark.getBookmarkStart(); (node != bookmark.getBookmarkEnd()) && (node != null); node = node.getNextSibling())

{

if (node.getNodeType() == NodeType.SHAPE)

{

Shape shape = (Shape)node;

if (shape.getShapeType() == ShapeType.IMAGE)

{

shape.getImageData().setImage(filename);

return;

}

}

}

}

Please let me know if it worked for you.

Best regards,

Thanks Vladimir!
Aspose support is the best!