We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Inserting Aspose Document in bookmark Return Line problem


i have been trying to insert an Aspose document into a bookmark with the Aspose function insertDocument() given on the website.

(thisDoc is my main document)

String name=“source”;

Bookmarkbmark = thisDoc.getRange().getBookmarks().get(name);
BookmarkStart bookmarkStart = bmark.getBookmarkStart();
BookmarkEnd bookmarkEnd = bmark.getBookmarkEnd();

extractedNodesInclusive = extractContent(bookmarkStart,bookmarkEnd, false);

Document dstDoc = generateDocument(thisDoc,extractedNodesInclusive);



Bookmark newBmark = thisDoc.getRange().getBookmarks().get(name);

insertDocument(newBmark.getBookmarkStart().getParentNode(), dstDoc);

Well everything works as it should be! BUT, i always have a return line at the start of my bookmark…

Is there any way to insert my document inside my bookmark without having extra return lines at the start?

When i use newBmark.setText(“test”) instead of insertDocument i dont have those extra lines.

Thank you!

Hi Gilles,

Thanks for your inquiry.

Could you please attach your Word document(s) here for testing? I will investigate the issue on my side and provide you more information.

Best regards,

Hi and thank you for your fast response!

Here is 2 documents in the zip file
  1. J-Unit class i have built to show you how i use Aspose. So you will see and understand exactly what is happening in my code!
  2. The word document template used for the test.

After the code executed, you will see that both static and dynamic bookmarks have the same behavior!

Thank you again for your time!

I’ll be waiting for your solution.

Hi Gilles,

Thanks for your inquiry. We’re checking with this scenario and will get back to you soon.

Best regards,

Hi Gilles,

Thanks for your patience. I have logged a new task in our issue tracking system as WORDSNET-7947 for our development team to further look into the details of this problem. Your thread has also been linked to this task and you will be notified as soon as it is worked out. Sorry for the inconvenience.

Best regards,

Do you have any target date for that correction?

Thank you again!

Hi Gilles,

Thanks for your inquiry. We are currently doing analysis of this scenario; the problem appears to be more complex than we initially estimated. Please give us some time and we will get back to you asap with results. We apologize for any inconvenience.

Best regards,

Hi, first of all, thank you for your time and answers!

I guess it is not an easy one.

By the way, after working a bit more i found out that sometimes an extra return line will appear just before the end of the Bookmark too.
Maybe this information will help you out in your analysis.

Please let me know when you will have some kind of successful results.
Thank you again!

Hi Gilles,

Thanks for the additional information. We will keep you informed of the status of this task and let you know once it is completed.

Best regards,

Hi Gilles,

Thanks for waiting. I have taken a look into your issue and found the problem occurs because you are replacing the text within the destination bookmark but not removing the paragraph. This is what causes a line in your document to be left over, not either of the helper methods you are using.

The easiest way to solve this is to remove the parent paragraph of the bookmark by using the bookmark reference:

// remove the following line
// myDocument.getRange().replace(“destination”, “”, false, false);

//Insertion of the temporary doc inside the bookmark destination
insertDocument(destBmark.getBookmarkStart().getParentNode(), dstDoc);

// Remove the text and paragraph using this instead:

//We insert the same temporary document inside the second bookmark
insertDocument(des2tBmark.getBookmarkStart().getParentNode(), dstDoc);




First of all, thank you for the answers and the effort you put into this problem.
I have tried the solution you gave me for the extra return line problem.
In fact, it does work for the very simple .doc i have included on this thread.
But in my real project, i don’t use a simple document like that. My bookmarks are in paragraphs.

For testing, i have created a simple document, where the destination bookmark is included in some text, with the technique you gave me, the text is getting removed from the document, so i can’t use this.

Ill be waiting for you answer,
thank you!

Hi Gillies,

Thanks for this additional information.

I’m afraid there is a bit of confusion here with the expected behavior of your class. The empty paragraph in your test document is caused because the original destination paragraph is left behind, but you do not want to remove this paragraph because it might contain other content. There could be checks implemented so that the paragraph is removed if there is no other content but then the destination bookmark will also be removed so it can’t be used for processing with other bookmarks. Could you please clarify the expected behavior of your code regarding these points?

Additionally the way that the content is inserted that causes it to move to a new paragraph (the next line) might not be your expected output. If you could pass on all of your test cases along with the the input document and expected output we can gather a better picture of what you are trying to achieve.


