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 = 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 How to set initial view properties? if you want to see how to answer this question in iText 5.