Setting Bookmarks- MoveToParagraph

I am processing a Word document to create a pdf document. One of the things that I thought I had to do was create the bookmarks for the Bookmarks list in the pdf.

So, I created this routine:

Protected Function setAllBookmarks(ByRef oWord As Document) As NodeCollection

Dim oBuilder As DocumentBuilder

Dim oParas As NodeCollection

Dim oPara As Paragraph

Dim sBook As String

Dim i As Integer

Trace.Warn("Doc sections: " + oWord.Sections.Count.ToString())

oBuilder = New DocumentBuilder(oWord)

oParas = oWord.GetChildNodes(NodeType.Paragraph, True)

For i = 0 To oParas.Count - 1

oPara = oParas(i)

If (oPara.ParagraphFormat.StyleName = "Title" Or oPara.ParagraphFormat.IsHeading = True) Then

sBook = oPara.ToTxt().TrimEnd()

Trace.Warn("Bookmark -" + sBook + "- p" + i.ToString())

Try

oBuilder.MoveToParagraph(i, 0)

oBuilder.StartBookmark(sBook)

oBuilder.MoveToParagraph(i, -1)

oBuilder.EndBookmark(sBook)

Catch ex As Exception

Trace.Warn("Caught - " + ex.Message)

Trace.Warn(" Para " + i.ToString() + " of " + oParas.Count.ToString() + " index = " + oParas.IndexOf(oPara).ToString())

End Try

End If

Next

Return oParas

End Function

When I did this, I got some errors in my trace:

Doc sections: 1
Bookmark -General- p21
Bookmark -Microsoft Word- p23
Bookmark -Copy and Paste- p26
Bookmark -Hot Keys- p35
Bookmark -Copy- p38
Bookmark -Paste- p40
Bookmark -Print Screen- p42
Bookmark -Save- p44
Bookmark -Find (Search Text)- p47
Bookmark -Creating a Save Folder- p49
Bookmark -Adobe Reader- p66
Bookmark -View and Search a PDF File- p75
Bookmark -Find Information in a PDF Portfolio or File- p78
Bookmark -Cursors- p85
Bookmark -Editing Tools- p90
Bookmark -Sticky Note- p92
Bookmark -Highlight Text- p95
Bookmark -Arrow Tool- p98
Caught - Specified argument was out of the range of valid values. Parameter name: paraIdx
Para 98 of 113 index = 98
Bookmark -Icons- p101
Caught - Specified argument was out of the range of valid values. Parameter name: paraIdx
Para 101 of 113 index = 101
Bookmark -Conclusion- p104
Caught - Specified argument was out of the range of valid values. Parameter name: paraIdx
Para 104 of 113 index = 104

Notice that paragraph 98 and above errored in MoveToParagraph. The trace indicates that oParas still has 113 paragraphs, but apparently MoveToParagraph does not see that many.

Any idea why this is failing?

Since I was still getting all the bookmarks in the pdf in spite of the error, I commented out the function; I still got all the bookmarks. It looks like they may be automatically generated when outputting the pdf and I may not need this routine at all.

Is that true? (Still would like to know why the MoveToParagraph fails, though).

Thanks

Hi Brad,

Thanks for your inquiry.

I think you can use the PdfSaveOptions.HeadingsOutlineLevels option instead. This will add each heading paragraph as an entry in the overview of the PDF which I suspect is what you are looking for.

Regarding the exception issue, could you please attach your document here for testing?

Thanks,