Bookmark missing after replace with other table


#1

Hi,
I have a .docx documents which contains a table with Bookmark “WI”.
When i upload the documents, i want to replace the entire table with new table while remaining the same bookmark . I tried below code, but the bookmark is gone after save. Could you please help on this?

		String templateUrl = "C://TEST";
		Document doc = new Document(templateUrl);	
		DocumentBuilder builder = new DocumentBuilder(doc);	
		
		builder.moveToBookmark("WI");
		Table newTable = builder.startTable();
		Bookmark bookmark = doc.getRange().getBookmarks().get("WI");			
		Table table = (Table) bookmark.getBookmarkStart().getAncestor(NodeType.TABLE);	

		builder.insertCell();
		builder.startBookmark("WI");
		
		/// continue insert content
		
		builder.endRow();
		builder.endTable();
		builder.endBookmark("WI");
		table.getParentNode().insertBefore(newTable, table);
		table.remove();
		
		String fileName = woNum + "_" + sdf.format(today)+ ".docx";
		String exportDir = tempFolder + fileName ;
		doc.save(exportDir);

#2

@WENDYTEE,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word documents
  • Aspose.Words generated output document showing the undesired behavior
  • Your expected document showing the correct output. You can create expected document by using MS Word. Please also list the steps that you performed in MS Word to create expected document.
  • Please also create a simplified standalone Java application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you more information. Thanks for your cooperation.

P.S. You can also upload the ZIP file to Dropbox and share the Download link here for testing.


#3

MissingBookmark.zip (40.0 KB)
Hi,
Please find the attached zip file as per requested.


#4

@WENDYTEE,

The problem occurs because the target Bookmark is part of the Table that you are removing. In this case, you need to reinsert the bookmark inside the new table by using the following code:

Document doc = new Document("E:\\MissingBookmark\\InputDocument.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Bookmark bookmark = doc.getRange().getBookmarks().get("WI");
Table table = (Table) bookmark.getBookmarkStart().getAncestor(NodeType.TABLE);
// bookmark.remove();

Table newTable = builder.startTable();
builder.insertCell();
builder.insertCell();
builder.endRow();
for(int i=1; i < 10; i ++) {
    builder.insertCell();
    builder.write("Row " + i);
    builder.insertCell();
    builder.insertCell();
    builder.endRow();
}
builder.endTable();

table.getParentNode().insertBefore(newTable, table);
table.remove();

BookmarkStart start = builder.startBookmark("WI");
BookmarkEnd end = builder.endBookmark("WI");

newTable.getFirstRow().getFirstCell().getFirstParagraph().insertBefore(start, newTable.getFirstRow().getFirstCell().getFirstParagraph().getFirstChild());
newTable.getLastRow().getLastCell().getLastParagraph().insertAfter(end, newTable.getLastRow().getLastCell().getLastParagraph().getLastChild());

doc.save("E:\\MissingBookmark\\awjava-19.7.docx");

Hope, this helps.