org.apache.pdfbox.cos.COSObject Java Examples

The following examples show how to use org.apache.pdfbox.cos.COSObject. 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: PDFunction.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Create the correct PD Model function based on the COS base function.
 *
 * @param function The COS function dictionary.
 *
 * @return The PDModel Function object.
 *
 * @throws IOException If we are unable to create the PDFunction object.
 */
public static PDFunction create( COSBase function ) throws IOException
{
    if (function == COSName.IDENTITY)
    {
        return new PDFunctionTypeIdentity(null);
    }
    
    COSBase base = function;
    if (function instanceof COSObject)
    {
        base = ((COSObject) function).getObject();
    }
    if (!(base instanceof COSDictionary))
    {
        throw new IOException("Error: Function must be a Dictionary, but is " +
                base.getClass().getSimpleName());
    }
    COSDictionary functionDictionary = (COSDictionary) base;
    int functionType = functionDictionary.getInt(COSName.FUNCTION_TYPE);
    switch (functionType)
    {
        case 0:
            return new PDFunctionType0(functionDictionary);
        case 2:
            return new PDFunctionType2(functionDictionary);
        case 3:
            return new PDFunctionType3(functionDictionary);
        case 4:
            return new PDFunctionType4(functionDictionary);
        default:
            throw new IOException("Error: Unknown function type " + functionType);
    }
}
 
Example #2
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Parse the values of the trailer dictionary and return the root object.
 *
 * @param trailer The trailer dictionary.
 * @return The parsed root object.
 * @throws IOException If an IO error occurs or if the root object is missing in the trailer dictionary.
 */
protected COSBase parseTrailerValuesDynamically(COSDictionary trailer) throws IOException
{
    // PDFBOX-1557 - ensure that all COSObject are loaded in the trailer
    // PDFBOX-1606 - after securityHandler has been instantiated
    for (COSBase trailerEntry : trailer.getValues())
    {
        if (trailerEntry instanceof COSObject)
        {
            COSObject tmpObj = (COSObject) trailerEntry;
            parseObjectDynamically(tmpObj, false);
        }
    }
    // parse catalog or root object
    COSObject root = trailer.getCOSObject(COSName.ROOT);
    if (root == null)
    {
        throw new IOException("Missing root object specification in trailer.");
    }
    return root.getObject();
}
 
Example #3
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Resolves all not already parsed objects of a dictionary recursively.
 * 
 * @param dictionaryObject dictionary to be parsed
 * @throws IOException if something went wrong
 * 
 */
private void parseDictionaryRecursive(COSObject dictionaryObject) throws IOException
{
    parseObjectDynamically(dictionaryObject, true);
    if (!(dictionaryObject.getObject() instanceof COSDictionary))
    {
        // we can't be lenient here, this is called by prepareDecryption()
        // to get the encryption directory
        throw new IOException("Dictionary object expected at offset " + source.getPosition());
    }
    COSDictionary dictionary = (COSDictionary) dictionaryObject.getObject();
    for (COSBase value : dictionary.getValues())
    {
        if (value instanceof COSObject)
        {
            COSObject object = (COSObject) value;
            if (object.getObject() == null)
            {
                parseDictionaryRecursive(object);
            }
        }
    }
}
 
Example #4
Source File: PDFStreamEngine.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Processes the operators of the given content stream.
 *
 * @param contentStream to content stream to parse.
 * @throws IOException if there is an error reading or parsing the content stream.
 */
