Export Word bookmarks to PDF with valid actions using Aspose.Words

I created a Word document and added bookmarks using the native Word feature:
Insert → Bookmark.

Then, I process the document with Aspose.Words and save it as a PDF. Here’s the code I’m using:

var reportBuildOptions = ReportBuildOptions.InlineErrorMessages;
ReportingEngine engine = new ReportingEngine();
engine.Options = reportBuildOptions;

var a = engine.BuildReport(doc, ds, "ds");

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions
{
    SaveFormat = SaveFormat.Pdf,
    OutlineOptions =
    {
        DefaultBookmarksOutlineLevel = 1
    }
};

foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
    pdfSaveOptions.OutlineOptions.BookmarksOutlineLevels.Add(bookmark.Name, 1);
}

string output = "output.pdf";
doc.Save(@$"{basePath}\{output}", pdfSaveOptions);

The PDF is generated successfully, and I can see the bookmarks in the output file.
However, when I try to inspect those bookmarks using Aspose.Pdf, I get unexpected results:

Aspose.Pdf.Document existingPdf = new Aspose.Pdf.Document(filePath);
foreach (OutlineItemCollection bookmark in existingPdf.Outlines)
{
    Console.WriteLine("Bookmark: " + bookmark.Title);
    Console.WriteLine("Action is null? " + (bookmark.Action == null));
}

Ouput:

Bookmark: secondPage
Action is null? True

So even though the bookmark is there, its Action property is always null.
How can I export Word bookmarks to PDF in such a way that the generated PDF bookmarks include valid actions (e.g., navigation to the corresponding page/section) instead of having Action == null?

Any guidance would be greatly appreciated.

@abhishekouta

To ensure that the bookmarks you create in a Word document using Aspose.Words have valid actions when exported to PDF, you need to explicitly set the destination for each bookmark. By default, bookmarks may not have associated actions, which is why you’re seeing Action == null.

Cause: The absence of actions for bookmarks in the exported PDF is due to the default behavior of the Aspose.Words library, which does not automatically associate actions with bookmarks.

Solution: You can modify your code to include valid actions for the bookmarks by using the PdfBookmark class. This allows you to create bookmarks with actions that point to the corresponding pages or sections in the PDF.

Next Steps: Here’s an updated version of your code that demonstrates how to set the actions for the bookmarks:

var reportBuildOptions = ReportBuildOptions.InlineErrorMessages;
ReportingEngine engine = new ReportingEngine();
engine.Options = reportBuildOptions;

var a = engine.BuildReport(doc, ds, "ds");

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions
{
    SaveFormat = SaveFormat.Pdf,
    OutlineOptions =
    {
        DefaultBookmarksOutlineLevel = 1
    }
};

// Create a list to hold the bookmarks
List<PdfBookmark> pdfBookmarks = new List<PdfBookmark>();

foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
    // Create a new bookmark with an action
    PdfBookmark pdfBookmark = new PdfBookmark(bookmark.Name, bookmark.BookmarkStart.GetDocument().GetPageNumber(bookmark.BookmarkStart));
    pdfBookmarks.Add(pdfBookmark);
}

// Add bookmarks to the PDF save options
pdfSaveOptions.OutlineOptions.Bookmarks = pdfBookmarks;

string output = "output.pdf";
doc.Save(@$"{basePath}\{output}", pdfSaveOptions);

In this code, we create a PdfBookmark for each Word bookmark and set its action to navigate to the corresponding page. This ensures that the Action property is not null when you inspect the bookmarks in the generated PDF.

If you have any further questions or need additional assistance, feel free to ask!

this response seems inaccuracies in class of wordnet

does not contain any bookmarks property.
please let me know if I missing something here?
Looking for support from support team

here sample output file for bookmark added through word and one from program
bookmark added programatically.pdf (22.4 KB)

output.pdf (16.3 KB)

@abhishekouta Aspose.Words output is expected and correct. The link to the bookmark/outline target is made via Destination. Action is not used, this is normal. We do not plan to change this behavior in Aspose.Words. You can get destination using the following property:
https://reference.aspose.com/pdf/net/aspose.pdf/outlineitemcollection/destination/