Hyperlink with bookmark is not working in pdf

Hi,

I am creating a word document and save word or pdf according to the requirement. In my document I am creating a table instead of table of content and want the title to point to corresponding description page. So I added the title as hyperlink with heading bookmark. This is working fine with word, not in pdf. attaching the sample code Test.zip (841 Bytes) and input doc test.docx (19.4 KB).
Normally table of contents are working fine in pdf. So kindly help me to figure out this issue.

Thank you

@Gptrnt It looks like the problem occurs because you are using hidden bookmarks. I have tested conversion your document to PDF using MS Word and it also does not export the hidden bookmarks properly to PDF.
As a workaround you can use regular bookmarks for your custom TOC. For example, the following code produces PDF with proper internal links:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Insert hyperlinks
builder.insertHyperlink("First", "bk1", true);
builder.writeln();
builder.insertHyperlink("Second", "bk2", true);
builder.writeln();

builder.insertBreak(BreakType.PAGE_BREAK);
builder.startBookmark("bk1");
builder.write("This is the first bookmark");
builder.endBookmark("bk1");
builder.writeln();
builder.insertBreak(BreakType.PAGE_BREAK);
builder.startBookmark("bk2");
builder.write("This is the second bookmark");
builder.endBookmark("bk2");

doc.save("C:\\Temp\\out.pdf");

Please let us know if such approach acceptable for you.

Hi Alexey,

I can’t use normal bookmark as it also comes in pdf bookmark. I can only added hidden bookmark, that doesn’t comes in the pdf bookmark list

Thanks

@Gptrnt Bookmarks are shown in the PDF bookmarks bar if Bookmarks Outline Level is greater than zero. If set zero outline level, bookmarks will not be shown in the bookmarks bar:

PdfSaveOptions options = new PdfSaveOptions();
options.getOutlineOptions().setDefaultBookmarksOutlineLevel(0);
doc.save("C:\\Temp\\out.pdf", options);

Actually zero is the default value of DefaultBookmarksOutlineLevel so you can simply do not set this property.

Hi,

I can’t completely remove all the bookmarks from the pdf bookmark bar. I added some other bookmarks intentionally to come in the bookmark bar. So is there any other way I can fix this issue?

Thank you

@Gptrnt You can specify outline level for individual bookmarks in your document. Please see OutlineOptions.BookmarksOutlineLevels property.