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

Hi,

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);
        db.moveTo(paragraph);
        db.startBookmark(paraString);
        db.endBookmark(paraString);
        result.add(paraString);
    }
    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))
            paragraphsWithStyle.add(paragraph);
    }
    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,
Manohar

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();
saveOptions.setCompliance(PdfCompliance.PDF_A_1_B);
saveOptions.getOutlineOptions().setDefaultBookmarksOutlineLevel(1);
saveOptions.getOutlineOptions().setHeadingsOutlineLevels(4);
saveOptions.getOutlineOptions().setExpandedOutlineLevels(9);
saveOptions.setCreateNoteHyperlinks(true);
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,
Manohar

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.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);