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

Free Support Forum - aspose.com

Adding a bookmark around the entire contents of a document

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.

Hi Simon,

Thanks for your inquiry. You can build on the following code to be able 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();
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,