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

# How to get content bookmark

Hello,

I have a paragraph (three text lines). I put a bookmark (select a paragraph and put a bookmark).

And my problem is...how to get content bookmark??

Thanks for all.

Best regards,

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

Thanks for your inquiry. Please see the following link to learn how to work with bookmarks using Aspose.Words:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/bookmarks-in-aspose-words.html

string bkText = doc.FirstSection.Range.Bookmarks["BookmarkName"].Text;

Best regards,

Yes but...If the paragraph includes an image or an other element different a text?

Thanks for all.

Hello

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

Thank you for additional information. To retrieve content between BookmarkStart and BookmarkEnd you can use ExtractContentBetweenNodes method from this post:

Bookmark bookmark = srcDoc.Range.Bookmarks["BookmarkName"];

// Get node that contains BookmarkStart and BookmarkEnd

Node startParentNode = bookmark.BookmarkStart.ParentNode;

Node endParentNode = bookmark.BookmarkEnd.ParentNode;

ExtractContentBetweenNodes(startParentNode, endParentNode);

Best regards,

Thanks! This is a good solution for my problem :)

But now..I need to append this content after a specific paragraph in the other document.

I have seen in model objects document.appendDocument but the content always append at the end of the document.

Are there any solution for my problem?

Thanks for all.

Hi

Thanks for your inquiry. You can try using code provided in the following article to insert a document after a specific paragraph:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-insert-a-document-into-another-document.html

Hope this helps.

Best regards,

First of all, thanks for the speed of your responses.

But now, the problem is that the bookmark is into a cell and the function don't work for this case (only if the parent node is a body).

How to get a content bookmark if the bookmark is into a cell?

Thanks for all.

Hi

Thanks for your request. I modified the method a bit:

[Test]

public void Test001()

{

Document srcDoc = new Document(@"Test001\in.doc");

Document dst = ExtractContentFromBookmark(srcDoc, "test");

dst.Save(@"Test001\out.doc");

}

///

/// Extracts content of bookmark.

///

public Document ExtractContentFromBookmark(Document srcDoc, string bookmarkName)

{

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

if(bookmark == null)

throw new Exception("There is no bookmark with specified name");

Paragraph startNode = (Paragraph)bookmark.BookmarkStart.GetAncestor(NodeType.Paragraph);

Paragraph endNode = (Paragraph)bookmark.BookmarkEnd.GetAncestor(NodeType.Paragraph);

// Make sure that start and end nodes are children of the same story.

if (!startNode.ParentNode.Equals(endNode.ParentNode))

throw new Exception("Start and end nodes should be children of the same story");

// Clone the original document, this is needed to preserve styles of the original document

Document dstDoc = srcDoc.Clone();

dstDoc.RemoveAllChildren();

// The destination document should contain at lean one section.

dstDoc.EnsureMinimum();

dstDoc.FirstSection.Body.RemoveAllChildren();

// We will use NodeImporter to import nodes from one document to another.

NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles);

Node currNode = startNode;

Node dstNode;

// Copy content

while (currNode!=null && !currNode.Equals(endNode))

{

// Import node

dstNode = importer.ImportNode(currNode, true);

dstDoc.LastSection.Body.AppendChild(dstNode);

// Move to the next node

Node nextNode = currNode.NextSibling;

//Move to the next section

if (nextNode == null && currNode.ParentNode.NodeType == NodeType.Body)

{

Node sect = currNode.GetAncestor(NodeType.Section);

if (sect.NextSibling != null)

{

dstNode = importer.ImportNode(sect.NextSibling, true);

dstDoc.AppendChild(dstNode);

dstDoc.LastSection.Body.RemoveAllChildren();

nextNode = ((Section)sect.NextSibling).Body.FirstChild;

}

}

currNode = nextNode;

}

// If the bookmark end is the last child of the last paragraph we need to copy it as well.

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

{

dstNode = dstDoc.ImportNode(endNode, true, ImportFormatMode.UseDestinationStyles);

dstDoc.LastSection.Body.AppendChild(dstNode);

}

return dstDoc;

}

You are free to chenge this code if you need more flexibility or functionality. The method just demonstrates the technique how to can extract content from bookmark.

Best regards,

Hi,

thanks for this example but I test with my document but de content is blank. Return a document with 10 paragraphs but all paragraphs are blank strings.

I attach the document and the bookmark name is bk_13_MV.

Thanks for all.

Hi

Thank you for additional information. I cannot reproduce the problem on my side. Here is my code:

[Test]

public void Test001()

{

Document srcDoc = new Document(@"Test001\1.docx");

Document dst = ExtractContentFromBookmark(srcDoc, "bk_13_MV");

dst.Save(@"Test001\out.doc");

}

Attached is the output document.

Best regards,

Sorry,

the code works ok. The problem was another part of mi source code.

Thanks for all.

Best regards,

Hi

Best regards,

A very late answer, but since it was one of the first results of my request, here is a way to get the pointed paragraph by a given Bookmark (it’s VB.NET but you can easily translate it into C# language):

'  Here bm is a bookmark retrieved by its name. Adapt the code to your context
Dim bm As Words.Bookmark = doc.Range.Bookmarks(aBookMarkName)
Dim bmPAs Words.Paragraph =
CType(bm.BookmarkEnd.GetAncestor(Words.NodeType.Paragraph), Words.Paragraph)
Dim styleName As String = bmP.ParagraphFormat.StyleName
styleName))