Java Code Examples for org.apache.pdfbox.cos.COSDictionary#entrySet()

The following examples show how to use org.apache.pdfbox.cos.COSDictionary#entrySet() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Adds the given resource if it does not already exist.
 */
private COSName add(COSName kind, String prefix, COSObjectable object)
{
    // return the existing key if the item exists already
    COSDictionary dict = (COSDictionary)resources.getDictionaryObject(kind);
    if (dict != null && dict.containsValue(object.getCOSObject()))
    {
        return dict.getKeyForValue(object.getCOSObject());
    }

    // PDFBOX-4509: It could exist as an indirect object, happens when a font is taken from the 
    // AcroForm default resources of a loaded PDF.
    if (dict != null && COSName.FONT.equals(kind))
    {
        for (Map.Entry<COSName, COSBase> entry : dict.entrySet())
        {
            if (entry.getValue() instanceof COSObject &&
                object.getCOSObject() == ((COSObject) entry.getValue()).getObject())
            {
                return entry.getKey();
            }
        }
    }

    // add the item with a new key
    COSName name = createKey(kind, prefix);
    put(kind, name, object);
    return name;
}
 
Example 2
Source File: LayerUtility.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void transferDict(COSDictionary orgDict, COSDictionary targetDict,
        Set<String> filter, boolean inclusive) throws IOException
{
    for (Map.Entry<COSName, COSBase> entry : orgDict.entrySet())
    {
        COSName key = entry.getKey();
        if (inclusive && !filter.contains(key.getName()))
        {
            continue;
        }
        else if (!inclusive && filter.contains(key.getName()))
        {
            continue;
        }
        targetDict.setItem(key,
                cloner.cloneForNewDocument(entry.getValue()));
    }
}
 
Example 3
Source File: PDFXRefStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Copy all Trailer Information to this file.
 * 
 * @param trailerDict dictionary to be added as trailer info
 */
public void addTrailerInfo(COSDictionary trailerDict)
{
    Set<Entry<COSName, COSBase>> entrySet = trailerDict.entrySet();
    for ( Entry<COSName, COSBase> entry : entrySet )
    {
        COSName key = entry.getKey();
        if (COSName.INFO.equals(key) || COSName.ROOT.equals(key) || COSName.ENCRYPT.equals(key) 
                || COSName.ID.equals(key) || COSName.PREV.equals(key))
        {
            stream.setItem(key, entry.getValue());
        }
    }
}
 
Example 4
Source File: SecurityHandler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will decrypt a dictionary.
 *
 * @param dictionary The dictionary to decrypt.
 * @param objNum The object number.
 * @param genNum The object generation number.
 *
 * @throws IOException If there is an error creating a new string.
 */
private void decryptDictionary(COSDictionary dictionary, long objNum, long genNum) throws IOException
{
    if (dictionary.getItem(COSName.CF) != null)
    {
        // PDFBOX-2936: avoid orphan /CF dictionaries found in US govt "I-" files
        return;
    }
    COSBase type = dictionary.getDictionaryObject(COSName.TYPE);
    boolean isSignature = COSName.SIG.equals(type) || COSName.DOC_TIME_STAMP.equals(type) ||
            // PDFBOX-4466: /Type is optional, see
            // https://ec.europa.eu/cefdigital/tracker/browse/DSS-1538
            (dictionary.getDictionaryObject(COSName.CONTENTS) instanceof COSString && 
             dictionary.getDictionaryObject(COSName.BYTERANGE) instanceof COSArray);
    for (Map.Entry<COSName, COSBase> entry : dictionary.entrySet())
    {
        if (isSignature && COSName.CONTENTS.equals(entry.getKey()))
        {
            // do not decrypt the signature contents string
            continue;
        }
        COSBase value = entry.getValue();
        // within a dictionary only the following kind of COS objects have to be decrypted
        if (value instanceof COSString || value instanceof COSArray || value instanceof COSDictionary)
        {
            decrypt(value, objNum, genNum);
        }
    }
}
 
