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

Free Support Forum - aspose.com

Getting the full text + formatting of a bookmark

Hello all,

We are looking at Aspose as a way to enhance a current document generation process that we have that involves taking a template document with bookmarks used to delineate replaceable text and programatically replacing those bookmarks with text from various sources.

One of our use cases involves replacing the bookmarked text in the template with the text (including formatting) contained in a bookmark in another document.

I’ve used something like this (in Java, but you can reply in whatever language you like):

Document template = new Document(“c:\tmp\template.doc”);
Document source = new Document(“c:\tmp\source.doc”);
Bookmark templateBookmark = template.getRange().getBookmarks().get(“r”);
Bookmark sourceBookmark = source.getRange().getBookmarks().get(“source”);

This works mostly fine (it inserts the text, including breaks & etc), but it does not bring over the formatting of the text in the “source” doc.

Am I on the right track? How can I do what I am requesting?

I appreciate any insight you can provide (even an RTFM response is much appreciated, as I have no real Aspose experience yet).

Best regards,


Hello John,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think the code provided in the following thread will be useful for you:


Also, you should note, getText method returns just a simple text.

Best regards,


Thank you very much, that is indeed useful.

Best regards,



After having digested that, I do have a subsequent question. The example you showed demonstrated inserting the entire source document into a bookmark in the target. What I want to do is to simply insert some bookmarked text from the source. How can I iterate over the objects/nodes/etc that are contained in a bookmark; I don’t see any way of doing that in the API.


Hi John,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Please try using the following code:

Document srcDoc = new Document("src.doc");

Document dstDoc = new Document("dst.doc");

CopyBookmarkContent(srcDoc, "Test1", dstDoc, "Test2");


private static void CopyBookmarkContent(Document srcDoc, String srcBookmark, Document dstDoc, String dstBookmark)


// Clear dst bookmark

dstDoc.Range.Bookmarks[dstBookmark].Text = string.Empty;

Bookmark bookmark = srcDoc.Range.Bookmarks[srcBookmark];

// Get node that contains BookmarkStart and BookmarkEnd

Node currentNode = bookmark.BookmarkStart.ParentNode;

Node endNode = bookmark.BookmarkEnd.ParentNode;

DocumentBuilder builder = new DocumentBuilder(dstDoc);

// Move cursor to bookmark and insert paragraph break



// Content of srcdoc will be inserted after this node

Node insertAfterNode = builder.CurrentParagraph.PreviousSibling;

// We will be inserting into the parent of the destination paragraph.

CompositeNode dstStory = insertAfterNode.ParentNode;

// Import nodes between BookmarkStart and BookmarkEnd Parent nodes

while (currentNode != endNode)


Node newNode = dstDoc.ImportNode(currentNode, true, ImportFormatMode.UseDestinationStyles);

dstStory.InsertAfter(newNode, insertAfterNode);

currentNode = currentNode.NextSibling;


if ((endNode as CompositeNode).LastChild.Equals(bookmark.BookmarkEnd))


dstStory.InsertAfter(dstDoc.ImportNode(endNode, true, ImportFormatMode.UseDestinationStyles), insertAfterNode);



Best regards,

That’s absolutely brilliant, thank you Andrey!