Bookmark is removed when appendDocument() is called in Java

Hello,

I am using current version of Aspose Words for Java. In my code, I try to append an existing MS Word document with a bookmark with another document which also has a bookmark. My code looks like this:

firstDoc.appendDocument(nextDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);

I verified that "firstDoc" as well as "nextDoc" both have bookmarks prior to the above mentioned method call. However, when I open the updated "firstDoc" (the combined document) after the method call, the second bookmark found missing which is supposed to be from the "nextDoc".

In short, after the method call, only one bookmark is availble.

Hi
Vinit,


Thanks for your inquiry, Most likely this problem occurs because the names of bookmarks are the same in both of your documents. If this is found to be the case, please try giving unique bookmark names in both firstDoc and nextDoc.

I hope, this will help.

Best Regards,

Hello Awais,

Thank you for your prompt reply. In my application, I try to generate multiple copies of the documents from MS Word template (using mail merge), the data comes from the CSV file. The CSV file contains field names in the first row and subsequent rows containing values for the field name. eg. if the CSV file has five rows (first row for the field name and remaining four rows for the values) then my application supposed to generate four copies. I use appendDocument() to combine the firstly generated document with the next document and so on. So, at the end there will be just one unified document containing four separate documents. Since the template is common, so the bookmark name will be the same. I hope, I was able to explain the scenario here.
Thanks,

Hi
Vinit,


Thank you for the additional information.

It is by MS WORD design that a Word document must have unique names for bookmarks. You can not insert two bookmarks with the same names. I think, in your case, you can simply try renaming bookmarks during mail merge (before appending documents). Moreover, please see the following code snippet to be able to rename bookmarks programatically:

Document
doc = new Document(MyDir
  • “Bookmark.doc”);

// Use the indexer of the Bookmarks collection to obtain the
desired bookmark.
Bookmark
bookmark = doc.Range.Bookmarks[“MyBookmark”];

// Get the name and text of the bookmark.
string
name = bookmark.Name;
string
text = bookmark.Text;

// Set the name and text of the bookmark.
bookmark.Name
=
“RenamedBookmark”;
bookmark.Text
=
“This is a new bookmarked text.”;

If we can help you with anything else, please feel free to ask.

Best Regards,

Thank you so much Awais for your prompt help. I was able to append document by temporarily renaming bookmark name.


Great support! You are just awesome !!