Search in sources :

Example 96 with COSBase

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

the class PDPageContentStream method drawImage.

/**
 * Draw an inline image at the x,y coordinates and a certain width and height.
 *
 * @param inlineImage The inline image to draw.
 * @param x The x-coordinate to draw the inline image.
 * @param y The y-coordinate to draw the inline image.
 * @param width The width of the inline image to draw.
 * @param height The height of the inline image to draw.
 *
 * @throws IOException If there is an error writing to the stream.
 * @throws IllegalStateException If the method was called within a text block.
 */
public void drawImage(PDInlineImage inlineImage, float x, float y, float width, float height) throws IOException {
    if (inTextMode) {
        throw new IllegalStateException("Error: drawImage is not allowed within a text block.");
    }
    saveGraphicsState();
    transform(new Matrix(width, 0, 0, height, x, y));
    // create the image dictionary
    StringBuilder sb = new StringBuilder();
    sb.append(OperatorName.BEGIN_INLINE_IMAGE);
    sb.append("\n /W ");
    sb.append(inlineImage.getWidth());
    sb.append("\n /H ");
    sb.append(inlineImage.getHeight());
    sb.append("\n /CS ");
    sb.append("/");
    sb.append(inlineImage.getColorSpace().getName());
    if (inlineImage.getDecode() != null && inlineImage.getDecode().size() > 0) {
        sb.append("\n /D ");
        sb.append("[");
        for (COSBase base : inlineImage.getDecode()) {
            sb.append(((COSNumber) base).intValue());
            sb.append(" ");
        }
        sb.append("]");
    }
    if (inlineImage.isStencil()) {
        sb.append("\n /IM true");
    }
    sb.append("\n /BPC ");
    sb.append(inlineImage.getBitsPerComponent());
    // image dictionary
    write(sb.toString());
    writeLine();
    // binary data
    writeOperator(OperatorName.BEGIN_INLINE_IMAGE_DATA);
    writeBytes(inlineImage.getData());
    writeLine();
    writeOperator(OperatorName.END_INLINE_IMAGE);
    restoreGraphicsState();
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 97 with COSBase

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

the class PDPageTree method getKids.

/**
 * Helper to get kids from malformed PDFs.
 * @param node page tree node
 * @return list of kids
 */
private List<COSDictionary> getKids(COSDictionary node) {
    List<COSDictionary> result = new ArrayList<COSDictionary>();
    COSArray kids = node.getCOSArray(COSName.KIDS);
    if (kids == null) {
        // probably a malformed PDF
        return result;
    }
    for (int i = 0, size = kids.size(); i < size; i++) {
        COSBase base = kids.getObject(i);
        if (base instanceof COSDictionary) {
            result.add((COSDictionary) base);
        } else {
            Log.w("PdfBox-Android", "COSDictionary expected, but got " + (base == null ? "null" : base.getClass().getSimpleName()));
        }
    }
    return result;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray) ArrayList(java.util.ArrayList) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 98 with COSBase

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

the class PDResources method getColorSpace.

/**
 * Returns the color space resource with the given name, or null if none exists. This method is
 * for PDFBox internal use only, others should use {@link #getColorSpace(COSName)}.
 *
 * @param name Name of the color space resource.
 * @param wasDefault if current color space was used by a default color space. This parameter is
 * to
 * @return a new color space.
 * @throws IOException if something went wrong.
 */
public PDColorSpace getColorSpace(COSName name, boolean wasDefault) throws IOException {
    COSObject indirect = getIndirect(COSName.COLORSPACE, name);
    if (cache != null && indirect != null) {
        PDColorSpace cached = cache.getColorSpace(indirect);
        if (cached != null) {
            return cached;
        }
    }
    // get the instance
    PDColorSpace colorSpace;
    COSBase object = get(COSName.COLORSPACE, name);
    if (object != null) {
        colorSpace = PDColorSpace.create(object, this, wasDefault);
    } else {
        colorSpace = PDColorSpace.create(name, this, wasDefault);
    }
    // we can't cache PDPattern, because it holds page resources, see PDFBOX-2370
    if (// TODO: PdfBox-Android
    cache != null) /*&& !(colorSpace instanceof PDPattern)*/
    {
        cache.put(indirect, colorSpace);
    }
    return colorSpace;
}
Also used : COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 99 with COSBase

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

the class PDResources method getIndirect.

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

Example 100 with COSBase

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

the class PDFunction method create.

/**
 * 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);
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

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