Bookmarks not being removed correctly

Hi,
I am currently evaluating Aspose.Words for Java by migrating the functionality an existing Word 2010 “dotm” template to that of a Java application. The existing “dotm” template contains a number of “bookmarks” and uses a Userform/VBA macro to selectively remove sections of text to eventually produce a formatted letter.
I have by and large converted the Word template to a Java program. However, I am coming across a problem in a particular section of the template when consecutive bookmarks are removed, instead of removing the text within the bookmark, text from the next bookmark are also removed. Looking at the resulting word document, it looks like the Java app has not only removed the required bookmark, but it has also removed the bookmark start of the next bookmark and as a result, the entire "next’ bookmark is somehow removed.
I’ve attached a stripped down dotm and Java program that demonstrate this issue, along with a VBA macro that demonstrate the correct behaviour (“testBookmarkProblem”). Can you have a look and let me know how I should be removing the bookmarks correctly?
Thanks,
Thomas

Hi Thomas,

Thanks for your inquiry. You are facing this issue because the BookmarkStart node of “COCClosing” comes before BookmarkEnd node of “PlacingSlipClosing”. Please check the attached DOM image for detail. In this case, I suggest you please use the following code example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "problem.dotm");
doc.getRange().getBookmarks().get("PlacingSlipClosing").setText("");
doc.getRange().getBookmarks().get("COCClosing").setText("");
doc.getRange().getBookmarks().get("PlacingSlipClosing").remove();
doc.getRange().getBookmarks().get("COCClosing").remove();
doc.save(MyDir + "Out.docm", SaveFormat.DOCM);

Hi Tahir,
Thank you for your suggestion. I’ve tried out your code, it did successfully remove the bookmarks, but there is still an issue.
The “PlacingSlipClosing” bookmark has been set the style “Section Intro” and this style has a “border” with aa cyan colour background (see supplied word doc). Using your approach of calling “.setText(”")" first on the 2 bookmarks clears the text, but does not clear the “Style” and after removing the bookmarks, the “Style” is now pushed into the next paragrah and now there is a “Cyan” colour in the first 2 lines of the “SlipsClosing” bookmark.
Also, looking closer at your analysis of my sample work document having bookmarks that are intertwined between paragraphs. At first, I thought it was accidentally created by our word template authors. However, I created a “clean” word doc using “=lorem(3,4)” (MS Word shortcuts to create dummy text) and then manually adding in the bookmarks (see newly attached sample - “remove consecutive bookmarks with style.7z”). I can see MS Word 2010 did not cross link the bookmarks and using the VBA code of:

ActiveDocument.Bookmarks("PARRA2").Select
Selection.Delete
ActiveDocument.Bookmarks("PARRA3").Select
Selection.Delete

I can successfully removed these 2 bookmarks and their associated text without issues.
Using the Aspose Document Viewer, I can see that in the situation where there are consecutive paragraphs are bookmarked, Aspose.Words somehow thinks these bookmarks overlaps each other. Hence when I use the Java Aspose.Words API to set the bookmark text to “” and then removing the bookmark, it removes both the target bookmark as well as the bookmark_start for the next paragrah and as a result, removing the second bookmark.
Can you investigate and provide a solution on how I can selectively remove bookmarks that are adjacent in consecutive paragraphs please?
Thanks,
Thomas

Hi Thomas,

Thanks for sharing the detail. Please use the following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "problem.dotm");
DocumentBuilder builder = new DocumentBuilder(doc);
Paragraph para = ((Paragraph)doc.getRange().getBookmarks().get("PlacingSlipClosing").getBookmarkStart().getParentNode());
// Solution 1
// Clear the paragraph formatting
// para.getParagraphFormat().clearFormatting();
// Solution 2
Node node = ((Paragraph)doc.getRange().getBookmarks().get("PlacingSlipClosing").getBookmarkStart().getParentNode()).getNextSibling();
if (node.getNodeType() == NodeType.PARAGRAPH)
{
    para.getParagraphFormat().setStyle(((Paragraph)node).getParagraphFormat().getStyle());
}
doc.getRange().getBookmarks().get("PlacingSlipClosing").setText("");
doc.getRange().getBookmarks().get("COCClosing").setText("");
doc.getRange().getBookmarks().get("PlacingSlipClosing").remove();
doc.getRange().getBookmarks().get("COCClosing").remove();
doc.save(MyDir + "Out.docm", SaveFormat.DOCM);

