Warm tip: This article is reproduced from stackoverflow.com, please click
itext7

iText7 ViewerPreferences: Should display Bookmarks by default

发布于 2020-04-16 12:14:50

I create a PDF with iText7, which should not show the toolbar. But it should display the area with the bookmarks/outlines.

PdfViewerPreferences pref = new PdfViewerPreferences();
pref.SetHideToolbar(true); // works
pref.SetNonFullScreenPageMode(PdfViewerPreferences.PdfViewerPreferencesConstants.USE_OUTLINES); // doesn't work
pdfDocument.GetCatalog().SetViewerPreferences(pref);

Acrobat Reader should display it this way: Should look like this

Questioner
Penguin
Viewed
35
Alexey Subach 2020-02-05 02:51

Documentation of SetNonFullScreenPageMode clearly says the following:

This entry is meaningful only if the value of the PageMode entry in the Catalog dictionary is FullScreen

Thus you need to also add the following line in order for your configuration to become active:

pdfDocument.GetCatalog().SetPageMode(PdfName.FullScreen);

However, this will make Acrobat ask you to enter full screen mode on opening the PDF and in full screen mode nothing is shown but the page content.

What you are really looking for is setting PageMode to UseOutlines:

pdfDocument.GetCatalog().SetPageMode(PdfName.UseOutlines);

Full code:

PdfViewerPreferences pref = new PdfViewerPreferences();
pref.SetHideToolbar(true);
pdfDocument.GetCatalog().SetViewerPreferences(pref);
pdfDocument.GetCatalog().SetPageMode(PdfName.UseOutlines);