Search in sources :

Example 1 with ResourceCache

use of com.tom_roush.pdfbox.pdmodel.ResourceCache in project PdfBox-Android by TomRoush.

the class PDXObject method createXObject.

/**
 * Creates a new XObject instance of the appropriate type for the COS stream.
 *
 * @param base The stream which is wrapped by this XObject.
 * @param resources
 * @return A new XObject instance.
 * @throws java.io.IOException if there is an error creating the XObject.
 */
public static PDXObject createXObject(COSBase base, PDResources resources) throws IOException {
    if (base == null) {
        // TODO throw an exception?
        return null;
    }
    if (!(base instanceof COSStream)) {
        throw new IOException("Unexpected object type: " + base.getClass().getName());
    }
    COSStream stream = (COSStream) base;
    String subtype = stream.getNameAsString(COSName.SUBTYPE);
    if (COSName.IMAGE.getName().equals(subtype)) {
        return new PDImageXObject(new PDStream(stream), resources);
    } else if (COSName.FORM.getName().equals(subtype)) {
        ResourceCache cache = resources != null ? resources.getResourceCache() : null;
        COSDictionary group = (COSDictionary) stream.getDictionaryObject(COSName.GROUP);
        if (group != null && COSName.TRANSPARENCY.equals(group.getCOSName(COSName.S))) {
            return new PDTransparencyGroup(stream, cache);
        }
        return new PDFormXObject(stream, cache);
    } else if (COSName.PS.getName().equals(subtype)) {
        return new PDPostScriptXObject(stream);
    } else {
        throw new IOException("Invalid XObject Subtype: " + subtype);
    }
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSStream(com.tom_roush.pdfbox.cos.COSStream) PDImageXObject(com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) IOException(java.io.IOException) PDStream(com.tom_roush.pdfbox.pdmodel.common.PDStream) ResourceCache(com.tom_roush.pdfbox.pdmodel.ResourceCache)

Example 2 with ResourceCache

use of com.tom_roush.pdfbox.pdmodel.ResourceCache in project PdfBox-Android by TomRoush.

the class PDColorSpace method createFromCOSObject.

private static PDColorSpace createFromCOSObject(COSObject colorSpace, PDResources resources) throws IOException {
    PDColorSpace cs;
    if (resources != null && resources.getResourceCache() != null) {
        ResourceCache resourceCache = resources.getResourceCache();
        cs = resourceCache.getColorSpace(colorSpace);
        if (cs != null) {
            return cs;
        }
    }
    cs = create(colorSpace.getObject(), resources);
    if (resources != null && resources.getResourceCache() != null && cs != null) {
        ResourceCache resourceCache = resources.getResourceCache();
        resourceCache.put(colorSpace, cs);
    }
    return cs;
}
Also used : ResourceCache(com.tom_roush.pdfbox.pdmodel.ResourceCache)

Aggregations

ResourceCache (com.tom_roush.pdfbox.pdmodel.ResourceCache)2 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 COSStream (com.tom_roush.pdfbox.cos.COSStream)1 PDStream (com.tom_roush.pdfbox.pdmodel.common.PDStream)1 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)1 PDTransparencyGroup (com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)1 PDImageXObject (com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject)1 IOException (java.io.IOException)1