Example 5
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void mergeRoleMap(PDStructureTreeRoot srcStructTree, PDStructureTreeRoot destStructTree)
{
    COSDictionary srcDict = srcStructTree.getCOSObject().getCOSDictionary(COSName.ROLE_MAP);
    COSDictionary destDict = destStructTree.getCOSObject().getCOSDictionary(COSName.ROLE_MAP);
    if (srcDict == null)
    {
        return;
    }
    if (destDict == null)
    {
        destStructTree.getCOSObject().setItem(COSName.ROLE_MAP, srcDict); // clone not needed
        return;
    }
    for (Map.Entry<COSName, COSBase> entry : srcDict.entrySet())
    {
        COSBase destValue = destDict.getDictionaryObject(entry.getKey());
        if (destValue != null && destValue.equals(entry.getValue()))
        {
            // already exists, but identical
            continue;
        }
        if (destDict.containsKey(entry.getKey()))
        {
            LOG.warn("key " + entry.getKey() + " already exists in destination RoleMap");
        }
        else
        {
            destDict.setItem(entry.getKey(), entry.getValue());
        }
    }
}
 
Example 6
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will add all of the dictionaries keys/values to this dictionary, but
 * only if they are not in an exclusion list and if they don't already
 * exist. If a key already exists in this dictionary then nothing is
 * changed.
 *
 * @param src The source dictionary to get the keys/values from.
 * @param dst The destination dictionary to merge the keys/values into.
 * @param exclude Names of keys that shall be skipped.
 */
private void mergeInto(COSDictionary src, COSDictionary dst, Set<COSName> exclude)
{
    for (Map.Entry<COSName, COSBase> entry : src.entrySet())
    {
        if (!exclude.contains(entry.getKey()) && !dst.containsKey(entry.getKey()))
        {
            dst.setItem(entry.getKey(), entry.getValue());
        }
    }
}
 
Example 7
Source File: ContentStreamWriter.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private void writeObject( Object o ) throws IOException
{
    if( o instanceof COSString )
    {
        COSWriter.writeString((COSString)o, output);
        output.write( SPACE );
    }
    else if( o instanceof COSFloat )
    {
        ((COSFloat)o).writePDF( output );
        output.write( SPACE );
    }
    else if( o instanceof COSInteger )
    {
        ((COSInteger)o).writePDF( output );
        output.write( SPACE );
    }
    else if( o instanceof COSBoolean )
    {
        ((COSBoolean)o).writePDF( output );
        output.write( SPACE );
    }
    else if( o instanceof COSName )
    {
        ((COSName)o).writePDF( output );
        output.write( SPACE );
    }
    else if( o instanceof COSArray )
    {
        COSArray array = (COSArray)o;
        output.write(COSWriter.ARRAY_OPEN);
        for( int i=0; i<array.size(); i++ )
        {
            writeObject( array.get( i ) );
            output.write( SPACE );
        }

        output.write( COSWriter.ARRAY_CLOSE );
    }
    else if( o instanceof COSDictionary )
    {
        COSDictionary obj = (COSDictionary)o;
        output.write( COSWriter.DICT_OPEN );
        for (Map.Entry<COSName, COSBase> entry : obj.entrySet())
        {
            if (entry.getValue() != null)
            {
                writeObject( entry.getKey() );
                output.write( SPACE );
                writeObject( entry.getValue() );
                output.write( SPACE );
            }
        }
        output.write( COSWriter.DICT_CLOSE );
        output.write( SPACE );
    }
    else if( o instanceof Operator)
    {
        Operator op = (Operator)o;
        if (op.getName().equals(OperatorName.BEGIN_INLINE_IMAGE))
        {
            output.write(OperatorName.BEGIN_INLINE_IMAGE.getBytes(Charsets.ISO_8859_1));
            COSDictionary dic = op.getImageParameters();
            for( COSName key : dic.keySet() )
            {
                Object value = dic.getDictionaryObject( key );
                key.writePDF( output );
                output.write( SPACE );
                writeObject( value );
                output.write( EOL );
            }
            output.write(OperatorName.BEGIN_INLINE_IMAGE_DATA.getBytes(Charsets.ISO_8859_1));
            output.write( EOL );
            output.write( op.getImageData() );
            output.write( EOL );
            output.write(OperatorName.END_INLINE_IMAGE.getBytes(Charsets.ISO_8859_1));
            output.write( EOL );
        }
        else
        {
            output.write( op.getName().getBytes(Charsets.ISO_8859_1) );
            output.write( EOL );
        }
    }
    else
    {
        throw new IOException( "Error:Unknown type in content stream:" + o );
    }
}