Search in sources :

Example 51 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase 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 52 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.

the class COSArrayList method convertFloatCOSArrayToList.

/**
 * This will take an array of COSNumbers and return a COSArrayList of
 * java.lang.Float values.
 *
 * @param floatArray The existing float Array.
 *
 * @return The list of Float objects.
 */
public static List<Float> convertFloatCOSArrayToList(COSArray floatArray) {
    List<Float> retval = null;
    if (floatArray != null) {
        List<Float> numbers = new ArrayList<Float>(floatArray.size());
        for (int i = 0; i < floatArray.size(); i++) {
            COSBase base = floatArray.getObject(i);
            if (base instanceof COSNumber) {
                numbers.add(((COSNumber) base).floatValue());
            } else {
                numbers.add(null);
            }
        }
        retval = new COSArrayList<Float>(numbers, floatArray);
    }
    return retval;
}
Also used : COSFloat(com.tom_roush.pdfbox.cos.COSFloat) ArrayList(java.util.ArrayList) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 53 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.

the class COSArrayList method toCOSObjectList.

private List<COSBase> toCOSObjectList(Collection<?> list) {
    List<COSBase> cosObjects = new ArrayList<COSBase>();
    for (Object next : list) {
        if (next instanceof String) {
            cosObjects.add(new COSString((String) next));
        } else {
            COSObjectable cos = (COSObjectable) next;
            cosObjects.add(cos.getCOSObject());
        }
    }
    return cosObjects;
}
Also used : ArrayList(java.util.ArrayList) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSString(com.tom_roush.pdfbox.cos.COSString) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 54 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.

the class PDType3CharProc method getWidth.

/**
 * Get the width from a type3 charproc stream.
 *
 * @return the glyph width.
 * @throws IOException if the stream could not be read, or did not have d0 or d1 as first
 * operator, or if their first argument was not a number.
 */
public float getWidth() throws IOException {
    List<COSBase> arguments = new ArrayList<COSBase>();
    PDFStreamParser parser = new PDFStreamParser(this);
    Object token = parser.parseNextToken();
    while (token != null) {
        if (token instanceof COSObject) {
            arguments.add(((COSObject) token).getObject());
        } else if (token instanceof Operator) {
            return parseWidth((Operator) token, arguments);
        } else {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
    throw new IOException("Unexpected end of stream");
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser) COSObject(com.tom_roush.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSObject(com.tom_roush.pdfbox.cos.COSObject) IOException(java.io.IOException)

Example 55 with COSBase

use of com.tom_roush.pdfbox.cos.COSBase in project PdfBox-Android by TomRoush.

the class PDType3CharProc method getGlyphBBox.

/**
 * Calculate the bounding box of this glyph. This will work only if the first operator in the
 * stream is d1.
 *
 * @return the bounding box of this glyph, or null if the first operator is not d1.
 * @throws IOException If an io error occurs while parsing the stream.
 */
public PDRectangle getGlyphBBox() throws IOException {
    List<COSBase> arguments = new ArrayList<COSBase>();
    PDFStreamParser parser = new PDFStreamParser(this);
    Object token = parser.parseNextToken();
    while (token != null) {
        if (token instanceof COSObject) {
            arguments.add(((COSObject) token).getObject());
        } else if (token instanceof Operator) {
            if (((Operator) token).getName().equals("d1") && arguments.size() == 6) {
                for (int i = 0; i < 6; ++i) {
                    if (!(arguments.get(i) instanceof COSNumber)) {
                        return null;
                    }
                }
                return new PDRectangle(((COSNumber) arguments.get(2)).floatValue(), ((COSNumber) arguments.get(3)).floatValue(), ((COSNumber) arguments.get(4)).floatValue() - ((COSNumber) arguments.get(2)).floatValue(), ((COSNumber) arguments.get(5)).floatValue() - ((COSNumber) arguments.get(3)).floatValue());
            } else {
                return null;
            }
        } else {
            arguments.add((COSBase) token);
        }
        token = parser.parseNextToken();
    }
    return null;
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser) COSObject(com.tom_roush.pdfbox.cos.COSObject) ArrayList(java.util.ArrayList) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSObject(com.tom_roush.pdfbox.cos.COSObject) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Aggregations

COSBase (com.tom_roush.pdfbox.cos.COSBase)215 COSArray (com.tom_roush.pdfbox.cos.COSArray)108 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)68 COSName (com.tom_roush.pdfbox.cos.COSName)50 COSObject (com.tom_roush.pdfbox.cos.COSObject)42 IOException (java.io.IOException)34 COSString (com.tom_roush.pdfbox.cos.COSString)33 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)29 ArrayList (java.util.ArrayList)29 COSStream (com.tom_roush.pdfbox.cos.COSStream)20 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)14 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)11 HashMap (java.util.HashMap)11 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)10 Map (java.util.Map)10 List (java.util.List)9 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)8 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)7 COSObjectKey (com.tom_roush.pdfbox.cos.COSObjectKey)7 PDFStreamParser (com.tom_roush.pdfbox.pdfparser.PDFStreamParser)6