How to remove image file after insert it after a bookmark

Hi, I some some simple java code to insert an image in Word like this:

DocumentBuilder builder = new DocumentBuilder(doc); //doc is a existing Aspose Word object

		builder.moveToBookmark("ALJSignDateDec2", false, false);
		
		URL url = new URL(barcodeUrl); //hardcoded url value
		
		BufferedImage bufferedImage = ImageIO.read(url);  //this part works
		
		 
		builder.insertImage(bufferedImage ); //image inserted fine

                   ...

it works well as expected. But I need to find a way to delete the same image file later . My current code is like this:

Bookmark barcodeIamge = pkg.getRange().getBookmarks().get(“ALJSignDateDec2”);

   barcodeIamge.getBookmarkStart().getNextSibling().remove();

It always gives me null pointer exception no matter I try NextSibling, PreviousSibling, bookmarkStart or bookmarkEnd.

Do you have any solution?

Thanks.

@ligongzhu In your code cursor of DocumentBuilder is placed before the end of the bookmark. So if your bookmark is not empty, you should use the following code:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// This will make bookmark not empty (just for demonstration)
doc.getRange().getBookmarks().get("test").setText("TEXT");
// This line of code moves the cursor to the position before bookmark end.
builder.moveToBookmark("test", false, false);
builder.insertImage("C:\\Temp\\in.png");
doc.save("C:\\Temp\\out.docx");

// Now load the document back and remove the image.
Document doc1 = new Document("C:\\Temp\\out.docx");
// Remove the image
doc1.getRange().getBookmarks().get("test").getBookmarkEnd().getPreviousSibling().remove();
doc1.save("C:\\Temp\\result.docx");

in.docx (12.0 KB)
out.docx (14.3 KB)
result.docx (9.6 KB)

If your bookmark is empty (without text or content between bookmark start and end), you can simply set bookmark text to empty string to remove the image:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// This line of code moves the cursor to the position before bookmark end.
builder.moveToBookmark("test", false, false);
builder.insertImage("C:\\Temp\\in.png");
doc.save("C:\\Temp\\out.docx");

// Now load the document back and remove the image.
Document doc1 = new Document("C:\\Temp\\out.docx");
// Remove the image
doc1.getRange().getBookmarks().get("test").setText("");
doc1.save("C:\\Temp\\result.docx");

Hi,

Your code works fine and a barcode is removed. But the problem now with both ways is that the bookmark is also removed with the image so it can’t insert the image again next time because the bookmark can’t be found now. Is there any solution to ?

Thanks.

@ligongzhu Both code example do not remove bookmark and it should be preserved in the result document. You can check the result.docx document.
Could you please attach your document and code that will allow us to reproduce the problem? We will check and provide you more information.

I created a new bookmark then tried again and it works as you described. Thanks a lot!

1 Like