How to set initial view properties?

I want to set the properties available under the Initial View tab in Adobe Acrobat for an existing PDF programmatically. Document Options: Show = Bookmarks Panel and Page Page Layout = Contin

I want to set the properties available under the Initial View tab in Adobe Acrobat for an existing PDF programmatically.

Document Options:

  • Show = Bookmarks Panel and Page
  • Page Layout = Continuous
  • Magnification = Fit Width
  • Open to Page number = 1

Window Options:

  • Show = Document Title

I tried to achieve this using the following code:

PdfStamper stamper =
    new PdfStamper(reader, new FileStream(dPDFFile, FileMode.Create));
stamper.AddViewerPreference(PdfName.DISPLAYDOCTITLE, new PdfBoolean(true));
the above code is used to set the document title show, but the following code is not working:

// For page layout
stamper.AddViewerPreference(PdfName.PAGELAYOUT, new PdfName("OneColumn"));
// For Bookmarks Panel and Page:
stamper.AddViewerPreference(PdfName. PageMode, new PdfName("UseOutlines"));
Finally I also want to set the language to English. The PS Script to do this looks like this [ {Catalog} > /PUT pdfmark. How is it done in PDF?

Posted on StackOverflow on Jun 23, 2014 by Thirusanguraja Venkatesan

In iText 7 when you have a PdfDocument instance named pdfDoc, you can set the Viewer preferences like this:

pdfDoc.getCatalog().setPageLayout(pageLayoutMode);

In this case, the pageLayoutMode is a value that can have one of the following values:

  • PdfName.SinglePage

  • PdfName.OneColumn

  • PdfName.TwoColumnLeft

  • PdfName.TwoColumnRight

  • PdfName.TwoPageLeft

  • PdfName.TwoPageRight

See the PageLayoutExample for more info.

But in some cases, it's just easier to use:

pdfDoc.getCatalog().setViewerPreferences(viewerPreferences);

Where viewerPreferences is a PdfViewerPreferences object. You can set the required parameters using boolean values or constants from PdfViewerPreferences.PdfViewerPreferencesConstants as is shown in the ViewerPreferencesExample:

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfViewerPreferences viewerPreferences = pdfDoc.getCatalog().getViewerPreferences();
if (viewerPreferences == null) {
    viewerPreferences = new PdfViewerPreferences();
    pdfDoc.getCatalog().setViewerPreferences(viewerPreferences);
}
viewerPreferences.setDuplex(PdfViewerPreferences.PdfViewerPreferencesConstants.DUPLEX_FLIP_LONG_EDGE);
pdfDoc.close();

As for setting the language, this is done like this:

pdfDoc.getCatalog().put(PdfName.Lang, new PdfString("EN"));

Taken from the additional answer by Chris Haas:

The items Magnification = Fit Width and Open to Page number = 1 are also part of the /Catalog but in a special key called /OpenAction. You can set this using setOpenAction().

In your case you're looking for:

pdfDoc.getCatalog().setOpenAction(PdfExplicitDestination.createXYZ(1, 0, pdfDoc.getPage(1).getPageSize().getHeight(), 0.75f));

Click this link if you want to see how to answer this question in iText 5.


Share this article

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now