How to set initial view properties?

Tags: open actionviewer preferencespage layoutiText 5

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} <</Lang(EN)>> /PUT pdfmark. How is it done in PDF?

Posted on StackOverflow on Jun 23, 2014 by Thirusanguraja Venkatesan

When you have a PdfWriter instance named writer, you can set the Viewer preferences like this:

writer.ViewerPreferences = viewerpreference;

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

  • PdfWriter.PageLayoutSinglePage

  • PdfWriter.PageLayoutOneColumn

  • PdfWriter.PageLayoutTwoColumnLeft

  • PdfWriter.PageLayoutTwoColumnRight

  • PdfWriter.PageLayoutTwoPageLeft

  • PdfWriter.PageLayoutTwoPageRight

See the PageLayoutExample for more info.

You can also change the page mode as is shown in the ViewerPreferencesExample. In which case the different values are "OR"-ed:

  • PdfWriter.PageModeFullScreen

  • PdfWriter.PageModeUseThumbs

  • PdfWriter.PageLayoutTwoColumnRight | PdfWriter.PageModeUseThumbs

  • PdfWriter.PageModeFullScreen | PdfWriter.NonFullScreenPageModeUseOutlines

  • PdfWriter.FitWindow | PdfWriter.HideToolbar

  • PdfWriter.HideWindowUI

Currently, you've only used the PrintPreferences example from the official documentation:

writer.AddViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
writer.AddViewerPreference(PdfName.NUMCOPIES, new PdfNumber(3));
writer.AddViewerPreference(PdfName.PICKTRAYBYPDFSIZE, PdfBoolean.PDFTRUE); 

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

writer.ViewerPreferences = viewerpreference;

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

stamper.Writer.ExtraCatalog.Put(PdfName.LANG, new PdfString("EN"));

The result is shown in the following screen shot:

RUPS: looking at the internal structure of a PDF
RUPS: looking at the internal structure of a PDF

As you can see, there is now a Lang entry with value EN added to the catalog.

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 Writer.SetOpenAction().

In your case you're looking for:

//Create a destination that fit's width (fit horizontal)
var D = new PdfDestination(PdfDestination.FITH);
//Create an open action that points to a specific page using this destination
var OA = PdfAction.GotoLocalPage(1, D, stamper.Writer);
//Set the open action on the writer