Bookmark in word document and convert the document to PDF

My requirement is to add a bookmark to a word document and then convert that to PDF. So using aspose.words i will bookmark the document and convert that to PDF. Or Do i need aspose.pdf to get the bookmark in the generated pdf?

Hi Showji,

Thanks for your inquiry. Yes, you can insert bookmark into Word document and convert it to Pdf document using Aspose.Words. Please use the following code example to achieve your requirements.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("Bookmark");
builder.writeln("Text inside a bookmark.");
builder.endBookmark("Bookmark");
doc.save(MyDir + "Out.pdf", SaveFormat.PDF);

Moreover, the DocumentBuilder has an internal cursor that you can navigate to a different location in a document using various DocumentBuilder.moveToXXX methods such as DocumentBuilder.moveToDocumentStart and DocumentBuilder.moveToField. After moving the cursor to a particular location, you can insert bookmark. Please read following documentation links for your kind reference.
https://docs.aspose.com/words/java/document-builder-overview/
https://docs.aspose.com/words/java/navigation-with-cursor/

Hi Tahir

Thank you for the response. I tried the code and generated the PDF. And when i open the pdf, i didn’t see any bookmark. Please review the attached PDF.

Here is the code i have tried

DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("Hello");
builder.writeln("Mark1");
builder.endBookmark("Hello");
builder.startBookmark("Apple");
builder.writeln("Mark2");
builder.endBookmark("Apple");

But the pdf contains statement like belo; Is the bookmark is not seen because i used the evaluation copy? Or i missed any library?
Evaluation Only. Created with Aspose.Words. Copyright 2003-2014 Aspose Pty Ltd.
Mark1
Mark2
Evaluation Only. Created with Aspose.Words. Copyright 2003-2014 Aspose Pty Ltd.

Regards
Showji

Hi Showji,

Thanks for your inquiry. Please note
that in evaluation mode there are some limitations applied. The
document’s content are truncated after a certain number of paragraphs
during import or export. To avoid this you can request a free 30-day
trial license which removes these evaluation restrictions. You can
request this from here:
https://purchase.aspose.com/temporary-license

Please use the OutlineOptions.BookmarksOutlineLevels property to achieve your requirements. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startBookmark("Hello");
builder.writeln("Mark1");
builder.endBookmark("Hello");
builder.startBookmark("Apple");
builder.writeln("Mark2");
builder.endBookmark("Apple");
PdfSaveOptions option = new PdfSaveOptions();
option.getOutlineOptions().setDefaultBookmarksOutlineLevel(3);
doc.save(MyDir + "Out.pdf", option);