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);
Paragraph startPara;
if (doc.FirstSection.Body.FirstChild.NodeType.Equals(NodeType.Table))
{
startPara = new Paragraph(doc);
startPara.ParagraphBreakFont.Size = 1;
startPara.ParagraphFormat.SpaceAfter = 0;
startPara.ParagraphFormat.SpaceBefore = 0;
startPara.Runs.Add(new Run(doc, ""));
doc.FirstSection.Body.FirstChild.ParentNode.InsertBefore(startPara, doc.FirstSection.Body.FirstChild);
}
else
{
startPara = doc.FirstSection.Body.FirstParagraph;
}
builder.MoveToDocumentEnd(); // or you can move cursor to any Node
BookmarkStart start = builder.StartBookmark("bm");
BookmarkEnd end = builder.EndBookmark("bm");
startPara.InsertBefore(start, startPara.FirstChild);
doc.Save(MyDir + @"15.10.0.docx");
Hope, this helps.
Best regards,