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

The following examples show how to use org.apache.pdfbox.cos.COSDictionary#containsKey() . 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: COSParser.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Tell if the dictionary is an info dictionary.
 * 
 * @param dictionary
 * @return true if the given dictionary is an info dictionary
 */
private boolean isInfo(COSDictionary dictionary)
{
    if (dictionary.containsKey(COSName.PARENT) || dictionary.containsKey(COSName.A) || dictionary.containsKey(COSName.DEST))
    {
        return false;
    }
    if (!dictionary.containsKey(COSName.MOD_DATE) && !dictionary.containsKey(COSName.TITLE)
            && !dictionary.containsKey(COSName.AUTHOR)
            && !dictionary.containsKey(COSName.SUBJECT)
            && !dictionary.containsKey(COSName.KEYWORDS)
            && !dictionary.containsKey(COSName.CREATOR)
            && !dictionary.containsKey(COSName.PRODUCER)
            && !dictionary.containsKey(COSName.CREATION_DATE))
    {
        return false;
    }
    return true;
}
 
Example 2
Source File: PDDocumentNameDestinationDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the destination corresponding to the parameter.
 *
 * @param name The destination name.
 * @return The destination for that name, or null if there isn't any.
 * 
 * @throws IOException if something goes wrong when creating the destination object.
 */
public PDDestination getDestination(String name) throws IOException
{
    COSBase item = nameDictionary.getDictionaryObject(name);

    // "The value of this entry shall be a dictionary in which each key is a destination name
    // and the corresponding value is either an array defining the destination (...) 
    // or a dictionary with a D entry whose value is such an array."                
    if (item instanceof COSArray)
    {
        return PDDestination.create(item);
    }
    else if (item instanceof COSDictionary)
    {
        COSDictionary dict = (COSDictionary) item;
        if (dict.containsKey(COSName.D))
        {
            return PDDestination.create(dict.getDictionaryObject(COSName.D));
        }
    }
    return null;
}
 
Example 3
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns a unique key for a new resource.
 */
private COSName createKey(COSName kind, String prefix)
{
    COSDictionary dict = (COSDictionary)resources.getDictionaryObject(kind);
    if (dict == null)
    {
        return COSName.getPDFName(prefix + 1);
    }

    // find a unique key
    String key;
    int n = dict.keySet().size();
    do
    {
        ++n;
        key = prefix + n;
    }
    while (dict.containsKey(key));
    return COSName.getPDFName(key);
}
 
Example 4
Source File: PdfUtils.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
/**
 * Generates the list of "PARAMETER" entries in the WKT.
 * 
 * @param projectionDictionary the GeoPDF projection dictionary
 * 
 * @returns string of WKT parameters
 */
private static String generateWKTParameters(COSDictionary projectionDictionary) throws IOException {
    // Set up the projection parameters
    Properties parameters = new Properties();

    COSDictionaryMap<String, Object> dictionaryMap = COSDictionaryMap.convertBasicTypesToMap(projectionDictionary);

    if (projectionDictionary.containsKey("CentralMeridian")) {
        parameters.put("Central_Meridian", (String) dictionaryMap.get("CentralMeridian"));
    }
    if (projectionDictionary.containsKey("OriginLatitude")) {
        parameters.put("Latitude_Of_Origin", (String) dictionaryMap.get("OriginLatitude"));
    }
    if (projectionDictionary.containsKey("StandardParallelOne")) {
        parameters.put("Standard_Parallel_1", (String) dictionaryMap.get("StandardParallelOne"));
    }
    if (projectionDictionary.containsKey("StandardParallelTwo")) {
        parameters.put("Standard_Parallel_2", (String) dictionaryMap.get("StandardParallelTwo"));
    }
    if (projectionDictionary.containsKey("FalseEasting")) {
        parameters.put("False_Easting", (String) dictionaryMap.get("FalseEasting"));
    }
    if (projectionDictionary.containsKey("FalseNorthing")) {
        parameters.put("False_Northing", (String) dictionaryMap.get("FalseNorthing"));
    }
    if (projectionDictionary.containsKey("ScaleFactor")) {
        parameters.put("Scale_Factor", (String) dictionaryMap.get("ScaleFactor"));
    }

    return parameters.entrySet().stream()
            .map(entry -> "PARAMETER[\"" + entry.getKey() + "\", " + entry.getValue() + "]")
            .collect(Collectors.joining(","));
}
 