Hi Tahir,
Thank you for the code sample, I tested it against my test rig and it addressed my current issue.
Before closing this thread. Can you comment on why Aspose.Words sees consecutive bookmarks being intertwined, but MS Word does not? As it is, while your code does address my current issue, but the code for removing consecutive bookmarks is now specific to each Word document and is no longer generalised. I’ve over 200 MS Word templates with similar bookmark structure to convert. Can this be addressed in a future release? ie. align the bookmark behaviour to that of MS Word?
Regards,
Thomas

Hi Thomas,

Thanks for your inquiry. Please check the attached DOM image. The BookmarkStart of COCClosing comes before BookmarkEnd of PlacingSlipClosing. I have logged an issue as WORDSNET-10762 to check either Aspose.Words loads the bookmarks correctly or not. Our development team will investigate the issue and we will update you via this forum thread once there is any update available on this issue. We apologize for your inconvenience.

Hi Tahir,
Any updates from your dev team on their investigation?
Regards,
Thomas

Hi Thomas,

Thanks for your inquiry. I would like
to share with you that issues are addressed and resolved based on first
come first serve basis. Currently, your issue is pending for analysis
and is in the queue. We will update you via this forum thread once there
is any update available on your issue. Thank you for your patience and understanding.

Hi Tahir,
My trial Aspose license expired today and I still don’t have a viable solution in using Aspose.Words to convert my 200+ word templates containing consecutive “bookmarks”.
Perhaps, I was not clear before, I currently have 2 issues:

  1. The solution that you offered to solve the "deleting of inter-linked bookmarks requires custom coding over 200 individual Java programs with individual “hard-coded” references to delete specific book marks. My preferred solution is to develop a generic Java program that would accept a Collection of “bookmark” names and then loop thru and delete these bookmarks.
  2. Can you explain why Aspose.Words sees the bookmarks as being interlinked, while MS Words does not? Is this an Aspose issue?

Regards,
Thomas

Hi Thomas,

Thanks for your inquiry.

*tsiu:

  1. The solution that you offered to solve the "deleting of inter-linked bookmarks requires custom coding over 200 individual Java programs with individual “hard-coded” references to delete specific book marks. My preferred solution is to develop a generic Java program that would accept a Collection of “bookmark” names and then loop thru and delete these bookmarks.*

In your case, I suggest you please iterate through bookmark collection and set the text of bookmark to empty string. Please use Bookmarks.clear method before saving the document to removes all bookmarks from the document. Hope this helps you.

*tsiu:

  1. Can you explain why Aspose.Words sees the bookmarks as being interlinked, while MS Words does not? Is this an Aspose issue?*

This is an issue and we logged this issue as WORDSNET-10762. We
will update you via this forum thread once this issue is resolved. We apologize for your inconvenience.

Hi Thomas,

Thanks for your patience. Please note that Aspose.Words mimics the same behavior as MS Word does.

It is to inform you that our development team has completed the work on the issue (WORDSNET-10762)
and has come to a conclusion that this issue and the undesired behavior
you’re observing is actually not a bug in Aspose.Words. So, we have
closed this issue as ‘Not a Bug’.

The BookmarkStart of ‘COCClosing’ comes before BookmarkEnd of ‘PlacingSlipClosing’ inside the original document.xml. Please check the attached image for detail.

<w:bookmarkStart w:id="4" w:name="PlacingSlipClosing" />
…
…
…
<w:bookmarkStart w:id="5" w:name="COCClosing" />
<w:bookmarkEnd w:id="4" />