001: /*
002: * $Id: PdfViewerPreferencesImp.java 2908 2007-09-03 09:18:23Z blowagie $
003: *
004: * Copyright 2006 Bruno Lowagie
005: *
006: * The contents of this file are subject to the Mozilla Public License Version 1.1
007: * (the "License"); you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the License.
013: *
014: * The Original Code is 'iText, a free JAVA-PDF library'.
015: *
016: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
017: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
018: * All Rights Reserved.
019: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
020: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
021: *
022: * Contributor(s): all the names of the contributors are added in the source code
023: * where applicable.
024: *
025: * Alternatively, the contents of this file may be used under the terms of the
026: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
027: * provisions of LGPL are applicable instead of those above. If you wish to
028: * allow use of your version of this file only under the terms of the LGPL
029: * License and not to allow others to use your version of this file under
030: * the MPL, indicate your decision by deleting the provisions above and
031: * replace them with the notice and other provisions required by the LGPL.
032: * If you do not delete the provisions above, a recipient may use your version
033: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
034: *
035: * This library is free software; you can redistribute it and/or modify it
036: * under the terms of the MPL as stated above or under the terms of the GNU
037: * Library General Public License as published by the Free Software Foundation;
038: * either version 2 of the License, or any later version.
039: *
040: * This library is distributed in the hope that it will be useful, but WITHOUT
041: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
042: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
043: * details.
044: *
045: * If you didn't download this code from the following link, you should check if
046: * you aren't using an obsolete version:
047: * http://www.lowagie.com/iText/
048: */
049:
050: package com.lowagie.text.pdf.internal;
051:
052: import com.lowagie.text.pdf.PdfArray;
053: import com.lowagie.text.pdf.PdfBoolean;
054: import com.lowagie.text.pdf.PdfDictionary;
055: import com.lowagie.text.pdf.PdfName;
056: import com.lowagie.text.pdf.PdfNumber;
057: import com.lowagie.text.pdf.PdfObject;
058: import com.lowagie.text.pdf.PdfReader;
059: import com.lowagie.text.pdf.PdfWriter;
060: import com.lowagie.text.pdf.interfaces.PdfViewerPreferences;
061:
062: /**
063: * Stores the information concerning viewer preferences,
064: * and contains the business logic that allows you to set viewer preferences.
065: */
066:
067: public class PdfViewerPreferencesImp implements PdfViewerPreferences {
068: public static final PdfName[] VIEWER_PREFERENCES = {
069: PdfName.HIDETOOLBAR, // 0
070: PdfName.HIDEMENUBAR, // 1
071: PdfName.HIDEWINDOWUI, // 2
072: PdfName.FITWINDOW, // 3
073: PdfName.CENTERWINDOW, // 4
074: PdfName.DISPLAYDOCTITLE, // 5
075: PdfName.NONFULLSCREENPAGEMODE, // 6
076: PdfName.DIRECTION, // 7
077: PdfName.VIEWAREA, // 8
078: PdfName.VIEWCLIP, // 9
079: PdfName.PRINTAREA, // 10
080: PdfName.PRINTCLIP, // 11
081: PdfName.PRINTSCALING, // 12
082: PdfName.DUPLEX, // 13
083: PdfName.PICKTRAYBYPDFSIZE, // 14
084: PdfName.PRINTPAGERANGE, // 15
085: PdfName.NUMCOPIES // 16
086: };
087:
088: /** A series of viewer preferences. */
089: public static final PdfName NONFULLSCREENPAGEMODE_PREFERENCES[] = {
090: PdfName.USENONE, PdfName.USEOUTLINES, PdfName.USETHUMBS,
091: PdfName.USEOC };
092: /** A series of viewer preferences. */
093: public static final PdfName DIRECTION_PREFERENCES[] = {
094: PdfName.L2R, PdfName.R2L };
095: /** A series of viewer preferences. */
096: public static final PdfName PAGE_BOUNDARIES[] = { PdfName.MEDIABOX,
097: PdfName.CROPBOX, PdfName.BLEEDBOX, PdfName.TRIMBOX,
098: PdfName.ARTBOX };
099: /** A series of viewer preferences */
100: public static final PdfName PRINTSCALING_PREFERENCES[] = {
101: PdfName.APPDEFAULT, PdfName.NONE };
102: /** A series of viewer preferences. */
103: public static final PdfName DUPLEX_PREFERENCES[] = {
104: PdfName.SIMPLEX, PdfName.DUPLEXFLIPSHORTEDGE,
105: PdfName.DUPLEXFLIPLONGEDGE };
106:
107: /** This value will hold the viewer preferences for the page layout and page mode. */
108: private int pageLayoutAndMode = 0;
109:
110: /** This dictionary holds the viewer preferences (other than page layout and page mode). */
111: private PdfDictionary viewerPreferences = new PdfDictionary();
112:
113: /** The mask to decide if a ViewerPreferences dictionary is needed */
114: private static final int viewerPreferencesMask = 0xfff000;
115:
116: /**
117: * Returns the page layout and page mode value.
118: */
119: public int getPageLayoutAndMode() {
120: return pageLayoutAndMode;
121: }
122:
123: /**
124: * Returns the viewer preferences.
125: */
126: public PdfDictionary getViewerPreferences() {
127: return viewerPreferences;
128: }
129:
130: /**
131: * Sets the viewer preferences as the sum of several constants.
132: *
133: * @param preferences
134: * the viewer preferences
135: * @see PdfViewerPreferences#setViewerPreferences
136: */
137: public void setViewerPreferences(int preferences) {
138: this .pageLayoutAndMode |= preferences;
139: // for backwards compatibility, it is also possible
140: // to set the following viewer preferences with this method:
141: if ((preferences & viewerPreferencesMask) != 0) {
142: pageLayoutAndMode = ~viewerPreferencesMask
143: & pageLayoutAndMode;
144: if ((preferences & PdfWriter.HideToolbar) != 0)
145: viewerPreferences.put(PdfName.HIDETOOLBAR,
146: PdfBoolean.PDFTRUE);
147: if ((preferences & PdfWriter.HideMenubar) != 0)
148: viewerPreferences.put(PdfName.HIDEMENUBAR,
149: PdfBoolean.PDFTRUE);
150: if ((preferences & PdfWriter.HideWindowUI) != 0)
151: viewerPreferences.put(PdfName.HIDEWINDOWUI,
152: PdfBoolean.PDFTRUE);
153: if ((preferences & PdfWriter.FitWindow) != 0)
154: viewerPreferences.put(PdfName.FITWINDOW,
155: PdfBoolean.PDFTRUE);
156: if ((preferences & PdfWriter.CenterWindow) != 0)
157: viewerPreferences.put(PdfName.CENTERWINDOW,
158: PdfBoolean.PDFTRUE);
159: if ((preferences & PdfWriter.DisplayDocTitle) != 0)
160: viewerPreferences.put(PdfName.DISPLAYDOCTITLE,
161: PdfBoolean.PDFTRUE);
162:
163: if ((preferences & PdfWriter.NonFullScreenPageModeUseNone) != 0)
164: viewerPreferences.put(PdfName.NONFULLSCREENPAGEMODE,
165: PdfName.USENONE);
166: else if ((preferences & PdfWriter.NonFullScreenPageModeUseOutlines) != 0)
167: viewerPreferences.put(PdfName.NONFULLSCREENPAGEMODE,
168: PdfName.USEOUTLINES);
169: else if ((preferences & PdfWriter.NonFullScreenPageModeUseThumbs) != 0)
170: viewerPreferences.put(PdfName.NONFULLSCREENPAGEMODE,
171: PdfName.USETHUMBS);
172: else if ((preferences & PdfWriter.NonFullScreenPageModeUseOC) != 0)
173: viewerPreferences.put(PdfName.NONFULLSCREENPAGEMODE,
174: PdfName.USEOC);
175:
176: if ((preferences & PdfWriter.DirectionL2R) != 0)
177: viewerPreferences.put(PdfName.DIRECTION, PdfName.L2R);
178: else if ((preferences & PdfWriter.DirectionR2L) != 0)
179: viewerPreferences.put(PdfName.DIRECTION, PdfName.R2L);
180:
181: if ((preferences & PdfWriter.PrintScalingNone) != 0)
182: viewerPreferences.put(PdfName.PRINTSCALING,
183: PdfName.NONE);
184: }
185: }
186:
187: /**
188: * Given a key for a viewer preference (a PdfName object),
189: * this method returns the index in the VIEWER_PREFERENCES array.
190: * @param key a PdfName referring to a viewer preference
191: * @return an index in the VIEWER_PREFERENCES array
192: */
193: private int getIndex(PdfName key) {
194: for (int i = 0; i < VIEWER_PREFERENCES.length; i++) {
195: if (VIEWER_PREFERENCES[i].equals(key))
196: return i;
197: }
198: return -1;
199: }
200:
201: /**
202: * Checks if some value is valid for a certain key.
203: */
204: private boolean isPossibleValue(PdfName value, PdfName[] accepted) {
205: for (int i = 0; i < accepted.length; i++) {
206: if (accepted[i].equals(value)) {
207: return true;
208: }
209: }
210: return false;
211: }
212:
213: /**
214: * Sets the viewer preferences for printing.
215: */
216: public void addViewerPreference(PdfName key, PdfObject value) {
217: switch (getIndex(key)) {
218: case 0: // HIDETOOLBAR
219: case 1: // HIDEMENUBAR
220: case 2: // HIDEWINDOWUI
221: case 3: // FITWINDOW
222: case 4: // CENTERWINDOW
223: case 5: // DISPLAYDOCTITLE
224: case 14: // PICKTRAYBYPDFSIZE
225: if (value instanceof PdfBoolean) {
226: viewerPreferences.put(key, value);
227: }
228: break;
229: case 6: // NONFULLSCREENPAGEMODE
230: if (value instanceof PdfName
231: && isPossibleValue((PdfName) value,
232: NONFULLSCREENPAGEMODE_PREFERENCES)) {
233: viewerPreferences.put(key, value);
234: }
235: break;
236: case 7: // DIRECTION
237: if (value instanceof PdfName
238: && isPossibleValue((PdfName) value,
239: DIRECTION_PREFERENCES)) {
240: viewerPreferences.put(key, value);
241: }
242: break;
243: case 8: // VIEWAREA
244: case 9: // VIEWCLIP
245: case 10: // PRINTAREA
246: case 11: // PRINTCLIP
247: if (value instanceof PdfName
248: && isPossibleValue((PdfName) value, PAGE_BOUNDARIES)) {
249: viewerPreferences.put(key, value);
250: }
251: break;
252: case 12: // PRINTSCALING
253: if (value instanceof PdfName
254: && isPossibleValue((PdfName) value,
255: PRINTSCALING_PREFERENCES)) {
256: viewerPreferences.put(key, value);
257: }
258: break;
259: case 13: // DUPLEX
260: if (value instanceof PdfName
261: && isPossibleValue((PdfName) value,
262: DUPLEX_PREFERENCES)) {
263: viewerPreferences.put(key, value);
264: }
265: break;
266: case 15: // PRINTPAGERANGE
267: if (value instanceof PdfArray) {
268: viewerPreferences.put(key, value);
269: }
270: break;
271: case 16: // NUMCOPIES
272: if (value instanceof PdfNumber) {
273: viewerPreferences.put(key, value);
274: }
275: break;
276: }
277: }
278:
279: /**
280: * Adds the viewer preferences defined in the preferences parameter to a
281: * PdfDictionary (more specifically the root or catalog of a PDF file).
282: *
283: * @param catalog
284: */
285: public void addToCatalog(PdfDictionary catalog) {
286: // Page Layout
287: catalog.remove(PdfName.PAGELAYOUT);
288: if ((pageLayoutAndMode & PdfWriter.PageLayoutSinglePage) != 0)
289: catalog.put(PdfName.PAGELAYOUT, PdfName.SINGLEPAGE);
290: else if ((pageLayoutAndMode & PdfWriter.PageLayoutOneColumn) != 0)
291: catalog.put(PdfName.PAGELAYOUT, PdfName.ONECOLUMN);
292: else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnLeft) != 0)
293: catalog.put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNLEFT);
294: else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoColumnRight) != 0)
295: catalog.put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNRIGHT);
296: else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageLeft) != 0)
297: catalog.put(PdfName.PAGELAYOUT, PdfName.TWOPAGELEFT);
298: else if ((pageLayoutAndMode & PdfWriter.PageLayoutTwoPageRight) != 0)
299: catalog.put(PdfName.PAGELAYOUT, PdfName.TWOPAGERIGHT);
300:
301: // Page Mode
302: catalog.remove(PdfName.PAGEMODE);
303: if ((pageLayoutAndMode & PdfWriter.PageModeUseNone) != 0)
304: catalog.put(PdfName.PAGEMODE, PdfName.USENONE);
305: else if ((pageLayoutAndMode & PdfWriter.PageModeUseOutlines) != 0)
306: catalog.put(PdfName.PAGEMODE, PdfName.USEOUTLINES);
307: else if ((pageLayoutAndMode & PdfWriter.PageModeUseThumbs) != 0)
308: catalog.put(PdfName.PAGEMODE, PdfName.USETHUMBS);
309: else if ((pageLayoutAndMode & PdfWriter.PageModeFullScreen) != 0)
310: catalog.put(PdfName.PAGEMODE, PdfName.FULLSCREEN);
311: else if ((pageLayoutAndMode & PdfWriter.PageModeUseOC) != 0)
312: catalog.put(PdfName.PAGEMODE, PdfName.USEOC);
313: else if ((pageLayoutAndMode & PdfWriter.PageModeUseAttachments) != 0)
314: catalog.put(PdfName.PAGEMODE, PdfName.USEATTACHMENTS);
315:
316: // viewer preferences (Table 8.1 of the PDF Reference)
317: catalog.remove(PdfName.VIEWERPREFERENCES);
318: if (viewerPreferences.size() > 0) {
319: catalog.put(PdfName.VIEWERPREFERENCES, viewerPreferences);
320: }
321: }
322:
323: public static PdfViewerPreferencesImp getViewerPreferences(
324: PdfDictionary catalog) {
325: PdfViewerPreferencesImp preferences = new PdfViewerPreferencesImp();
326: int prefs = 0;
327: PdfName name = null;
328: // page layout
329: PdfObject obj = PdfReader.getPdfObjectRelease(catalog
330: .get(PdfName.PAGELAYOUT));
331: if (obj != null && obj.isName()) {
332: name = (PdfName) obj;
333: if (name.equals(PdfName.SINGLEPAGE))
334: prefs |= PdfWriter.PageLayoutSinglePage;
335: else if (name.equals(PdfName.ONECOLUMN))
336: prefs |= PdfWriter.PageLayoutOneColumn;
337: else if (name.equals(PdfName.TWOCOLUMNLEFT))
338: prefs |= PdfWriter.PageLayoutTwoColumnLeft;
339: else if (name.equals(PdfName.TWOCOLUMNRIGHT))
340: prefs |= PdfWriter.PageLayoutTwoColumnRight;
341: else if (name.equals(PdfName.TWOPAGELEFT))
342: prefs |= PdfWriter.PageLayoutTwoPageLeft;
343: else if (name.equals(PdfName.TWOPAGERIGHT))
344: prefs |= PdfWriter.PageLayoutTwoPageRight;
345: }
346: // page mode
347: obj = PdfReader.getPdfObjectRelease(catalog
348: .get(PdfName.PAGEMODE));
349: if (obj != null && obj.isName()) {
350: name = (PdfName) obj;
351: if (name.equals(PdfName.USENONE))
352: prefs |= PdfWriter.PageModeUseNone;
353: else if (name.equals(PdfName.USEOUTLINES))
354: prefs |= PdfWriter.PageModeUseOutlines;
355: else if (name.equals(PdfName.USETHUMBS))
356: prefs |= PdfWriter.PageModeUseThumbs;
357: else if (name.equals(PdfName.USEOC))
358: prefs |= PdfWriter.PageModeUseOC;
359: else if (name.equals(PdfName.USEATTACHMENTS))
360: prefs |= PdfWriter.PageModeUseAttachments;
361: }
362: // set page layout and page mode preferences
363: preferences.setViewerPreferences(prefs);
364: // other preferences
365: obj = PdfReader.getPdfObjectRelease(catalog
366: .get(PdfName.VIEWERPREFERENCES));
367: if (obj != null && obj.isDictionary()) {
368: PdfDictionary vp = (PdfDictionary) obj;
369: for (int i = 0; i < VIEWER_PREFERENCES.length; i++) {
370: obj = PdfReader.getPdfObjectRelease(vp
371: .get(VIEWER_PREFERENCES[i]));
372: preferences.addViewerPreference(VIEWER_PREFERENCES[i],
373: obj);
374: }
375: }
376: return preferences;
377: }
378: }
|