KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > internal > PdfViewerPreferencesImp


1 /*
2  * $Id: PdfViewerPreferencesImp.java 2772 2007-05-21 14:06:08Z blowagie $
3  *
4  * Copyright 2006 Bruno Lowagie
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text.pdf.internal;
51
52 import com.lowagie.text.pdf.PdfArray;
53 import com.lowagie.text.pdf.PdfBoolean;
54 import com.lowagie.text.pdf.PdfDictionary;
55 import com.lowagie.text.pdf.PdfName;
56 import com.lowagie.text.pdf.PdfNumber;
57 import com.lowagie.text.pdf.PdfObject;
58 import com.lowagie.text.pdf.PdfReader;
59 import com.lowagie.text.pdf.PdfWriter;
60 import com.lowagie.text.pdf.interfaces.PdfViewerPreferences;
61
62 /**
63  * Stores the information concerning viewer preferences,
64  * and contains the business logic that allows you to set viewer preferences.
65  */

66
67 public class PdfViewerPreferencesImp implements PdfViewerPreferences {
68     public static final PdfName[] VIEWER_PREFERENCES = {
69             PdfName.HIDETOOLBAR, // 0
70
PdfName.HIDEMENUBAR, // 1
71
PdfName.HIDEWINDOWUI, // 2
72
PdfName.FITWINDOW, // 3
73
PdfName.CENTERWINDOW, // 4
74
PdfName.DISPLAYDOCTITLE, // 5
75
PdfName.NONFULLSCREENPAGEMODE, // 6
76
PdfName.DIRECTION, // 7
77
PdfName.VIEWAREA, // 8
78
PdfName.VIEWCLIP, // 9
79
PdfName.PRINTAREA, // 10
80
PdfName.PRINTCLIP, // 11
81
PdfName.PRINTSCALING, // 12
82
PdfName.DUPLEX, // 13
83
PdfName.PICKTRAYBYPDFSIZE, // 14
84
PdfName.PRINTPAGERANGE, // 15
85
PdfName.NUMCOPIES // 16
86
};
87
88
89     /** A series of viewer preferences. */
90     public static final PdfName NONFULLSCREENPAGEMODE_PREFERENCES[] = {
91         PdfName.USENONE, PdfName.USEOUTLINES, PdfName.USETHUMBS, PdfName.USEOC
92     };
93     /** A series of viewer preferences. */
94     public static final PdfName DIRECTION_PREFERENCES[] = {
95         PdfName.L2R, PdfName.R2L
96     };
97     /** A series of viewer preferences. */
98     public static final PdfName PAGE_BOUNDARIES[] = {
99         PdfName.MEDIABOX, PdfName.CROPBOX, PdfName.BLEEDBOX, PdfName.TRIMBOX, PdfName.ARTBOX
100     };
101     /** A series of viewer preferences */
102     public static final PdfName PRINTSCALING_PREFERENCES[] = {
103         PdfName.APPDEFAULT, PdfName.NONE
104     };
105     /** A series of viewer preferences. */
106     public static final PdfName DUPLEX_PREFERENCES[] = {
107         PdfName.SIMPLEX, PdfName.DUPLEXFLIPSHORTEDGE, PdfName.DUPLEXFLIPSHORTEDGE
108     };
109     
110     /** This value will hold the viewer preferences for the page layout and page mode. */
111     private int pageLayoutAndMode = 0;
112     
113     /** This dictionary holds the viewer preferences (other than page layout and page mode). */
114     private PdfDictionary viewerPreferences = new PdfDictionary();
115     
116     /** The mask to decide if a ViewerPreferences dictionary is needed */
117     private static final int viewerPreferencesMask = 0xfff000;
118
119     /**
120      * Returns the page layout and page mode value.
121      */

122     public int getPageLayoutAndMode() {
123         return pageLayoutAndMode;
124     }
125
126     /**
127      * Returns the viewer preferences.
128      */

129     public PdfDictionary getViewerPreferences() {
130         return viewerPreferences;
131     }
132     
133     /**
134      * Sets the viewer preferences as the sum of several constants.
135      *
136      * @param preferences
137      * the viewer preferences
138      * @see PdfViewerPreferences#setViewerPreferences
139      */

140     public void setViewerPreferences(int preferences) {
141         this.pageLayoutAndMode |= preferences;
142         // for backwards compatibility, it is also possible
143
// to set the following viewer preferences with this method:
144
if ((preferences & viewerPreferencesMask) != 0) {
145             pageLayoutAndMode = ~viewerPreferencesMask & pageLayoutAndMode;
146             if ((preferences & PdfWriter.HideToolbar) != 0)
147                 viewerPreferences.put(PdfName.HIDETOOLBAR, PdfBoolean.PDFTRUE);
148             if ((preferences & PdfWriter.HideMenubar) != 0)
149                 viewerPreferences.put(PdfName.HIDEMENUBAR, PdfBoolean.PDFTRUE);
150             if ((preferences & PdfWriter.HideWindowUI) != 0)
151                 viewerPreferences.put(PdfName.HIDEWINDOWUI, PdfBoolean.PDFTRUE);
152             if ((preferences & PdfWriter.FitWindow) != 0)
153                 viewerPreferences.put(PdfName.FITWINDOW, PdfBoolean.PDFTRUE);
154             if ((preferences & PdfWriter.CenterWindow) != 0)
155                 viewerPreferences.put(PdfName.CENTERWINDOW, PdfBoolean.PDFTRUE);
156             if ((preferences & PdfWriter.DisplayDocTitle) != 0)
157                 viewerPreferences.put(PdfName.DISPLAYDOCTITLE, PdfBoolean.PDFTRUE);
158             
159             if ((preferences & PdfWriter.NonFullScreenPageModeUseNone) != 0)
160                 viewerPreferences.put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USENONE);
161             else if ((preferences & PdfWriter.NonFullScreenPageModeUseOutlines) != 0)
162                 viewerPreferences.put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USEOUTLINES);
163             else if ((preferences & PdfWriter.NonFullScreenPageModeUseThumbs) != 0)
164                 viewerPreferences.put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USETHUMBS);
165             else if ((preferences & PdfWriter.NonFullScreenPageModeUseOC) != 0)
166                 viewerPreferences.put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USEOC);
167
168             if ((preferences & PdfWriter.DirectionL2R) != 0)
169                 viewerPreferences.put(PdfName.DIRECTION, PdfName.L2R);
170             else if ((preferences & PdfWriter.DirectionR2L) != 0)
171                 viewerPreferences.put(PdfName.DIRECTION, PdfName.R2L);
172
173             if ((preferences & PdfWriter.PrintScalingNone) != 0)
174                 viewerPreferences.put(PdfName.PRINTSCALING, PdfName.NONE);
175         }
176     }
177     
178     /**
179      * Given a key for a viewer preference (a PdfName object),
180      * this method returns the index in the VIEWER_PREFERENCES array.
181      * @param key a PdfName referring to a viewer preference
182      * @return an index in the VIEWER_PREFERENCES array
183      */

