1 50 51 package com.lowagie.text.pdf; 52 53 import java.io.IOException ; 54 import java.io.OutputStream ; 55 import java.util.HashMap ; 56 import java.util.Iterator ; 57 import java.util.Set ; 58 59 78 79 public class PdfDictionary extends PdfObject { 80 81 83 84 public static final PdfName FONT = PdfName.FONT; 85 86 87 public static final PdfName OUTLINES = PdfName.OUTLINES; 88 89 90 public static final PdfName PAGE = PdfName.PAGE; 91 92 93 public static final PdfName PAGES = PdfName.PAGES; 94 95 96 public static final PdfName CATALOG = PdfName.CATALOG; 97 98 100 101 private PdfName dictionaryType = null; 102 103 104 protected HashMap hashMap; 105 106 108 111 112 public PdfDictionary() { 113 super(DICTIONARY); 114 hashMap = new HashMap (); 115 } 116 117 122 123 public PdfDictionary(PdfName type) { 124 this(); 125 dictionaryType = type; 126 put(PdfName.TYPE, dictionaryType); 127 } 128 129 131 134 135 public void toPdf(PdfWriter writer, OutputStream os) throws IOException { 136 os.write('<'); 137 os.write('<'); 138 139 PdfName key; 141 PdfObject value; 142 int type = 0; 143 for (Iterator i = hashMap.keySet().iterator(); i.hasNext(); ) { 144 key = (PdfName) i.next(); 145 value = (PdfObject) hashMap.get(key); 146 key.toPdf(writer, os); 147 type = value.type(); 148 if (type != PdfObject.ARRAY && type != PdfObject.DICTIONARY && type != PdfObject.NAME && type != PdfObject.STRING) 149 os.write(' '); 150 value.toPdf(writer, os); 151 } 152 os.write('>'); 153 os.write('>'); 154 } 155 156 158 165 166 public void put(PdfName key, PdfObject value) { 167 if (value == null || value.isNull()) 168 hashMap.remove(key); 169 else 170 hashMap.put(key, value); 171 } 172 173 180 public void putEx(PdfName key, PdfObject value) { 181 if (value == null) 182 return; 183 put(key, value); 184 } 185 186 191 192 public void remove(PdfName key) { 193 hashMap.remove(key); 194 } 195 196 202 203 public PdfObject get(PdfName key) { 204 return (PdfObject) hashMap.get(key); 205 } 206 207 209 217 218 public boolean isDictionaryType(PdfName type) { 219 return type.equals(dictionaryType); 220 } 221 222 227 228 public boolean isFont() { 229 return FONT.equals(dictionaryType); 230 } 231 232 237 238 public boolean isPage() { 239 return PAGE.equals(dictionaryType); 240 } 241 242 247 248 public boolean isPages() { 249 return PAGES.equals(dictionaryType); 250 } 251 252 257 258 public boolean isCatalog() { 259 return CATALOG.equals(dictionaryType); 260 } 261 262 267 268 public boolean isOutlineTree() { 269 return OUTLINES.equals(dictionaryType); 270 } 271 272 public void merge(PdfDictionary other) { 273 hashMap.putAll(other.hashMap); 274 } 275 276 public void mergeDifferent(PdfDictionary other) { 277 for (Iterator i = other.hashMap.keySet().iterator(); i.hasNext();) { 278 Object key = i.next(); 279 if (!hashMap.containsKey(key)) { 280 hashMap.put(key, other.hashMap.get(key)); 281 } 282 } 283 } 284 285 public Set getKeys() { 286 return hashMap.keySet(); 287 } 288 289 public void putAll(PdfDictionary dic) { 290 hashMap.putAll(dic.hashMap); 291 } 292 293 public int size() { 294 return hashMap.size(); 295 } 296 297 public boolean contains(PdfName key) { 298 return hashMap.containsKey(key); 299 } 300 301 public String toString() { 302 return "Dictionary of type: " + get(PdfName.TYPE); 303 } 304 305 311 public PdfObject getDirectObject(PdfName key) { 312 return PdfReader.getPdfObject(get(key)); 313 } 314 315 324 public PdfDictionary getAsDict(PdfName key) { 325 PdfDictionary dict = null; 326 PdfObject orig = getDirectObject(key); 327 if (orig != null && orig.isDictionary()) 328 dict = (PdfDictionary) orig; 329 return dict; 330 } 331 332 public PdfArray getAsArray(PdfName key) { 333 PdfArray array = null; 334 PdfObject orig = getDirectObject(key); 335 if (orig != null && orig.isArray()) 336 array = (PdfArray) orig; 337 return array; 338 } 339 340 public PdfStream getAsStream(PdfName key) { 341 PdfStream stream = null; 342 PdfObject orig = getDirectObject(key); 343 if (orig != null && orig.isStream()) 344 stream = (PdfStream) orig; 345 return stream; 346 } 347 348 public PdfString getAsString(PdfName key) { 349 PdfString string = null; 350 PdfObject orig = getDirectObject(key); 351 if (orig != null && orig.isString()) 352 string = (PdfString) orig; 353 return string; 354 } 355 356 public PdfNumber getAsNumber(PdfName key) { 357 PdfNumber number = null; 358 PdfObject orig = getDirectObject(key); 359 if (orig != null && orig.isNumber()) 360 number = (PdfNumber) orig; 361 return number; 362 } 363 364 public PdfName getAsName(PdfName key) { 365 PdfName name = null; 366 PdfObject orig = getDirectObject(key); 367 if (orig != null && orig.isName()) 368 name = (PdfName) orig; 369 return name; 370 } 371 372 public PdfBoolean getAsBoolean(PdfName key) { 373 PdfBoolean bool = null; 374 PdfObject orig = getDirectObject(key); 375 if (orig != null && orig.isBoolean()) 376 bool = (PdfBoolean)orig; 377 return bool; 378 } 379 380 public PdfIndirectReference getAsIndirectObject( PdfName key ) { 381 PdfIndirectReference ref = null; 382 PdfObject orig = get(key); if (orig != null && orig.isIndirect()) 384 ref = (PdfIndirectReference) orig; 385 return ref; 386 } 387 } | Popular Tags |