Using getSimpleViewerPreferences

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Using getSimpleViewerPreferences

tacallah
I am using iText 2.1.5, and I was trying to determine viewer preferences of a PDF.  I know it has HideToolbar and HideWindowUI set, but when I do the following, getSimpleViewerPreferences returns zero:

    try {
      loPdfReader = new PdfReader(this.cOldFile)
    } catch (Exception loError) {
      HandleError(loError);
      return;
    }
    System.out.println("vp=" + loPdfReader.getSimpleViewerPreferences());

Am I doing something wrong, or is there a better way to check for viewer preferences?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Using getSimpleViewerPreferences

tacallah
I could never get getSimpleViewerPreferences to work, but I was able to use the following code to do what I need:

      loDictionary = loPdfReader.getCatalog();   // Get PDF catalog
      if (loDictionary != null) {                // If catalog exists...
        loDictionary = (PdfDictionary) loDictionary.get(PdfName.VIEWERPREFERENCES);
                                                 // Get viewer preferences
        if (loDictionary != null) {              // If viewer preferences exist...
          if (loDictionary.contains(PdfName.HIDEMENUBAR))
                                                 // If menu bar is hidden...
            HandleError(new Throwable("Menu bar is hidden (HideMenuBar)."), lcRecCode);
                                                 // Generate an error
          if (loDictionary.contains(PdfName.HIDETOOLBAR))
                                                 // If tool bar is hidden...
            HandleError(new Throwable("Tool bar is hidden (HideToolBar)."), lcRecCode);
                                                 // Generate an error
          if (loDictionary.contains(PdfName.HIDEWINDOWUI))
                                                 // If user interface is hidden...
            HandleError(new Throwable("User interface is hidden (HideWindowUI)."), lcRecCode);
                                                 // Generate an error
        }
      }

Tim
tacallah wrote
I am using iText 2.1.5, and I was trying to determine viewer preferences of a PDF.  I know it has HideToolbar and HideWindowUI set, but when I do the following, getSimpleViewerPreferences returns zero:

    try {
      loPdfReader = new PdfReader(this.cOldFile)
    } catch (Exception loError) {
      HandleError(loError);
      return;
    }
    System.out.println("vp=" + loPdfReader.getSimpleViewerPreferences());

Am I doing something wrong, or is there a better way to check for viewer preferences?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: Using getSimpleViewerPreferences

iText mailing list
tacallah wrote:
> I could never get getSimpleViewerPreferences to work, but I was to use the
> following code to do what I need

Your code is wrong. I'll tell you why in the next mail.
--
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info

------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Reply | Threaded
Open this post in threaded view
|

Re: Using getSimpleViewerPreferences

iText mailing list
In reply to this post by tacallah
tacallah wrote:
> I am using iText 2.1.5, and I was trying to determine viewer preferences of a
> PDF.  I know it has HideToolbar and HideWindowUI set, but when I do the
> following, getSimpleViewerPreferences returns zero

getSimpleViewerPreferences only returns an int for the PageMode and
PageLayout key, NOT for keys in the ViewerPreferences dictionary such as
HideToolbar and HideWindowUI.
You need to do:
PdfViewerPreferencesImp.getViewerPreferences(reader.getCatalog()).getViewerPreferences();
This returns a dictionary with keys such as: PdfName.HIDEMENUBAR,
PdfName.HIDETOOLBAR, and so on.

> Am I doing something wrong, or is there a better way to check for viewer
> preferences?

In your "solution" you check if the keys are present, but that isn't
sufficient! You should check if the value is equal to PdfBoolean.PDFTRUE
or PdfBoolean.PDFFALSE.
The key can be present, but when it's value is PDFFALSE, then the viewer
preference isn't set!
--
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info

------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Reply | Threaded
Open this post in threaded view
|

Re: Using getSimpleViewerPreferences

tacallah
Thanks for your response!  I just started using iText, so I am still trying to learn.  The JavaDocs are nice, but I guess a user guide would be more helpful.