Hi, thanks for the effort you put into the resolution of the problem.

As you requested, i have made an other test case for you.
The archive contains:
SimpleTester.java -> Class used for the generation
input.docx - > Word Template.
output.docx-> Output results.
excepted output - >Excepted output results.

In this case, i have represented the kind of thing that i do with the word documents.

For the first test i have used simple words (pierre,jean,paul) but it could have been a lot more complicated text, even paragraph with formatting. In this test case, i have not been able to put the desired data into the destination boomarks. The data gets injected after the bookmarks… You’ll be able to see it in the output.docx

Second test, a paragraph is located into a bookmark, followed by another bookmark. I try to copy the bookmark (p1) into the second bookmark (p1_copy). An extra return line separate the two paragraphs (return line is not present in the bookmark), but i want the two paragraph side by side.

Hope this example will be clear enough!

For any questions do not hesitate to contact me.
Thank you

Hi Gillies,

Thanks for posting your full tests here. What you are looking to do is insert the content of the source document inside the bookmark. I have included an additional method called insertDocumentAtBookmark in your code to achieve this. We also have plans to introduce built in functionality to insert a document at any location including a bookmark sometime in the future. I have linked your request to this issue in our internal database and you will be notified through a post in this thread as soon as this feature is implemented.

I have attached the full code for you to test to this forum post. There are two things to note, the first is that in your template I had to recreate your copy_side bookmark. This is because the way the bookmark appears causes it to fall inbetween two paragraph which is imported in incorrectly into the AW model. To prevent this make sure when you are inserting a bookmark to not highlight the paragraph break (turn on show formatting marks in Microsoft Word to see the paragraph break), and instead only highlight the text. This avoids this issue.

The second thing to note is that the p1 bookmark content that is duplicated at the end of the document includes an paragraph break in the result document. This is the correct behavior since the source bookmark spans over two paragraphs.

Hopefully this solves the issues for you, please let me know if there are any problems.


Hi and thank you again for your help.

I thought that your code would have solve my problem, but after integrating your insertDocumentAtBookmark function in my real code, i still could not get the excepted result.

After some testing i figured out how to reproduce the bug!

My archive contains:
InsertDocumentAtBookmarkWithRemoveTest.java -> Test class.
input_with_replace.docx -> Word template.
excepted output.docx - > Show the excepted result.
result.dox-> The real result.

So here what i’m doing.
  1. I have a bookmark with text inside (templating)
  2. I create a temporary document with the first bookmark (dstDoc)
  3. I create a new bookmark after the first bookmark (templating_copy)
  4. I replace some text at the start and end of the temporary document (dstDoc.getRange.Replace)
  5. I insert the document into the new bookmark (insertDocumentAtBookmark)
  6. Then I create a new bookmark after the second bookmark (templating_copy2)
  7. I create a new temporary document with the first bookmark (dstDoc)
  8. I replace some text at the start and end of the new temporary document (dstDoc.getRange.Replace)
  9. I insert the document into the third bookmark (insertDocumentAtBookmark)
  10. I remove the first bookmark (bookmark.remove())
Nothing really complicated here, but my 2 new bookmarks come with return line at their ends!.
You will see it on the result.docx.

Thank you again!

Hi Gillies,

Thanks for getting back to us so quickly.

I see the cause of the issue is when you are creating the copy of the bookmark you are inserting a blank paragraph: myDocumentBuilder.writeln("");. If you remove these lines from any part of code creating copies of bookmarks then the output is correct, including your original test document.

Also note you can most likely simplify your code by inserting the content of the source document directly after the original bookmark instead of creating a copy of the original bookmark. To do this you would need to pass the name of the original bookmark to the insertDocumentAtBookmark method and modify the second line of code in that method so that moveToBookmark takes the desired parameters to move to the end of the original bookmark like this: moveToBookmark(bookmarkName, false, true);. This this of course depends upon your full use case so you will need to decide if you want to do that.

Please let me know if there are any other problems.


Hi and thank you for your support.

We are now testing the code, for now, everything seems right!

I will confirm that when the solution will be approved by my client!
I’ll let you know if something goes wrong !

Thank you again.

Hi there,

That’s great it’s working as expecting now. Please let us know if you have any problems.


The issues you have found earlier (filed as WORDSNET-7947) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.

The issues you have found earlier (filed as WORDSNET-5251) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.