Free Support Forum - aspose.com

Convert Nested Bookmarks in Word Document to PDF Bookmark links using C# .NET

I am not quite able to get my bookmarks to show up when I save as a PDF. (Or Word, but we really want them in PDF.)

I am adding bookmarks via a find and replace. Here is the code in question:

            var basicAiText = this.agendaItem.AgendaItemNameNoHtml();
            docBuilder.InsertField("TC \"" + basicAiText + "\" \\f t");

            var bmStart = docBuilder.StartBookmark(basicAiText);
            docBuilder.InsertHtml(replacementHTML, true);
            var bmEnd = docBuilder.EndBookmark(basicAiText);

            List<Run> runs = this.getMatchingRuns(args);
            foreach (Run run in runs)
            {
                run.Remove();
            }

            return ReplaceAction.Skip;

The insert works and the table of contents works. But I don’t see any bookmarks in the PDF.

When I check in code, there are 17 (or 34) bookmarks. It is 17 before I do these calls:

            docBuilder.Document.UpdateFields();
            docBuilder.Document.UpdatePageLayout();

And it shows 34 afterwards. And my next call is directly to save:

docBuilder.Document.Save(filename + ".pdf");

But when I check the PDF, I don’t see any of these bookmarks.

2020-01-16 16-52-01.792.pdf (75.4 KB)

image.png (10.2 KB)

What am I doing wrong?

@jsierks,

To show Bookmarks of Word Document inside generated PDF file, please use the following Aspose.Words for .NET code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartBookmark("My Bookmark");
builder.Writeln("Text inside a bookmark.");
builder.StartBookmark("Nested Bookmark");
builder.Writeln("Text inside a Nested Bookmark.");
builder.EndBookmark("Nested Bookmark");
builder.Writeln("Text after Nested Bookmark.");
builder.EndBookmark("My Bookmark");
PdfSaveOptions options = new PdfSaveOptions();
options.OutlineOptions.DefaultBookmarksOutlineLevel = 9;
// or use below lines
//options.OutlineOptions.BookmarksOutlineLevels.Add("My Bookmark", 1);
//options.OutlineOptions.BookmarksOutlineLevels.Add("Nested Bookmark", 2);
doc.Save("E:\\Temp\\20.1.pdf", options);

Thank you, that was it!

Now, how about DOC and DOCX files? They’re not as important, but are occasionally needed.

@jsierks,

Yes, it is possible to bookmark content inside DOC or DOCX Word documents. Please see the following example code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.StartBookmark("My Bookmark");
builder.Writeln("Text inside a bookmark.");
builder.StartBookmark("Nested Bookmark");
builder.Writeln("Text inside a Nested Bookmark.");
builder.EndBookmark("Nested Bookmark");
builder.Writeln("Text after Nested Bookmark.");
builder.EndBookmark("My Bookmark");
doc.Save("E:\\Temp\\20.1.doc");
doc.Save("E:\\Temp\\20.1.docx");

Thanks again. We were already doing that - I just expected Word to make them easier to find.

@jsierks,

In case you may have further inquiries or need any help in future, please let us know.