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.