Saving document as Pdf doesn't export/generate PDF bookmarks

We are using Aspose.Words to generate a Word document and, following generation, save as a PDF. The expectation is that headings defined in the document and displayed in the table of contents will export to the PDF and users will be able to navigate to the appropriate sections in the PDF using the bookmarks in the left-hand navigation of the PDF viewer. Unfortunately, we’ve tried several approaches including using PdfSaveOptions as well as specifically adding BookmarkOutlineLevel, but none have worked. There appear to be a number of articles where users have experienced the same or a similar problem which seem to indicate that this should work, but thus far we haven’t found a solution. Any input would be much appreciated.

    protected Document Document { get; set; }
    protected DocumentBuilder Builder { get; set; }

    private void CreateDocument()
    {
        Document = new Document();
        Builder = new DocumentBuilder(Document);
        var pageSetup = Builder.PageSetup;
        pageSetup.PaperSize = PaperSize.A4;
        pageSetup.RightMargin = ConvertUtil.InchToPoint(0.75);
        pageSetup.LeftMargin = ConvertUtil.InchToPoint(0.75);

        DefineTableOfContents();
    }

    public void DefineTableOfContents()
    {
        Builder.CurrentParagraph.ParagraphFormat.Style = Document.Styles["TableOfContentHeader"];
        Builder.Writeln("Table of Contents");
        Builder.CurrentParagraph.ParagraphFormat.Style = Document.Styles[StyleIdentifier.Normal];

        Builder.InsertTableOfContents("\\o \"1-2\" \\h \\z \\u");
        Builder.InsertBreak(BreakType.PageBreak);
    }

    public void Save()
    {
        try
        {
            Document.UpdateFields();

             var options = new PdfSaveOptions { ExportDocumentStructure = true };
             foreach (var section in PasDocument.Questionnaire.Sections)
             {
                 options.OutlineOptions.BookmarksOutlineLevels.Add(section.Name, 1);
                 foreach (var group in section.Groups)
                 {
                     options.OutlineOptions.BookmarksOutlineLevels.Add(group.Name, 2);
                 }
             }

             options.OutlineOptions.ExpandedOutlineLevels = 2;
             Document.Save(FileName, SaveFormat.Pdf);
        }
        catch (Exception ex)
        {
            Globals.LogError(ex);
            throw;
        }
    }

Note that we are using Aspose.Words rather than Aspose.Pdf due to an issue in the PDF library wherein Html content does not get inserted properly. This issue does not exist in Aspose.Words with using the Builder.InsertHtml method.

@ajivener

In this case, you need to use OutlineOptions.HeadingsOutlineLevels property.

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Please attach the output PDF file that shows the undesired behavior.
  • Please attach the expected output PDF file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

We were finally able to address our issue with changes to OutlineOptions in our Save method.

public void Save()
{
    try
    {
        Document.UpdateFields();

         var options = new PdfSaveOptions { ExportDocumentStructure = true };
         options.OutlineOptions.ExpandedOutlineLevels = 2;
         options.OutlineOptions.HeadingsOutlineLevels = 2;
         options.OutlineOptions.DefaultBookmarksOutlineLevel = 2;
         options.OutlineOptions.CreateMissingOutlineLevels = true;
         options.SaveFormat = SaveFormat.Pdf;

         Document.Save(FileName, options);
    }
    catch (Exception ex)
    {
        Globals.LogError(ex);
        throw;
    }
}

@ajivener

It is nice to hear from you that you have found the solution of your query. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.