⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cosdictionary.java

📁 非常有用的操作pdf文件的java源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param defaultValue The default value to return.
     * @return The name converted to a string.
     * @throws IOException If there is an error converting to a date.
     */
    public Calendar getEmbeddedDate( String embedded, COSName key, Calendar defaultValue ) throws IOException
    {
        Calendar retval = defaultValue;
        COSDictionary eDic = (COSDictionary)getDictionaryObject( embedded );
        if( eDic != null )
        {
            retval = eDic.getDate( key, defaultValue );
        }
        return retval;
    }

    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be a cos boolean and convert it to a primitive boolean.
     *
     * @param key The key to the item in the dictionary.
     * @param defaultValue The value returned if the entry is null.
     *
     * @return The value converted to a boolean.
     */
    public boolean getBoolean( String key, boolean defaultValue )
    {
        return getBoolean( COSName.getPDFName( key ), defaultValue );
    }

    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be a COSBoolean and convert it to a primitive boolean.
     *
     * @param key The key to the item in the dictionary.
     * @param defaultValue The value returned if the entry is null.
     *
     * @return The entry converted to a boolean.
     */
    public boolean getBoolean( COSName key, boolean defaultValue )
    {
        boolean retval = defaultValue;
        COSBoolean bool = (COSBoolean)getDictionaryObject( key );
        if( bool != null )
        {
            retval = bool.getValue();
        }
        return retval;
    }
    
    /**
     * Get an integer from an embedded dictionary.  Useful for 1-1 mappings.  default:-1
     * 
     * @param embeddedDictionary The name of the embedded dictionary.
     * @param key The key in the embedded dictionary.
     * 
     * @return The value of the embedded integer.
     */
    public int getEmbeddedInt( String embeddedDictionary, String key )
    {
        return getEmbeddedInt( embeddedDictionary, COSName.getPDFName( key ) );
    }
    
    /**
     * Get an integer from an embedded dictionary.  Useful for 1-1 mappings.  default:-1
     * 
     * @param embeddedDictionary The name of the embedded dictionary.
     * @param key The key in the embedded dictionary.
     * 
     * @return The value of the embedded integer.
     */
    public int getEmbeddedInt( String embeddedDictionary, COSName key )
    {
        return getEmbeddedInt( embeddedDictionary, key, -1 );
    }
    
    /**
     * Get an integer from an embedded dictionary.  Useful for 1-1 mappings.
     * 
     * @param embeddedDictionary The name of the embedded dictionary.
     * @param key The key in the embedded dictionary.
     * @param defaultValue The value if there is no embedded dictionary or it does not contain the key.
     * 
     * @return The value of the embedded integer.
     */
    public int getEmbeddedInt( String embeddedDictionary, String key, int defaultValue )
    {
        return getEmbeddedInt( embeddedDictionary, COSName.getPDFName( key ), defaultValue );
    }
    
    
    /**
     * Get an integer from an embedded dictionary.  Useful for 1-1 mappings.
     * 
     * @param embeddedDictionary The name of the embedded dictionary.
     * @param key The key in the embedded dictionary.
     * @param defaultValue The value if there is no embedded dictionary or it does not contain the key.
     * 
     * @return The value of the embedded integer.
     */
    public int getEmbeddedInt( String embeddedDictionary, COSName key, int defaultValue )
    {
        int retval = defaultValue;
        COSDictionary embedded = (COSDictionary)getDictionaryObject( embeddedDictionary );
        if( embedded != null )
        {
            retval = embedded.getInt( key, defaultValue );
        }
        return retval;
    }
    
    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an int.  -1 is returned if there is no value.
     *
     * @param key The key to the item in the dictionary.
     * @return The integer value.
     */
    public int getInt( String key )
    {
        return getInt( COSName.getPDFName( key ) );
    }

    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an int.  -1 is returned if there is no value.
     *
     * @param key The key to the item in the dictionary.
     * @return The integer value..
     */
    public int getInt( COSName key )
    {
        return getInt( key, -1 );
    }
    
    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an integer.  If the dictionary value is null then the
     * default Value will be returned.
     *
     * @param keyList The key to the item in the dictionary.
     * @param defaultValue The value to return if the dictionary item is null.
     * @return The integer value.
     */
    public int getInt( String[] keyList, int defaultValue )
    {
        int retval = defaultValue;
        COSNumber obj = (COSNumber)getDictionaryObject( keyList );
        if( obj != null )
        {
            retval = obj.intValue();
        }
        return retval;
    }
    
    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an integer.  If the dictionary value is null then the
     * default Value will be returned.
     *
     * @param key The key to the item in the dictionary.
     * @param defaultValue The value to return if the dictionary item is null.
     * @return The integer value.
     */
    public int getInt( String key, int defaultValue )
    {
        return getInt( new String []{ key }, defaultValue );
    }

    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an integer.  If the dictionary value is null then the
     * default Value will be returned.
     *
     * @param key The key to the item in the dictionary.
     * @param defaultValue The value to return if the dictionary item is null.
     * @return The integer value.
     */
    public int getInt( COSName key, int defaultValue )
    {
        return getInt(key.getName(), defaultValue );
    }
    
    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an long.  -1 is returned if there is no value.
     *
     * @param key The key to the item in the dictionary.
     * 
     * @return The long value.
     */
    public long getLong( String key )
    {
        return getLong( COSName.getPDFName( key ) );
    }

    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an long.  -1 is returned if there is no value.
     *
     * @param key The key to the item in the dictionary.
     * @return The long value.
     */
    public long getLong( COSName key )
    {
        return getLong( key, -1L );
    }
    
    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an long.  If the dictionary value is null then the
     * default Value will be returned.
     *
     * @param keyList The key to the item in the dictionary.
     * @param defaultValue The value to return if the dictionary item is null.
     * @return The long value.
     */
    public long getLong( String[] keyList, long defaultValue )
    {
        long retval = defaultValue;
        COSNumber obj = (COSNumber)getDictionaryObject( keyList );
        if( obj != null )
        {
            retval = obj.longValue();
        }
        return retval;
    }
    
    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an integer.  If the dictionary value is null then the
     * default Value will be returned.
     *
     * @param key The key to the item in the dictionary.
     * @param defaultValue The value to return if the dictionary item is null.
     * @return The integer value.
     */
    public long getLong( String key, long defaultValue )
    {
        return getLong( new String []{ key }, defaultValue );
    }

    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an integer.  If the dictionary value is null then the
     * default Value will be returned.
     *
     * @param key The key to the item in the dictionary.
     * @param defaultValue The value to return if the dictionary item is null.
     * @return The integer value.
     */
    public long getLong( COSName key, long defaultValue )
    {
        return getLong(key.getName(), defaultValue );
    }
    
    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an int.  -1 is returned if there is no value.
     *
     * @param key The key to the item in the dictionary.
     * @return The float value.
     */
    public float getFloat( String key )
    {
        return getFloat( COSName.getPDFName( key ) );
    }

    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an float.  -1 is returned if there is no value.
     *
     * @param key The key to the item in the dictionary.
     * @return The float value.
     */
    public float getFloat( COSName key )
    {
        return getFloat( key, -1 );
    }
    
    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be a float.  If the dictionary value is null then the
     * default Value will be returned.
     *
     * @param key The key to the item in the dictionary.
     * @param defaultValue The value to return if the dictionary item is null.
     * @return The float value.
     */
    public float getFloat( String key, float defaultValue )
    {
        return getFloat( COSName.getPDFName( key ), defaultValue );
    }

    /**
     * This is a convenience method that will get the dictionary object that
     * is expected to be an float.  If the dictionary value is null then the
     * default Value will be returned.
     *
     * @param key The key to the item in the dictionary.
     * @param defaultValue The value to return if the dictionary item is null.
     * @return The float value.
     */
    public float getFloat( COSName key, float defaultValue )
    {
        float retval = defaultValue;
        COSNumber obj = (COSNumber)getDictionaryObject( key );
        if( obj != null )
        {
            retval = obj.floatValue();
        }
        return retval;
    }

    /**
     * This will remove an item for the dictionary.  This
     * will do nothing of the object does not exist.
     *
     * @param key The key to the item to remove from the dictionary.
     */
    public void removeItem( COSName key )
    {
        keys.remove( key );
        items.remove( key );
    }

    /**
     * This will do a lookup into the dictionary.
     *
     * @param key The key to the object.
     *
     * @return The item that matches the key.
     */
    public COSBase getItem( COSName key )
    {
        return (COSBase)items.get( key );
    }
    
    



    /**
     * This will get the keys for all objects in the dictionary in the sequence that
     * they were added.
     *
     * @return a list of the keys in the sequence of insertion
     *
     */
    public List keyList()
    {
        return keys;
    }

    /**
     * This will get all of the values for the dictionary.
     *
     * @return All the values for the dictionary.
     */
    public Collection getValues()
    {
        return items.values();
    }

    /**
     * visitor pattern double dispatch method.
     *
     * @param visitor The object to notify when visiting this object.
     * @return The object that the visitor returns.
     *
     * @throws COSVisitorException If there is an error visiting this object.
     */
    public Object accept(ICOSVisitor  visitor) throws COSVisitorException
    {
        return visitor.visitFromDictionary(this);
    }

    /**
     * This will add all of the dictionarys keys/values to this dictionary.
     *
     * @param dic The dic to get the keys from.
     */
    public void addAll( COSDictionary dic )
    {
        Iterator dicKeys = dic.keyList().iterator();
        while( dicKeys.hasNext() )
        {
            COSName key = (COSName)dicKeys.next();
            COSBase value = dic.getItem( key );
            setItem( key, value );
        }
    }
    
    /**
     * This will add all of the dictionarys keys/values to this dictionary, but only
     * if they don't already exist.  If a key already exists in this dictionary then 
     * nothing is changed.
     *
     * @param dic The dic to get the keys from.
     */
    public void mergeInto( COSDictionary dic )
    {
        Iterator dicKeys = dic.keyList().iterator();
        while( dicKeys.hasNext() )
        {
            COSName key = (COSName)dicKeys.next();
            COSBase value = dic.getItem( key );
            if( getItem( key ) == null )
            {
                setItem( key, value );
            }
        }
    }
    
    /**
     * Nice method, gives you every object you want
     * Arrays works properly too. Try "P/Annots/[k]/Rect"
     * where k means the index of the Annotsarray.
     *
     * @param objPath the relative path to the object.
     * @return the object
     */
    public COSBase getObjectFromPath(String objPath) 
    {
        COSBase retval = null;
        String[] path = objPath.split(PATH_SEPARATOR);
        retval = this;

        for (int i = 0; i < path.length; i++)
        {
            if(retval instanceof COSArray)
            {
                int idx = new Integer(path[i].replaceAll("\\[","").replaceAll("\\]","")).intValue();
                retval = ((COSArray)retval).getObject(idx);
            }
            else if (retval instanceof COSDictionary)
            {
                retval = ((COSDictionary)retval).getDictionaryObject( path[i] );
            }
        }
        return retval;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -