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”);
dbT.moveToBookmark(“r”);
Bookmark templateBookmark = template.getRange().getBookmarks().get(“r”);
Bookmark sourceBookmark = source.getRange().getBookmarks().get(“source”);
templateBookmark.setText(sourceBookmark.getText());

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,

John

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:

http://www.aspose.com/community/forums/230061/issue-in-inserting-a-document-into-another-at-a-bookmark/showthread.aspx

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

Best regards,

Andrey,


Thank you very much, that is indeed useful.

Best regards,

John

Andrey,


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.

John

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");

dstDoc.Save("out.doc");

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

builder.MoveToBookmark(dstBookmark);

builder.Writeln();

// 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!