Search in sources :

Example 21 with COSBase

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

the class COSParser method retrieveCOSDictionary.

private COSDictionary retrieveCOSDictionary(COSObjectKey key, long offset) throws IOException {
    COSDictionary dictionary = null;
    // handle compressed objects
    if (offset < 0) {
        COSObject compressedObject = document.getObjectFromPool(key);
        if (compressedObject.getObject() == null) {
            parseObjectStream((int) -offset);
        }
        COSBase baseObject = compressedObject.getObject();
        if (baseObject instanceof COSDictionary) {
            dictionary = (COSDictionary) baseObject;
        }
    } else {
        source.seek(offset);
        readObjectNumber();
        readGenerationNumber();
        readExpectedString(OBJ_MARKER, true);
        if (source.peek() != '<') {
            return null;
        }
        try {
            dictionary = parseCOSDictionary();
        } catch (IOException exception) {
            Log.d("PdfBox-Android", "Skipped object " + key + ", either it's corrupt or not a dictionary");
        }
    }
    return dictionary;
}
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)

Example 22 with COSBase

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

the class PDAnnotationLine method setEndPointEndingStyle.

/**
 * This will set the line ending style for the end point, see the LE_ constants for the possible values.
 *
 * @param style The new style.
 */
public void setEndPointEndingStyle(String style) {
    if (style == null) {
        style = LE_NONE;
    }
    COSBase base = getCOSObject().getDictionaryObject(COSName.LE);
    COSArray array;
    if (!(base instanceof COSArray) || ((COSArray) base).size() < 2) {
        array = new COSArray();
        array.add(COSName.getPDFName(LE_NONE));
        array.add(COSName.getPDFName(style));
        getCOSObject().setItem(COSName.LE, array);
    } else {
        array = (COSArray) base;
        array.setName(1, style);
    }
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 23 with COSBase

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

the class PDAnnotationMarkup method getInkList.

/**
 * Get one or more disjoint paths that make this annotation.
 *
 * @return An array of arrays, each representing a stroked path. Each array shall be a series of
 * alternating horizontal and vertical coordinates.
 */
public float[][] getInkList() {
    COSBase base = getCOSObject().getDictionaryObject(COSName.INKLIST);
    if (base instanceof COSArray) {
        COSArray array = (COSArray) base;
        float[][] inkList = new float[array.size()][];
        for (int i = 0; i < array.size(); ++i) {
            COSBase base2 = array.getObject(i);
            if (base2 instanceof COSArray) {
                inkList[i] = ((COSArray) array.getObject(i)).toFloatArray();
            } else {
                inkList[i] = new float[0];
            }
        }
        return inkList;
    }
    return new float[0][0];
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 24 with COSBase

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

the class PDAnnotationMarkup method setStartPointEndingStyle.

/**
 * This will set the line ending style for the start point, see the LE_ constants for the possible values.
 *
 * @param style The new style.
 */
public void setStartPointEndingStyle(String style) {
    String actualStyle = style == null ? PDAnnotationLine.LE_NONE : style;
    COSBase base = getCOSObject().getDictionaryObject(COSName.LE);
    COSArray array;
    if (!(base instanceof COSArray) || ((COSArray) base).size() == 0) {
        array = new COSArray();
        array.add(COSName.getPDFName(actualStyle));
        array.add(COSName.getPDFName(PDAnnotationLine.LE_NONE));
        getCOSObject().setItem(COSName.LE, array);
    } else {
        array = (COSArray) base;
        array.setName(0, actualStyle);
    }
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 25 with COSBase

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

the class PDFreeTextAppearanceHandler method extractNonStrokingColor.

// get the last non stroking color from the /DA entry
private PDColor extractNonStrokingColor(PDAnnotationMarkup annotation) {
    // It could also work with a regular expression, but that should be written so that
    // "/LucidaConsole 13.94766 Tf .392 .585 .93 rg" does not produce "2 .585 .93 rg" as result
    // Another alternative might be to create a PDDocument and a PDPage with /DA content as /Content,
    // process the whole thing and then get the non stroking color.
    PDColor strokingColor = new PDColor(new float[] { 0 }, PDDeviceGray.INSTANCE);
    String defaultAppearance = annotation.getDefaultAppearance();
    if (defaultAppearance == null) {
        return strokingColor;
    }
    try {
        // not sure if charset is correct, but we only need numbers and simple characters
        PDFStreamParser parser = new PDFStreamParser(defaultAppearance.getBytes(Charsets.US_ASCII));
        COSArray arguments = new COSArray();
        COSArray colors = null;
        Operator graphicOp = null;
        for (Object token = parser.parseNextToken(); token != null; token = parser.parseNextToken()) {
            if (token instanceof COSObject) {
                arguments.add(((COSObject) token).getObject());
            } else if (token instanceof Operator) {
                Operator op = (Operator) token;
                String name = op.getName();
                if (OperatorName.NON_STROKING_GRAY.equals(name) || OperatorName.NON_STROKING_RGB.equals(name) || OperatorName.NON_STROKING_CMYK.equals(name)) {
                    graphicOp = op;
                    colors = arguments;
                }
                arguments = new COSArray();
            } else {
                arguments.add((COSBase) token);
            }
        }
        if (graphicOp != null) {
            String graphicOpName = graphicOp.getName();
            if (OperatorName.NON_STROKING_GRAY.equals(graphicOpName)) {
                strokingColor = new PDColor(colors, PDDeviceGray.INSTANCE);
            } else if (OperatorName.NON_STROKING_RGB.equals(graphicOpName)) {
                strokingColor = new PDColor(colors, PDDeviceRGB.INSTANCE);
            } else if (OperatorName.NON_STROKING_CMYK.equals(graphicOpName)) {
            // strokingColor = new PDColor(colors, PDDeviceCMYK.INSTANCE); TODO: PdfBox-Android
            }
        }
    } catch (IOException ex) {
        Log.w("PdfBox-Android", "Problem parsing /DA, will use default black", ex);
    }
    return strokingColor;
}
Also used : Operator(com.tom_roush.pdfbox.contentstream.operator.Operator) PDFStreamParser(com.tom_roush.pdfbox.pdfparser.PDFStreamParser) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSObject(com.tom_roush.pdfbox.cos.COSObject) IOException(java.io.IOException) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

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