private void processStreamOperators(PDContentStream contentStream) throws IOException
{
    List<COSBase> arguments = new ArrayList<COSBase>();
    PDFStreamParser parser = new PDFStreamParser(contentStream);
    Object token = parser.parseNextToken();
    while (token != null)
    {
        if (token instanceof COSObject)
        {
            arguments.add(((COSObject) token).getObject());
        }
        else if (token instanceof Operator)
        {
            processOperator((Operator) token, arguments);
            arguments = new ArrayList<COSBase>();
        }
        else
        {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
}
 
Example #5
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Adds newObject to toBeParsedList if it is not an COSObject or we didn't
 * add this COSObject already (checked via addedObjects). Simple objects are
 * not added because nothing is done with them when toBeParsedList is
 * processed.
 */
private void addNewToList(final Queue<COSBase> toBeParsedList, final COSBase newObject,
        final Set<Long> addedObjects)
{
    if (newObject instanceof COSObject)
    {
        final long objId = getObjectId((COSObject) newObject);
        if (!addedObjects.add(objId))
        {
            return;
        }
        toBeParsedList.add(newObject);
    }
    else if (newObject instanceof COSDictionary || newObject instanceof COSArray)
    {
        toBeParsedList.add(newObject);
    }
}
 
Example #6
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the property list resource with the given name, or null if none exists.
 * 
 * @param name Name of the property list resource.
 * 
 * @return the property list resource of the given name.
 */
public PDPropertyList getProperties(COSName name)
{
    COSObject indirect = getIndirect(COSName.PROPERTIES, name);
    if (cache != null && indirect != null)
    {
        PDPropertyList cached = cache.getProperties(indirect);
        if (cached != null)
        {
            return cached;
        }
    }

    // get the instance
    PDPropertyList propertyList = null;
    COSDictionary dict = (COSDictionary)get(COSName.PROPERTIES, name);
    if (dict != null)
    {
        propertyList = PDPropertyList.create(dict);
    }

    if (cache != null)
    {
        cache.put(indirect, propertyList);
    }
    return propertyList;
}
 
Example #7
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Tells whether the XObject resource with the given name is an image.
 *
 * @param name Name of the XObject resource.
 * @return true if it is an image XObject, false if not.
 */
public boolean isImageXObject(COSName name)
{
    // get the instance
    COSBase value = get(COSName.XOBJECT, name);
    if (value == null)
    {
        return false;
    }
    else if (value instanceof COSObject)
    {
        value = ((COSObject) value).getObject();
    }
    if (!(value instanceof COSStream))
    {
        return false;
    }
    COSStream stream = (COSStream) value;
    return COSName.IMAGE.equals(stream.getCOSName(COSName.SUBTYPE));
}
 
Example #8
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the resource with the given name and kind as an indirect object, or null.
 */
private COSObject getIndirect(COSName kind, COSName name)
{
    COSDictionary dict = (COSDictionary)resources.getDictionaryObject(kind);
    if (dict == null)
    {
        return null;
    }
    COSBase base = dict.getItem(name);
    if (base instanceof COSObject)
    {
        return (COSObject)base;
    }
    // not an indirect object. Resource may have been added at runtime.
    return null;
}
 
Example #9
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 #10
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDXObject getXObject(COSObject indirect) throws IOException
{
    SoftReference<PDXObject> xobject = xobjects.get(indirect);
    if (xobject != null)
    {
        return xobject.get();
    }
    return null;
}
 
Example #11
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDPropertyList getProperties(COSObject indirect)
{
    SoftReference<PDPropertyList> propertyList = properties.get(indirect);
    if (propertyList != null)
    {
        return propertyList.get();
    }
    return null;
}
 
Example #12
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDShading getShading(COSObject indirect) throws IOException
{
    SoftReference<PDShading> shading = shadings.get(indirect);
    if (shading != null)
    {
        return shading.get();
    }
    return null;
}
 
Example #13
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDExtendedGraphicsState getExtGState(COSObject indirect)
{
    SoftReference<PDExtendedGraphicsState> extGState = extGStates.get(indirect);
    if (extGState != null)
    {
        return extGState.get();
    }
    return null;
}
 
Example #14
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDColorSpace getColorSpace(COSObject indirect) throws IOException
{
    SoftReference<PDColorSpace> colorSpace = colorSpaces.get(indirect);
    if (colorSpace != null)
    {
        return colorSpace.get();
    }
    return null;
}
 
Example #15
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDFont getFont(COSObject indirect) throws IOException
{
    SoftReference<PDFont> font = fonts.get(indirect);
    if (font != null)
    {
        return font.get();
    }
    return null;
}
 
Example #16
Source File: PDStructureElement.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Removes an attribute object.
 * 
 * @param attributeObject the attribute object
 */
public void removeAttribute(PDAttributeObject attributeObject)
{
    COSName key = COSName.A;
    COSBase a = this.getCOSObject().getDictionaryObject(key);
    if (a instanceof COSArray)
    {
        COSArray array = (COSArray) a;
        array.remove(attributeObject.getCOSObject());
        if ((array.size() == 2) && (array.getInt(1) == 0))
        {
            this.getCOSObject().setItem(key, array.getObject(0));
        }
    }
    else
    {
        COSBase directA = a;
        if (a instanceof COSObject)
        {
            directA = ((COSObject) a).getObject();
        }
        if (attributeObject.getCOSObject().equals(directA))
        {
            this.getCOSObject().setItem(key, null);
        }
    }
    attributeObject.setStructureElement(null);
}
 
Example #17
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDAbstractPattern getPattern(COSObject indirect) throws IOException
{
    SoftReference<PDAbstractPattern> pattern = patterns.get(indirect);
    if (pattern != null)
    {
        return pattern.get();
    }
    return null;
}
 
Example #18
Source File: PDStructureElement.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the class names together with their revision numbers (C).
 * 
 * @return the class names as a list, never null.
 */
public Revisions<String> getClassNames()
{
    COSName key = COSName.C;
    Revisions<String> classNames = new Revisions<String>();
    COSBase c = this.getCOSObject().getDictionaryObject(key);
    if (c instanceof COSName)
    {
        classNames.addObject(((COSName) c).getName(), 0);
    }
    if (c instanceof COSArray)
    {
        COSArray array = (COSArray) c;
        Iterator<COSBase> it = array.iterator();
        String className = null;
        while (it.hasNext())
        {
            COSBase item = it.next();
            if (item instanceof COSObject)
            {
                item = ((COSObject) item).getObject();
            }
            if (item instanceof COSName)
            {
                className = ((COSName) item).getName();
                classNames.addObject(className, 0);
            }
            else if (item instanceof COSInteger)
            {
                classNames.setRevisionNumber(className, ((COSInteger) item).intValue());
            }
        }
    }
    return classNames;
}
 
Example #19
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Returns the XObject resource with the given name, or null if none exists.
 * 
 * @param name Name of the XObject resource.
 * 
 * @return the XObject resource of the given name.
 * 
 * @throws IOException if something went wrong.
 */
public PDXObject getXObject(COSName name) throws IOException
{
    COSObject indirect = getIndirect(COSName.XOBJECT, name);
    if (cache != null && indirect != null)
    {
        PDXObject cached = cache.getXObject(indirect);
        if (cached != null)
        {
            return cached;
        }
    }

    // get the instance
    PDXObject xobject;
    COSBase value = get(COSName.XOBJECT, name);
    if (value == null)
    {
        xobject = null;
    }
    else if (value instanceof COSObject)
    {
        xobject = PDXObject.createXObject(((COSObject) value).getObject(), this);
    }
    else
    {
        xobject = PDXObject.createXObject(value, this);
    }
    if (cache != null && isAllowedCache(xobject))
    {
        cache.put(indirect, xobject);
    }
    return xobject;
}
 
Example #20
Source File: PDStructureElement.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Removes a class name.
 * 
 * @param className the class name
 */
public void removeClassName(String className)
{
    if (className == null)
    {
        return;
    }
    COSName key = COSName.C;
    COSBase c = this.getCOSObject().getDictionaryObject(key);
    COSName name = COSName.getPDFName(className);
    if (c instanceof COSArray)
    {
        COSArray array = (COSArray) c;
        array.remove(name);
        if ((array.size() == 2) && (array.getInt(1) == 0))
        {
            this.getCOSObject().setItem(key, array.getObject(0));
        }
    }
    else
    {
        COSBase directC = c;
        if (c instanceof COSObject)
        {
            directC = ((COSObject) c).getObject();
        }
        if (name.equals(directC))
        {
            this.getCOSObject().setItem(key, null);
        }
    }
}
 
Example #21
Source File: PDDefaultAppearanceString.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Processes the operators of the given content stream.
 *
 * @param content the content to parse.
 * @throws IOException if there is an error reading or parsing the content stream.
 */
private void processAppearanceStringOperators(byte[] content) throws IOException
{
    List<COSBase> arguments = new ArrayList<COSBase>();
    PDFStreamParser parser = new PDFStreamParser(content);
    Object token = parser.parseNextToken();
    while (token != null)
    {
        if (token instanceof COSObject)
        {
            arguments.add(((COSObject) token).getObject());
        }
        else if (token instanceof Operator)
        {
            processOperator((Operator) token, arguments);
            arguments = new ArrayList<COSBase>();
        }
        else
        {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
}
 
Example #22
Source File: PDOptionalContentProperties.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private COSDictionary toDictionary(COSBase o)
{
    if (o instanceof COSObject)
    {
        return (COSDictionary)((COSObject)o).getObject();
    }
    else
    {
        return (COSDictionary)o;
    }
}
 
Example #23
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the pattern resource with the given name, or null if none exists.
 * 
 * @param name Name of the pattern resource.
 * 
 * @return the pattern resource of the given name.
 * 
 * @throws IOException if something went wrong.
 */
public PDAbstractPattern getPattern(COSName name) throws IOException
{
    COSObject indirect = getIndirect(COSName.PATTERN, name);
    if (cache != null && indirect != null)
    {
        PDAbstractPattern cached = cache.getPattern(indirect);
        if (cached != null)
        {
            return cached;
        }
    }

    // get the instance
    PDAbstractPattern pattern = null;
    COSDictionary dict = (COSDictionary)get(COSName.PATTERN, name);
    if (dict != null)
    {
        pattern = PDAbstractPattern.create(dict);
    }

    if (cache != null)
    {
        cache.put(indirect, pattern);
    }
    return pattern;
}
 
Example #24
Source File: COSWriter.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * This will get the object key for the object.
 *
 * @param obj The object to get the key for.
 *
 * @return The object key for the object.
 */
private COSObjectKey getObjectKey( COSBase obj )
{
    COSBase actual = obj;
    if( actual instanceof COSObject )
    {
        actual = ((COSObject)obj).getObject();
    }
    // PDFBOX-4540: because objectKeys is accessible from outside, it is possible
    // that a COSObject obj is already in the objectKeys map.
    COSObjectKey key = objectKeys.get(obj);
    if( key == null && actual != null )
    {
        key = objectKeys.get(actual);
    }
    if (key == null)
    {
        setNumber(getNumber()+1);
        key = new COSObjectKey(getNumber(),0);
        objectKeys.put(obj, key);
        if( actual != null )
        {
            objectKeys.put(actual, key);
        }
    }
    return key;
}
 
Example #25
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Check if all entries of the pages dictionary are present. Those which can't be dereferenced are removed.
 * 
 * @param root the root dictionary of the pdf
 */
protected void checkPages(COSDictionary root)
{
    if (trailerWasRebuild && root != null)
    {
        // check if all page objects are dereferenced
        COSBase pages = root.getDictionaryObject(COSName.PAGES);
        if (pages instanceof COSDictionary)
        {
            checkPagesDictionary((COSDictionary) pages, new HashSet<COSObject>());
        }
    }
}
 
Example #26
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private COSDictionary retrieveCOSDictionary(COSObject object) throws IOException
{
    COSObjectKey key = new COSObjectKey(object);
    Long offset = bfSearchCOSObjectKeyOffsets.get(key);
    if (offset != null)
    {
        return retrieveCOSDictionary(key, offset);
    }
    return null;
}
 
Example #27
Source File: ResourceCacheWithLimitedImages.java    From pdfcompare with Apache License 2.0 5 votes vote down vote up
@Override
public PDXObject getXObject(COSObject indirect) throws IOException {
    SoftReference<PDXObject> xobject = this.xobjects.get(indirect);
    if (xobject != null) {
        return xobject.get();
    }
    return null;
}
 
Example #28
Source File: COSParser.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void addExcludedToList(COSName[] excludeObjects, COSDictionary dict, final Set<Long> parsedObjects)
{
    if (excludeObjects != null)
    {
        for (COSName objName : excludeObjects)
        {
            COSBase baseObj = dict.getItem(objName);
            if (baseObj instanceof COSObject)
            {
                parsedObjects.add(getObjectId((COSObject) baseObj));
            }
        }
    }
}
 
Example #29
Source File: PdfBoxSignatureService.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private COSObject getByObjectNumber(PDDocument pdDocument, Long objectNumber) {
	List<COSObject> objects = pdDocument.getDocument().getObjects();
	for (COSObject cosObject : objects) {
		if (cosObject.getObjectNumber() == objectNumber) {
			return cosObject;
		}
	}
	return null;
}
 
Example #30
Source File: Overlay.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private List<COSStream> createContentStreamList(COSBase contents) throws IOException
{
    List<COSStream> contentStreams = new ArrayList<COSStream>();
    if (contents == null)
    {
        return contentStreams;
    }
    else if (contents instanceof COSStream)
    {
        contentStreams.add((COSStream) contents);
    }
    else if (contents instanceof COSArray)
    {
        for (COSBase item : (COSArray) contents)
        {
            contentStreams.addAll(createContentStreamList(item));
        }
    }
    else if (contents instanceof COSObject)
    {
        contentStreams.addAll(createContentStreamList(((COSObject) contents).getObject()));
    }
    else
    {
        throw new IOException("Unknown content type: " + contents.getClass().getName());
    }
    return contentStreams;
}