Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

itextsharp - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm adding an extra answer in answer to the extra question in the comments of the previous answer:

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;

Note that the official documentation is the book "iText in Action - Second Edition." The examples are written in Java, but you can find the C# version here. There is a new book in the works called "The ABC of PDF", but so far only 4 chapters were written. You'll find more info here: http://itextpdf.com/learn

The part about the different options to create a PdfDestination is already present in "The ABC of PDF".

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:

enter image description here

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...