Hi,
It seems that when I add a hidden bookmark in a Word document and I convert the word document to a pdf using the pdf saveformat option, it seems that it isn’t appearing in the same. Could you let me know how I can make these appear in the generated pdf document?
Hi Praneeth,
Thanks for your inquiry. Please use the OutlineOptions.DefaultBookmarksOutlineLevel property to Specify the default level in the document outline at which to display Word bookmarks. Individual bookmarks level could be specified using BookmarksOutlineLevels property.
Specify 0 and Word bookmarks will not be displayed in the document outline. Specify 1 and Word bookmarks will be displayed in the document outline at level 1; 2 for level 2 and so on. Default is 0. Valid range is 0 to 9.
Please check the following code example for your kind reference.
Document doc = new Document(MyDir + "in.docx");
PdfSaveOptions option = new PdfSaveOptions();
option.getOutlineOptions().setDefaultBookmarksOutlineLevel(2);
doc.save(MyDir + "Out.pdf", option);
Hey Tahir,
I’m not quite sure what is meant by the bookmark outline level in the document. In any case, I have tried to run the same code which you have provided (and changed the default level value from 0 through 9) and what I have found out is that the bookmarks which aren’t hidden do show up in the pdf, but the hidden bookmarks in word do not. PFA the word document and the pdf document. Could you please look into this?
Hi Praneeth,
Thanks for sharing the document. Please note that Aspose.Words mimics the same behavior as MS Word does. If you convert your document to Pdf using MS Word, you will get the same output. The hidden bookmarks are not exported in output Pdf file. The bookmarks that are created automatically by Word are hidden. Their names, which begin with an underscore character (_).
In your case, you can simply insert temporary Bookmarks inside those hidden Bookmarks before saving to PDF as follows:
Document doc = new Document(MyDir + "SampleDoc1.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
int i = 1;
for (Bookmark bm: doc.getRange().getBookmarks())
{
if (bm.getName().startsWith("_Hidden"))
{
builder.moveToBookmark(bm.getName(), false, false);
builder.startBookmark("Hidden Bookmark" + i);
builder.endBookmark("Hidden Bookmark" + i);
i++;
}
}
PdfSaveOptions option = new PdfSaveOptions();
option.getOutlineOptions().setDefaultBookmarksOutlineLevel(2);
doc.save(MyDir + "Out.pdf", option);