Word to Pdf Conversion - how to set the outline level for the manually added bookmark?


I am trying to convert a word document to pdf. The word document TOC is not available in the Headings section of the source document, and hence this will NOT be available as pdf bookmark when its converted to pdf. When I try to manually bookmark the TOC using the below code:

public static List appendCaptionAsBookmarkToWordDoc(Document msword) throws Exception {
    List result = new ArrayList();
    DocumentBuilder db = new DocumentBuilder(msword);
    List tocTitle = getParagraphsByStyleName(msword, "TOC Title");
    String paraString = null;
    for (Paragraph paragraph : (Iterable) tocTitle) {
        paraString = paragraph.toString(com.aspose.words.SaveFormat.TEXT);
    return result;

public static List getParagraphsByStyleName(com.aspose.words.Document doc, String styleName) {
    List paragraphsWithStyle = new ArrayList();
    NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
    for (Paragraph paragraph : (Iterable) paragraphs) {
        if (paragraph.getParagraphFormat().getStyle().getName().equals(styleName))
    return paragraphsWithStyle;

Version used: aspose-words-15.6.0

The converted document contains a bookmark for TOC. But the if there is a heading before TOC in the word document, then the manually added bookmark becomes a child of previous pdf bookmark.

Please sugest if there is a way to set the outline level for the manually added bookmark?

Thanks in advance.

Hi Manohar,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.

  • Please create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate your output document

  • Please attach the output Pdf file that shows the undesired behavior.

Unfortunately, it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi Tahir Manzoor,

Thanks a lot for the quick reply.
Please find the attached zip document containing the word and converted pdf and the code used for conversion.

Thanks and Regards,

Hi Manohar,

Thanks for sharing the detail. Firstly, I suggest you please use OutlineOptions to specify outline options. See the following highlighted code snippet.

Secondly, please set the DefaultBookmarksOutlineLevel to 1 to get the required output.

Document msword = new Document(MyDir + "sample.docx");
java.util.List captionList = appendCaptionAsBookmarkToWordDoc(msword);
PdfSaveOptions saveOptions = new PdfSaveOptions();
msword.save(MyDir + "Out.pdf", saveOptions);

Hi Tahir Manzoor,

If the manually added bookmark lies at the middle of document headings, for example: if I manually bookmark a table which in the middle of section 3.1 and 3.2, then the manually added bookmark becomes the parent of 3.2 and the connectivity between section 3, 3.1 and 3.2 is lost. Please refer the screenshot attached.
Is there a work around for this issue.

Thanks in advance,

Hi Manohar,

Thanks for your inquiry. Please check Pdf save options provided by MS Word in attached image. You can only select one option at a time. As you are creating bookmarks for headings and Word bookmarks, you are getting the expected output. The sequence of bookmark is correct.

Moreover, please check the following code example about usage of OutlineOptions.BookmarksOutlineLevels. If bookmark level is not specified in this collection then DefaultBookmarksOutlineLevel value is used.

Document doc = new Document();
// Create a document builder to insert content with into document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Text inside a bookmark.");
builder.writeln("Text inside a NestedBookmark.");
builder.writeln("Text after Nested Bookmark.");
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("MyBookmark", 1);
pdfSaveOptions.getOutlineOptions().getBookmarksOutlineLevels().add("NestedBookmark", 2);
doc.save(MyDir  +"Out.pdf", pdfSaveOptions);