1T3XT info wrote
getSimpleViewerPreferences only returns an int for the PageMode and
PageLayout key, NOT for keys in the ViewerPreferences dictionary such as
HideToolbar and HideWindowUI.
You need to do:
PdfViewerPreferencesImp.getViewerPreferences(reader.getCatalog()).getViewerPreferences();
This returns a dictionary with keys such as: PdfName.HIDEMENUBAR,
PdfName.HIDETOOLBAR, and so on.!
I thought I tried something like this, but I tried so many things and cannot remember.  For fun, I tried what you suggested as shown below.  It is interesting to note that I had to test loDictionary.getAsBoolean(...) for null; whereas, in the other solution I did not.

    PdfViewerPreferencesImp loVP = PdfViewerPreferencesImp.getViewerPreferences(loPdfReader.getCatalog());
    if (loVP != null) {
      loDictionary = loVP.getViewerPreferences();
      if (loDictionary != null) {
        if (loDictionary.getAsBoolean(PdfName.HIDEMENUBAR) != null && loDictionary.getAsBoolean(PdfName.HIDEMENUBAR).booleanValue())
                                                 // If menu bar is hidden...
          HandleError(new Throwable("Menu bar is hidden (HideMenuBar)."), lcRecCode);
                                                 // Generate an error
        if (loDictionary.getAsBoolean(PdfName.HIDETOOLBAR) != null && loDictionary.getAsBoolean(PdfName.HIDETOOLBAR).booleanValue())
                                                 // If tool bar is hidden...
          HandleError(new Throwable("Tool bar is hidden (HideToolBar)."), lcRecCode);
                                                 // Generate an error
        if (loDictionary.getAsBoolean(PdfName.HIDEWINDOWUI) != null && loDictionary.getAsBoolean(PdfName.HIDEWINDOWUI).booleanValue())
                                                 // If user interface is hidden...
          HandleError(new Throwable("User interface is hidden (HideWindowUI)."), lcRecCode);
                                                 // Generate an error
      }
    }

1T3XT info wrote
In your "solution" you check if the keys are present, but that isn't
sufficient! You should check if the value is equal to PdfBoolean.PDFTRUE
or PdfBoolean.PDFFALSE.
The key can be present, but when it's value is PDFFALSE, then the viewer
preference isn't set!
As shown below, I corrected the code to check the values, not just the existance of the fields.  I will probably stick with this solution.  Thanks for pointing out the problem with the code!

      loDictionary = loPdfReader.getCatalog();   // Get PDF catalog
      if (loDictionary != null) {                // If catalog exists...
        loDictionary = (PdfDictionary) loDictionary.get(PdfName.VIEWERPREFERENCES);
                                                 // Get viewer preferences
        if (loDictionary != null) {              // If viewer preferences exist...
          if (loDictionary.getAsBoolean(PdfName.HIDEMENUBAR).booleanValue())
                                                 // If menu bar is hidden...
            HandleError(new Throwable("Menu bar is hidden (HideMenuBar)."), lcRecCode);
                                                 // Generate an error
          if (loDictionary.getAsBoolean(PdfName.HIDETOOLBAR).booleanValue())
                                                 // If tool bar is hidden...
            HandleError(new Throwable("Tool bar is hidden (HideToolBar)."), lcRecCode);
                                                 // Generate an error
          if (loDictionary.getAsBoolean(PdfName.HIDEWINDOWUI).booleanValue())
                                                 // If user interface is hidden...
            HandleError(new Throwable("User interface is hidden (HideWindowUI)."), lcRecCode);
                                                 // Generate an error
        }
      }

Tim
Reply | Threaded
Open this post in threaded view
|

Re: Using getSimpleViewerPreferences

iText mailing list
tacallah wrote:
> I guess a user guide would be more helpful.

That's what the book if for: http://1t3xt.com/docs/book.php
--
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info

------------------------------------------------------------------------------
Stay on top of everything new and different, both inside and
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today.
Use priority code J9JMT32. http://p.sf.net/sfu/p
_______________________________________________
iText-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php