184     private int getIndex(PdfName key) {
185         for (int i = 0; i < VIEWER_PREFERENCES.length; i++) {
186             if (VIEWER_PREFERENCES[i].equals(key))
187                 return i;
188         }
189         return -1;
190     }
191     
192     /**
193      * Checks if some value is valid for a certain key.
194      */

195     private boolean isPossibleValue(PdfName value, PdfName[] accepted) {
196         for (int i = 0; i < accepted.length; i++) {
197             if (accepted[i].equals(value)) {
198                 return true;
199             }
200         }
201         return false;
202     }
203     
204     /**
205      * Sets the viewer preferences for printing.
206      */

207     public void addViewerPreference(PdfName key, PdfObject value) {
208         switch(getIndex(key)) {
209         case 0: // HIDETOOLBAR
210
case 1: // HIDEMENUBAR
211
case 2: // HIDEWINDOWUI
212
case 3: // FITWINDOW
213
case 4: // CENTERWINDOW
214
case 5: // DISPLAYDOCTITLE
215
case 14: // PICKTRAYBYPDFSIZE
216
if (value instanceof PdfBoolean) {
217                 viewerPreferences.put(key, value);
218             }
219             break;
220         case 6: // NONFULLSCREENPAGEMODE
221
if (value instanceof PdfName
222                     && isPossibleValue((PdfName)value, NONFULLSCREENPAGEMODE_PREFERENCES)) {
223                 viewerPreferences.put(key, value);
224             }
225             break;
226         case 7: // DIRECTION
227
if (value instanceof PdfName
228                     && isPossibleValue((PdfName)value, DIRECTION_PREFERENCES)) {
229                 viewerPreferences.put(key, value);
230             }
231             break;
232         case 8: // VIEWAREA
233
case 9: // VIEWCLIP
234
case 10: // PRINTAREA
235
case 11: // PRINTCLIP
236
if (value instanceof PdfName
237                     && isPossibleValue((PdfName)value, PAGE_BOUNDARIES)) {
238                 viewerPreferences.put(key, value);
239             }
240             break;
241         case 12: // PRINTSCALING
242
if (value instanceof PdfName
243                     && isPossibleValue((PdfName)value, PRINTSCALING_PREFERENCES)) {
244                 viewerPreferences.put(key, value);
245             }
246             break;
247         case 13: // DUPLEX
248
if (value instanceof PdfName
249                     && isPossibleValue((PdfName)value, DUPLEX_PREFERENCES)) {
250                 viewerPreferences.put(key, value);
251             }
252             break;
253         case 15: // PRINTPAGERANGE
254
if (value instanceof PdfArray) {
255                 viewerPreferences.put(key, value);
256             }
257             break;
258         case 16: // NUMCOPIES
259
if (value instanceof PdfNumber) {
260                 viewerPreferences.put(key, value);
261             }
262             break;
263         }
264     }
265
266     /**
267      * Adds the viewer preferences defined in the preferences parameter to a
268      * PdfDictionary (more specifically the root or catalog of a PDF file).
269      *
270      * @param catalog
271      */

