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

Free Support Forum - aspose.com

Bookmarking a range?

How can I take a Range and add a bookmark such that the contents of the Range are now fully inside the Bookmark start and end?


Hi Simon,


Thanks for your inquiry.You first need to determine the start and end nodes of the selected Range. For example, the following code can be used to bookmark whole content of document:

Document doc = new Document(MyDir + @“input.docx”);

DocumentBuilder builder = new DocumentBuilder(doc);<o:p></o:p>

<o:p> </o:p>

Paragraph startPara;<o:p></o:p>

if (doc.FirstSection.Body.FirstChild.NodeType.Equals(NodeType.Table))<o:p></o:p>

{<o:p></o:p>

startPara = new Paragraph(doc);<o:p></o:p>

startPara.ParagraphBreakFont.Size = 1;<o:p></o:p>

startPara.ParagraphFormat.SpaceAfter = 0;<o:p></o:p>

startPara.ParagraphFormat.SpaceBefore = 0;<o:p></o:p>

startPara.Runs.Add(new Run(doc, “”));<o:p></o:p>

<o:p> </o:p>

doc.FirstSection.Body.FirstChild.ParentNode.InsertBefore(startPara, doc.FirstSection.Body.FirstChild);<o:p></o:p>

}<o:p></o:p>

else<o:p></o:p>

{<o:p></o:p>

startPara = doc.FirstSection.Body.FirstParagraph;<o:p></o:p>

}<o:p></o:p>

<o:p> </o:p>

builder.MoveToDocumentEnd(); // or you can move cursor to any Node<o:p></o:p>

<o:p> </o:p>

BookmarkStart start = builder.StartBookmark(“bm”);<o:p></o:p>

BookmarkEnd end = builder.EndBookmark(“bm”);<o:p></o:p>

<o:p> </o:p>

startPara.InsertBefore(start, startPara.FirstChild);<o:p></o:p>

<o:p> </o:p>

doc.Save(MyDir + @“15.10.0.docx”);


Hope, this helps.

Best regards,