PDF with bookmarks

Hi Team,

Using Aspose.Words for Java, is there a way to create a PDF with bookmarks (TOC in the left pane)? See attached screenshot for details.

Thanks,
Kumar

Hi Kumar,

Thanks for your inquiry. Please use the OutlineOptions class to specify outline options like HeadingsOutlineLevels, ExpandedOutlineLevels, BookmarksOutlineLevels. Please check the members of this class from here:
http://www.aspose.com/docs/display/wordsjava/OutlineOptions+Members

Please check the following code examples for your kind reference. Hope this helps you.

Document doc = new Document();

// Create a document builder to insert content with into document.

DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a table of contents at the beginning of the document.

builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");

// Start the actual document content on the second page.

builder.insertBreak(BreakType.PAGE_BREAK);

// Build a document with complex structure by applying different heading styles thus creating TOC entries.

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);

builder.writeln("Heading 1");

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);

builder.writeln("Heading 1.1");

builder.writeln("Heading 1.2");

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);

builder.writeln("Heading 2");

builder.writeln("Heading 3");

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);

builder.writeln("Heading 3.1");

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3);

builder.writeln("Heading 3.1.1");

builder.writeln("Heading 3.1.2");

builder.writeln("Heading 3.1.3");

builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2);

builder.writeln("Heading 3.2");

builder.writeln("Heading 3.3");

// Call the method below to update the TOC.

doc.updateFields();

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();

pdfSaveOptions.getOutlineOptions().setHeadingsOutlineLevels(6);

pdfSaveOptions.getOutlineOptions().setExpandedOutlineLevels(9);

doc.save(MyDir + "Out.pdf", pdfSaveOptions);

Document doc = new Document();

// Create a document builder to insert content with into document.

DocumentBuilder builder = new DocumentBuilder(doc);

builder.startBookmark("MyBookmark");

builder.writeln("Text inside a bookmark.");

builder.startBookmark("NestedBookmark");

builder.writeln("Text inside a NestedBookmark.");

builder.endBookmark("NestedBookmark");

builder.writeln("Text after Nested Bookmark.");

builder.endBookmark("MyBookmark");

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();

pdfSaveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("MyBookmark", 1);

pdfSaveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("NestedBookmark", 2);

pdfSaveOptions.getOutlineOptions().setExpandedOutlineLevels(2);

doc.save(MyDir + "Out.pdf", pdfSaveOptions);


Thanks Tahir. This is exactly what I was looking for.

Hi Kumar,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.