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?
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!