1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.pdfbox.pdmodel; 18 19 import org.apache.pdfbox.cos.COSBase; 20 import org.apache.pdfbox.cos.COSDictionary; 21 import org.apache.pdfbox.cos.COSName; 22 import org.apache.pdfbox.pdmodel.common.COSObjectable; 23 24 /** 25 * This class holds all of the name trees that are available at the document level. 26 * 27 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 28 * @version $Revision: 1.4 $ 29 */ 30 public class PDDocumentNameDictionary implements COSObjectable 31 { 32 private COSDictionary nameDictionary; 33 private PDDocumentCatalog catalog; 34 35 /** 36 * Constructor. 37 * 38 * @param cat The document catalog that this dictionary is part of. 39 */ 40 public PDDocumentNameDictionary( PDDocumentCatalog cat ) 41 { 42 nameDictionary = new COSDictionary(); 43 catalog = cat; 44 } 45 46 /** 47 * Constructor. 48 * 49 * @param cat The document that this dictionary is part of. 50 * @param names The names dictionary. 51 */ 52 public PDDocumentNameDictionary( PDDocumentCatalog cat, COSDictionary names ) 53 { 54 catalog = cat; 55 nameDictionary = names; 56 } 57 58 /** 59 * Convert this standard java object to a COS object. 60 * 61 * @return The cos object that matches this Java object. 62 */ 63 public COSBase getCOSObject() 64 { 65 return nameDictionary; 66 } 67 68 /** 69 * Convert this standard java object to a COS object. 70 * 71 * @return The cos dictionary for this object. 72 */ 73 public COSDictionary getCOSDictionary() 74 { 75 return nameDictionary; 76 } 77 78 /** 79 * Get the destination named tree node. The value in this name tree will be PDDestination 80 * objects. 81 * 82 * @return The destination name tree node. 83 */ 84 public PDDestinationNameTreeNode getDests() 85 { 86 PDDestinationNameTreeNode dests = null; 87 88 COSDictionary dic = (COSDictionary)nameDictionary.getDictionaryObject( COSName.DESTS ); 89 90 //The document catalog also contains the Dests entry sometimes 91 //so check there as well. 92 if( dic == null ) 93 { 94 dic = (COSDictionary)catalog.getCOSDictionary().getDictionaryObject( COSName.DESTS ); 95 } 96 97 if( dic != null ) 98 { 99 dests = new PDDestinationNameTreeNode( dic ); 100 } 101 102 103 return dests; 104 } 105 106 /** 107 * Set the named destinations that are associated with this document. 108 * 109 * @param dests The destination names. 110 */ 111 public void setDests( PDDestinationNameTreeNode dests ) 112 { 113 nameDictionary.setItem( COSName.DESTS, dests ); 114 //The dests can either be in the document catalog or in the 115 //names dictionary, PDFBox will just maintain the one in the 116 //names dictionary for now unless there is a reason to do 117 //something else. 118 //clear the potentially out of date Dests reference. 119 catalog.getCOSDictionary().setItem( COSName.DESTS, (COSObjectable)null); 120 } 121 122 /** 123 * Get the embedded files named tree node. The value in this name tree will be PDComplexFileSpecification 124 * objects. 125 * 126 * @return The embedded files name tree node. 127 */ 128 public PDEmbeddedFilesNameTreeNode getEmbeddedFiles() 129 { 130 PDEmbeddedFilesNameTreeNode retval = null; 131 132 COSDictionary dic = (COSDictionary)nameDictionary.getDictionaryObject( COSName.EMBEDDED_FILES ); 133 134 if( dic != null ) 135 { 136 retval = new PDEmbeddedFilesNameTreeNode( dic ); 137 } 138 139 return retval; 140 } 141 142 /** 143 * Set the named embedded files that are associated with this document. 144 * 145 * @param ef The new embedded files 146 */ 147 public void setEmbeddedFiles( PDEmbeddedFilesNameTreeNode ef ) 148 { 149 nameDictionary.setItem( COSName.EMBEDDED_FILES, ef ); 150 } 151 152 /** 153 * Get the document level javascript entries. The value in this name tree will be PDTextStream. 154 * 155 * @return The document level named javascript. 156 */ 157 public PDJavascriptNameTreeNode getJavaScript() 158 { 159 PDJavascriptNameTreeNode retval = null; 160 161 COSDictionary dic = (COSDictionary)nameDictionary.getDictionaryObject( COSName.JAVA_SCRIPT ); 162 163 if( dic != null ) 164 { 165 retval = new PDJavascriptNameTreeNode( dic ); 166 } 167 168 return retval; 169 } 170 171 /** 172 * Set the named javascript entries that are associated with this document. 173 * 174 * @param js The new Javascript entries. 175 */ 176 public void setJavascript( PDJavascriptNameTreeNode js ) 177 { 178 nameDictionary.setItem( COSName.JAVA_SCRIPT, js ); 179 } 180 }