Hello,
I am just trying to put a bookmark to surround the entire contents of a document. Here are two variants that I have tried:
static void AddBookMarksToDocument(Document doc, String bookmarkName)
{
int sectionCount = doc.Sections.Count;
if (sectionCount > 0)
{
var firstPara = doc.Sections[0].Body.FirstParagraph;
firstPara.PrependChild(new BookmarkStart(doc, bookmarkName));
var lasPara = doc.Sections[sectionCount - 1].Body.LastParagraph;
lasPara.AppendChild(new BookmarkEnd(doc, bookmarkName));
}
}
This works, but unfortunately if the document starts with a Table instead of a paragraph, the bookmark is added after the table instead of to the table. The second variant I tried:
static void SurroundDocumentWithBookmarks(Document doc, String bookmarkName)
{
int sectionCount = doc.Sections.Count;
if (sectionCount > 0)
{
var firstChild = doc.Sections[0].Body.FirstChild;
// Make sure that the node is either a paragraph or table.
if ((!firstChild.NodeType.Equals(NodeType.Paragraph)) &
(!firstChild.NodeType.Equals(NodeType.Table)))
{
throw new ArgumentException("The first thing in the document should be a paragraph or table.");
}
((CompositeNode)firstChild).PrependChild(new BookmarkStart(doc, bookmarkName));
//var firstRun = (firstPara.Runs.Count > 0) ? firstPara.Runs[0] : null;
// firstPara.InsertBefore(bs, firstRun);
var lastChild = doc.Sections[sectionCount - 1].Body.LastChild;
if ((!lastChild.NodeType.Equals(NodeType.Paragraph)) &
(!lastChild.NodeType.Equals(NodeType.Table)))
{
throw new ArgumentException("The last thing in the document should be a paragraph or table.");
}
((CompositeNode)lastChild).AppendChild(new BookmarkEnd(doc, bookmarkName));
}
}
Unfortunately this gives me “Cannot insert a node of this type at this location”
So I am a bit lost as to what I would have to do in order to bookmark the entire content of a document. Or failing that, a section. Any help appreciated.