Example 5
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * The initial parse will first parse only the trailer, the xrefstart and all xref tables to have a pointer (offset)
 * to all the pdf's objects. It can handle linearized pdfs, which will have an xref at the end pointing to an xref
 * at the beginning of the file. Last the root object is parsed.
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException If something went wrong.
 */
protected void initialParse() throws IOException
{
    COSDictionary trailer = retrieveTrailer();

    COSBase base = parseTrailerValuesDynamically(trailer);
    if (!(base instanceof COSDictionary))
    {
        throw new IOException("Expected root dictionary, but got this: " + base);
    }
    COSDictionary root = (COSDictionary) base;
    // in some pdfs the type value "Catalog" is missing in the root object
    if (isLenient() && !root.containsKey(COSName.TYPE))
    {
        root.setItem(COSName.TYPE, COSName.CATALOG);
    }
    // parse all objects, starting at the root dictionary
    parseDictObjects(root, (COSName[]) null);
    // parse all objects of the info dictionary
    COSBase infoBase = trailer.getDictionaryObject(COSName.INFO);
    if (infoBase instanceof COSDictionary)
    {
        parseDictObjects((COSDictionary) infoBase, (COSName[]) null);
    }
    // check pages dictionaries
    checkPages(root);
    if (!(root.getDictionaryObject(COSName.PAGES) instanceof COSDictionary))
    {
        throw new IOException("Page tree root must be a dictionary");
    }
    document.setDecrypted();
    initialParseDone = true;
}
 
Example 6
Source File: PDPageTree.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns true if the node is a page tree node (i.e. and intermediate).
 */
private boolean isPageTreeNode(COSDictionary node )
{
    // some files such as PDFBOX-2250-229205.pdf don't have Pages set as the Type, so we have
    // to check for the presence of Kids too
    return node != null &&
           (node.getCOSName(COSName.TYPE) == COSName.PAGES || node.containsKey(COSName.KIDS));
}
 
Example 7
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 8
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 9
Source File: PDFieldFactory.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a COSField subclass from the given field.
 *
 * @param form the form that the field is part of
 * @param field the dictionary representing a field element
 * @param parent the parent node of the node to be created 
 * @return the corresponding PDField instance
 */
static PDField createField(PDAcroForm form, COSDictionary field, PDNonTerminalField parent)
{
    String fieldType = findFieldType(field);
    
    // Test if we have a non terminal field first as it might have
    // properties which do apply to other fields
    // A non terminal fields has Kids entries which do have
    // a field name (other than annotations)
    if (field.containsKey(COSName.KIDS))
    {
        COSArray kids = (COSArray) field.getDictionaryObject(COSName.KIDS);
        if (kids != null && kids.size() > 0)
        {
            for (int i = 0; i < kids.size(); i++)
            {
                COSBase kid = kids.getObject(i);
                if (kid instanceof COSDictionary && ((COSDictionary) kid).getString(COSName.T) != null)
                {
                    return new PDNonTerminalField(form, field, parent);
                }
            }
        }
    } 
    
    if (FIELD_TYPE_CHOICE.equals(fieldType))
    {
        return createChoiceSubType(form, field, parent);
    }
    else if (FIELD_TYPE_TEXT.equals(fieldType))
    {
        return new PDTextField(form, field, parent);
    }
    else if (FIELD_TYPE_SIGNATURE.equals(fieldType))
    {
        return new PDSignatureField(form, field, parent);
    }
    else if (FIELD_TYPE_BUTTON.equals(fieldType))
    {
        return createButtonSubType(form, field, parent);
    }
    else
    {
        // an erroneous non-field object, see PDFBOX-2885
        return null;
    }
}
 
Example 10
Source File: FDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Tell if the dictionary is a FDF catalog.
 *
 * @param dictionary
 * @return
 */
@Override
protected final boolean isCatalog(COSDictionary dictionary)
{
    return dictionary.containsKey(COSName.FDF);
}