272     public void addToCatalog(PdfDictionary catalog) {
273         // Page Layout
274
catalog.remove(PdfName.PAGELAYOUT);
275         if ((pageLayoutAndMode & PdfWriter.PageLayoutSinglePage) != 0)
276             catalog.put(PdfName.PAGELAYOUT, PdfName.SINGLEPAGE);
277         else if ((pageLayoutAndMode & PdfWriter.PageLayoutOneColumn) != 0)
278             catalog.put(PdfName.PAGELAYOUT, PdfName.ONECOLUMN);
279         else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnLeft) != 0)
280             catalog.put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNLEFT);
281         else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnRight) != 0)
282             catalog.put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNRIGHT);
283         else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageLeft) != 0)
284             catalog.put(PdfName.PAGELAYOUT, PdfName.TWOPAGELEFT);
285         else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageRight) != 0)
286             catalog.put(PdfName.PAGELAYOUT, PdfName.TWOPAGERIGHT);
287
288         // Page Mode
289
catalog.remove(PdfName.PAGEMODE);
290         if ((pageLayoutAndMode & PdfWriter.PageModeUseNone) != 0)
291             catalog.put(PdfName.PAGEMODE, PdfName.USENONE);
292         else if ((pageLayoutAndMode & PdfWriter.PageModeUseOutlines) != 0)
293             catalog.put(PdfName.PAGEMODE, PdfName.USEOUTLINES);
294         else if ((pageLayoutAndMode & PdfWriter.PageModeUseThumbs) != 0)
295             catalog.put(PdfName.PAGEMODE, PdfName.USETHUMBS);
296         else if ((pageLayoutAndMode & PdfWriter.PageModeFullScreen) != 0)
297             catalog.put(PdfName.PAGEMODE, PdfName.FULLSCREEN);
298         else if ((pageLayoutAndMode & PdfWriter.PageModeUseOC) != 0)
299             catalog.put(PdfName.PAGEMODE, PdfName.USEOC);
300         else if ((pageLayoutAndMode & PdfWriter.PageModeUseAttachments) != 0)
301             catalog.put(PdfName.PAGEMODE, PdfName.USEATTACHMENTS);
302
303         // viewer preferences (Table 8.1 of the PDF Reference)
304
catalog.remove(PdfName.VIEWERPREFERENCES);
305         if (viewerPreferences.size() > 0) {
306             catalog.put(PdfName.VIEWERPREFERENCES, viewerPreferences);
307         }
308     }
309
310     public static PdfViewerPreferencesImp getViewerPreferences(PdfDictionary catalog) {
311         PdfViewerPreferencesImp preferences = new PdfViewerPreferencesImp();
312         int prefs = 0;
313         PdfName name = null;
314         // page layout
315
PdfObject obj = PdfReader.getPdfObjectRelease(catalog.get(PdfName.PAGELAYOUT));
316         if (obj != null && obj.isName()) {
317             name = (PdfName) obj;
318             if (name.equals(PdfName.SINGLEPAGE))
319                 prefs |= PdfWriter.PageLayoutSinglePage;
320             else if (name.equals(PdfName.ONECOLUMN))
321                 prefs |= PdfWriter.PageLayoutOneColumn;
322             else if (name.equals(PdfName.TWOCOLUMNLEFT))
323                 prefs |= PdfWriter.PageLayoutTwoColumnLeft;
324             else if (name.equals(PdfName.TWOCOLUMNRIGHT))
325                 prefs |= PdfWriter.PageLayoutTwoColumnRight;
326             else if (name.equals(PdfName.TWOPAGELEFT))
327                 prefs |= PdfWriter.PageLayoutTwoPageLeft;
328             else if (name.equals(PdfName.TWOPAGERIGHT))
329                 prefs |= PdfWriter.PageLayoutTwoPageRight;
330         }
331         // page mode
332
obj = PdfReader.getPdfObjectRelease(catalog.get(PdfName.PAGEMODE));
333         if (obj != null && obj.isName()) {
334             name = (PdfName) obj;
335             if (name.equals(PdfName.USENONE))
336                 prefs |= PdfWriter.PageModeUseNone;
337             else if (name.equals(PdfName.USEOUTLINES))
338                 prefs |= PdfWriter.PageModeUseOutlines;
339             else if (name.equals(PdfName.USETHUMBS))
340                 prefs |= PdfWriter.PageModeUseThumbs;
341             else if (name.equals(PdfName.USEOC))
342                 prefs |= PdfWriter.PageModeUseOC;
343             else if (name.equals(PdfName.USEATTACHMENTS))
344                 prefs |= PdfWriter.PageModeUseAttachments;
345         }
346         // set page layout and page mode preferences
347
preferences.setViewerPreferences(prefs);
348         // other preferences
349
obj = PdfReader.getPdfObjectRelease(catalog
350                 .get(PdfName.VIEWERPREFERENCES));
351         if (obj != null && obj.isDictionary()) {
352             PdfDictionary vp = (PdfDictionary) obj;
353             for (int i = 0; i < VIEWER_PREFERENCES.length; i++) {
354                 obj = PdfReader.getPdfObjectRelease(vp.get(VIEWER_PREFERENCES[i]));
355                 preferences.addViewerPreference(VIEWER_PREFERENCES[i], obj);
356             }
357         }
358         return preferences;
359     }
360 }
Popular Tags