Search in sources :

Example 1 with PDXObject

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

the class PDResources method getXObject.

/**
 * 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;
}
Also used : COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Example 2 with PDXObject

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

the class PageDrawer method hasBlendMode.

private boolean hasBlendMode(PDTransparencyGroup group, Set<COSBase> groupsDone) {
    if (groupsDone.contains(group.getCOSObject())) {
        // The group was already processed. Avoid endless recursion.
        return false;
    }
    groupsDone.add(group.getCOSObject());
    PDResources resources = group.getResources();
    if (resources == null) {
        return false;
    }
    for (COSName name : resources.getExtGStateNames()) {
        PDExtendedGraphicsState extGState = resources.getExtGState(name);
        if (extGState == null) {
            continue;
        }
        BlendMode blendMode = extGState.getBlendMode();
        if (blendMode != BlendMode.NORMAL) {
            return true;
        }
    }
    // Recursively process nested transparency groups
    for (COSName name : resources.getXObjectNames()) {
        PDXObject xObject;
        try {
            xObject = resources.getXObject(name);
        } catch (IOException ex) {
            continue;
        }
        if (xObject instanceof PDTransparencyGroup && hasBlendMode((PDTransparencyGroup) xObject, groupsDone)) {
            return true;
        }
    }
    return false;
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSName(com.tom_roush.pdfbox.cos.COSName) PDExtendedGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) BlendMode(com.tom_roush.pdfbox.pdmodel.graphics.blend.BlendMode) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Example 3 with PDXObject

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

the class PDObjectReference method getReferencedObject.

/**
 * Gets a higher-level object for the referenced object.
 * Currently this method may return a {@link PDAnnotation},
 * a {@link PDXObject} or <code>null</code>.
 *
 * @return a higher-level object for the referenced object
 */
public COSObjectable getReferencedObject() {
    COSBase obj = this.getCOSObject().getDictionaryObject(COSName.OBJ);
    if (!(obj instanceof COSDictionary)) {
        return null;
    }
    try {
        if (obj instanceof COSStream) {
            // <-- TODO: valid?
            PDXObject xobject = PDXObject.createXObject(obj, null);
            if (xobject != null) {
                return xobject;
            }
        }
        COSDictionary objDictionary = (COSDictionary) obj;
        PDAnnotation annotation = PDAnnotation.createAnnotation(obj);
        /*
             * COSName.TYPE is optional, so if annotation is of type unknown and
             * COSName.TYPE is not COSName.ANNOT it still may be an annotation.
             * TODO shall we return the annotation object instead of null?
             * what else can be the target of the object reference?
             */
        if (!(annotation instanceof PDAnnotationUnknown) || COSName.ANNOT.equals(objDictionary.getDictionaryObject(COSName.TYPE))) {
            return annotation;
        }
    } catch (IOException exception) {
    // this can only happen if the target is an XObject.
    }
    return null;
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException) PDAnnotationUnknown(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationUnknown) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Example 4 with PDXObject

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

the class PDAcroForm method resolveNeedsTranslation.

/**
 * Check if there is a translation needed to place the annotations content.
 *
 * @param appearanceStream
 * @return the need for a translation transformation.
 */
private boolean resolveNeedsTranslation(PDAppearanceStream appearanceStream) {
    boolean needsTranslation = true;
    PDResources resources = appearanceStream.getResources();
    if (resources != null && resources.getXObjectNames().iterator().hasNext()) {
        Iterator<COSName> xObjectNames = resources.getXObjectNames().iterator();
        while (xObjectNames.hasNext()) {
            try {
                // if the BBox of the PDFormXObject does not start at 0,0
                // there is no need do translate as this is done by the BBox definition.
                PDXObject xObject = resources.getXObject(xObjectNames.next());
                if (xObject instanceof PDFormXObject) {
                    PDRectangle bbox = ((PDFormXObject) xObject).getBBox();
                    float llX = bbox.getLowerLeftX();
                    float llY = bbox.getLowerLeftY();
                    if (Float.compare(llX, 0) != 0 && Float.compare(llY, 0) != 0) {
                        needsTranslation = false;
                    }
                }
            } catch (IOException e) {
            // we can safely ignore the exception here
            // as this might only cause a misplacement
            }
        }
        return needsTranslation;
    }
    return true;
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Example 5 with PDXObject

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

the class DrawObject method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
    if (arguments.isEmpty()) {
        throw new MissingOperandException(operator, arguments);
    }
    COSBase base0 = arguments.get(0);
    if (!(base0 instanceof COSName)) {
        return;
    }
    COSName name = (COSName) base0;
    PDXObject xobject = context.getResources().getXObject(name);
    ((PDFMarkedContentExtractor) context).xobject(xobject);
    if (xobject instanceof PDFormXObject) {
        try {
            context.increaseLevel();
            if (context.getLevel() > 25) {
                Log.e("PdfBox-Android", "recursion is too deep, skipping form XObject");
                return;
            }
            PDFormXObject form = (PDFormXObject) xobject;
            if (form instanceof PDTransparencyGroup) {
                context.showTransparencyGroup((PDTransparencyGroup) form);
            } else {
                context.showForm(form);
            }
        } finally {
            context.decreaseLevel();
        }
    }
}
Also used : PDTransparencyGroup(com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSName(com.tom_roush.pdfbox.cos.COSName) MissingOperandException(com.tom_roush.pdfbox.contentstream.operator.MissingOperandException) PDFMarkedContentExtractor(com.tom_roush.pdfbox.text.PDFMarkedContentExtractor) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) PDXObject(com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)

Aggregations

PDXObject (com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)7 COSBase (com.tom_roush.pdfbox.cos.COSBase)5 COSName (com.tom_roush.pdfbox.cos.COSName)5 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)4 PDTransparencyGroup (com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)4 IOException (java.io.IOException)3 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)2 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)2 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 COSObject (com.tom_roush.pdfbox.cos.COSObject)1 COSStream (com.tom_roush.pdfbox.cos.COSStream)1 MissingResourceException (com.tom_roush.pdfbox.pdmodel.MissingResourceException)1 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)1 BlendMode (com.tom_roush.pdfbox.pdmodel.graphics.blend.BlendMode)1 PDImageXObject (com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject)1 PDExtendedGraphicsState (com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState)1 PDAnnotation (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)1 PDAnnotationUnknown (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationUnknown)1 PDFMarkedContentExtractor (com.tom_roush.pdfbox.text.PDFMarkedContentExtractor)1