Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set initial view properties?

Here I want to set the already exist PDF document properties under Initial View tab in acrobat.

Document Options:

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

Window Options:

  • Show = Document Title

As show in below screen shot:

I am tried 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 following code are not working

For Page Layout:

stamper.AddViewerPreference(PdfName.PAGELAYOUT, new PdfName("OneColumn"));

For Bookmarks Panel and Page:

stamper.AddViewerPreference(PdfName. PageMode, new PdfName("UseOutlines"));

So please give guide me what is the correct way to meet my requirement.

like image 356
Thirusanguraja Venkatesan Avatar asked Jun 23 '14 15:06

Thirusanguraja Venkatesan


1 Answers

The two items Show = Bookmarks Panel and Page and Page Layout = Continuous are controlled one layer up from the ViewerPreferences in the document's /Catalog. You can get to this via:

stamper.Writer.ExtraCatalog

In your case you're looking for:

// Acrobat's Single Page
stamper.Writer.ExtraCatalog.Put(PdfName.PAGELAYOUT, PdfName.ONECOLUMN);
// Show bookmarks
stamper.Writer.ExtraCatalog.Put(PdfName.PAGEMODE, PdfName.USEOUTLINES);

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:

stamper.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
stamper.Writer.SetOpenAction(OA);
like image 98
Chris Haas Avatar answered Oct 30 '22 17:10

Chris Haas