1 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 66 67 public class PdfViewerPreferencesImp implements PdfViewerPreferences { 68 public static final PdfName[] VIEWER_PREFERENCES = { 69 PdfName.HIDETOOLBAR, PdfName.HIDEMENUBAR, PdfName.HIDEWINDOWUI, PdfName.FITWINDOW, PdfName.CENTERWINDOW, PdfName.DISPLAYDOCTITLE, PdfName.NONFULLSCREENPAGEMODE, PdfName.DIRECTION, PdfName.VIEWAREA, PdfName.VIEWCLIP, PdfName.PRINTAREA, PdfName.PRINTCLIP, PdfName.PRINTSCALING, PdfName.DUPLEX, PdfName.PICKTRAYBYPDFSIZE, PdfName.PRINTPAGERANGE, PdfName.NUMCOPIES }; 87 88 89 90 public static final PdfName NONFULLSCREENPAGEMODE_PREFERENCES[] = { 91 PdfName.USENONE, PdfName.USEOUTLINES, PdfName.USETHUMBS, PdfName.USEOC 92 }; 93 94 public static final PdfName DIRECTION_PREFERENCES[] = { 95 PdfName.L2R, PdfName.R2L 96 }; 97 98 public static final PdfName PAGE_BOUNDARIES[] = { 99 PdfName.MEDIABOX, PdfName.CROPBOX, PdfName.BLEEDBOX, PdfName.TRIMBOX, PdfName.ARTBOX 100 }; 101 102 public static final PdfName PRINTSCALING_PREFERENCES[] = { 103 PdfName.APPDEFAULT, PdfName.NONE 104 }; 105 106 public static final PdfName DUPLEX_PREFERENCES[] = { 107 PdfName.SIMPLEX, PdfName.DUPLEXFLIPSHORTEDGE, PdfName.DUPLEXFLIPSHORTEDGE 108 }; 109 110 111 private int pageLayoutAndMode = 0; 112 113 114 private PdfDictionary viewerPreferences = new PdfDictionary(); 115 116 117 private static final int viewerPreferencesMask = 0xfff000; 118 119 122 public int getPageLayoutAndMode() { 123 return pageLayoutAndMode; 124 } 125 126 129 public PdfDictionary getViewerPreferences() { 130 return viewerPreferences; 131 } 132 133 140 public void setViewerPreferences(int preferences) { 141 this.pageLayoutAndMode |= preferences; 142 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 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 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 207 public void addViewerPreference(PdfName key, PdfObject value) { 208 switch(getIndex(key)) { 209 case 0: case 1: case 2: case 3: case 4: case 5: case 14: if (value instanceof PdfBoolean) { 217 viewerPreferences.put(key, value); 218 } 219 break; 220 case 6: if (value instanceof PdfName 222 && isPossibleValue((PdfName)value, NONFULLSCREENPAGEMODE_PREFERENCES)) { 223 viewerPreferences.put(key, value); 224 } 225 break; 226 case 7: if (value instanceof PdfName 228 && isPossibleValue((PdfName)value, DIRECTION_PREFERENCES)) { 229 viewerPreferences.put(key, value); 230 } 231 break; 232 case 8: case 9: case 10: case 11: if (value instanceof PdfName 237 && isPossibleValue((PdfName)value, PAGE_BOUNDARIES)) { 238 viewerPreferences.put(key, value); 239 } 240 break; 241 case 12: if (value instanceof PdfName 243 && isPossibleValue((PdfName)value, PRINTSCALING_PREFERENCES)) { 244 viewerPreferences.put(key, value); 245 } 246 break; 247 case 13: if (value instanceof PdfName 249 && isPossibleValue((PdfName)value, DUPLEX_PREFERENCES)) { 250 viewerPreferences.put(key, value); 251 } 252 break; 253 case 15: if (value instanceof PdfArray) { 255 viewerPreferences.put(key, value); 256 } 257 break; 258 case 16: if (value instanceof PdfNumber) { 260 viewerPreferences.put(key, value); 261 } 262 break; 263 } 264 } 265 266 272 public void addToCatalog(PdfDictionary catalog) { 273 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 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 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 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 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 preferences.setViewerPreferences(prefs); 348 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 |