Getting error while creating the bookmarks with same name

Hi

We want to create certain categories and sub categories as bookmarks but, in our document subcategories can repeat within the categories.
For example : Category1 -> subCategory1, subCategory2
Category2 -> subCategory3, subCategory2

Since subCategory2 is repeated in between the category1 and category2, We are getting following errors.

We are getting following error

Caused by: java.lang.IllegalArgumentException: duplicate
	at asposewobfuscated.zz9O.zzK(Unknown Source)
	at com.aspose.words.BookmarksOutlineLevelCollection.add(Unknown Source).

We are using something similar to following code:

com.aspose.words.PdfSaveOptions options = new com.aspose.words.PdfSaveOptions();

builder.startBookmark("Bookmark 1");
//builder.writeln("hi");
builder.endBookmark("Bookmark 1");
options.getOutlineOptions().getBookmarksOutlineLevels().add("Bookmark 1", 1);

builder.startBookmark("nested bookmark 1");
//builder.writeln("hello");
builder.endBookmark("nested bookmark 1");

options.getOutlineOptions().getBookmarksOutlineLevels().add("nested bookmark 1", 2);

builder.startBookmark("nested bookmark 2");
//builder.writeln("hello");
builder.endBookmark("nested bookmark 2");

options.getOutlineOptions().getBookmarksOutlineLevels().add("nested bookmark 2", 2);

builder.startBookmark("Bookmark 2");
//builder.writeln("hi");
builder.endBookmark("Bookmark 2");
options.getOutlineOptions().getBookmarksOutlineLevels().add("Bookmark 2", 1);

builder.startBookmark("nested bookmark 3");
//builder.writeln("hello");
builder.endBookmark("nested bookmark 3");

options.getOutlineOptions().getBookmarksOutlineLevels().add("nested bookmark 3", 2);

builder.startBookmark("nested bookmark 2");
//builder.writeln("hello");
builder.endBookmark("nested bookmark 2");

options.getOutlineOptions().getBookmarksOutlineLevels().add("nested bookmark 2", 2);

We want the bookmark to match the name of the subCategory1. Please let us know if there is any other way to do that.

Thankyou.

@cvsformulary

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the expected output document.
  • Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hi
Please find attached Sample input, Expected Output and sample code that is giving error.
We are using Aspose words version 17.6. Please let us know if we have a solution for this issue with the same version.Sample BookMarks Option.zip (59.5 KB)

Thanks

@cvsformulary

Thanks for sharing the detail. It is by Microsoft Word design that a Word document must have unique names for bookmarks. You can not insert two bookmarks with the same names. In your case, before adding a new bookmark, you can check whether a bookmark with same name is already present in document. If yes, you can first remove the existing bookmark from document and add new one at a new location.

Please check the “nested bookmark 4” in the following code example.

Document doc = new Document();

com.aspose.words.PdfSaveOptions options = new com.aspose.words.PdfSaveOptions();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("Bookmark 1");
//builder.writeln("hi");
builder.endBookmark("Bookmark 1");
options.getOutlineOptions().getBookmarksOutlineLevels().add("Bookmark 1", 1);

builder.startBookmark("nested bookmark 1");
//builder.writeln("hello");
builder.endBookmark("nested bookmark 1");

options.getOutlineOptions().getBookmarksOutlineLevels().add("nested bookmark 1", 2);

builder.startBookmark("nested bookmark 2");
//builder.writeln("hello");
builder.endBookmark("nested bookmark 2");

options.getOutlineOptions().getBookmarksOutlineLevels().add("nested bookmark 2", 2);

builder.startBookmark("Bookmark 2");
//builder.writeln("hi");
builder.endBookmark("Bookmark 2");
options.getOutlineOptions().getBookmarksOutlineLevels().add("Bookmark 2", 1);

builder.startBookmark("nested bookmark 3");
//builder.writeln("hello");
builder.endBookmark("nested bookmark 3");

options.getOutlineOptions().getBookmarksOutlineLevels().add("nested bookmark 3", 2);

builder.startBookmark("nested bookmark 4");
//builder.writeln("hello");
builder.endBookmark("nested bookmark 4");

options.getOutlineOptions().getBookmarksOutlineLevels().add("nested bookmark 4", 2);

doc.save(MyDir + "18.8.pdf", options);

Thanks Tahir ,
I got your point. But I have one more question on bookmarks,

Documentation says: “Badly formed bookmarks or bookmarks with duplicate names will be ignored when the document is saved.” . Is there any definition for “Badly formed bookmarks”.
We came accross a scenario where if a bookmark starts with a space character, It is not created as bookmark.
Example:
builder.startBookmark(" MyBookmark");
builder.writeln(“Text inside a bookmark.”);
builder.endBookmark(" MyBookmark");
options.getOutlineOptions().getBookmarksOutlineLevels().add(" MyBookmark", 1);

" MyBookmark" was ignored on saving the document. Can you please let us know, other cases like these. We want to put validation in our code so that we donot put such characters in the text of the documents which needs to be created as bookmarks.

Thanks

@cvsformulary

Thanks for your inquiry.

Bookmark is a “facade” object that encapsulates two nodes BookmarkStart and BookmarkEnd in a document tree and allows to work with a bookmark as a single object. If you insert only BookmarkStart for a bookmark, it will be ignored when document is saved.

You only need to insert bookmarks with unique names. It is by Microsoft Word design that a Word document must have unique names for bookmarks.

Thanks Tahir for explaining this but if this is the case, then why if a bookmark has a leading space in front of it, it is getting ignored when document gets save. Please find attached Sample program for this.
Sample BookMarks Option.zip (55.1 KB).
This zip file has:

  • Sample java program
  • Sample input file
  • output file from the program
  • Expected output file.

Is there any restriction that bookmark cannot start with a leading space. Are there any other restrictions like that.

@cvsformulary

Thanks for sharing the detail. Please note that Aspose.Words mimics the behavior of MS Word. You cannot add space in the bookmark. The name for bookmark must follow these rules:

  • Name must begin with a letter of the alphabet.
  • Name can contain only letters, numbers, and the underscore.
  • Name cannot contain spaces or punctuation marks.

Thank you so much for sharing the rules and I agree that is the case for doc files and I saw MSWord does not allow these values but in our case we are using PdfSaveOptions to create the bookmarks in PDF file and we were able to create following types of bookmark :

“$MyBookmark” – Bookmark starting with special Character
“MyBookmark Book” – Bookmark with space in between
“MyBookmark? Book” – Bookmark with Special character and space in between
"MyBookmark " – Bookmark with Trailing spaces

Which works perfectly for us but only leading spaces is not working:
" MyBookmark"

So just wanted to check why only leading spaces is an exception or do we have any other exceptions that we should handle in our code before creating the bookmarks in PDF.

Please find sample program for the same.
Bookmark Example.zip (21.6 KB)

Thank you

@cvsformulary

Thanks for your inquiry. Aspose.Words replaces the space character with the underscore when document is saved to DOCX. However, when document is saved to PDF using Aspose.Words, the underscore is ignored.

In MS Word document, the hidden bookmark is started with an underscore character (_). Moreover, MS Word ignores the space character when the bookmark is started with space. This is the reason the leading spaces are not working in your case.

We have logged a feature request to ignore underscore at the start of bookmark when document is exported to PDF. The issue ID is WORDSNET-17555. We will inform you via this forum thread once this feature is available.

We apologize for your inconvenience.

@cvsformulary

Thanks for your patience. It is to inform you that we have closed this issue (WORDSNET-17555). Please note that Aspose.Words mimics the behavior of MS Word. It does not allow space and underscore as a leading bookmark character.