Search in sources :

Example 1 with COSNumber

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

the class COSWriter method prepareIncrement.

private void prepareIncrement(PDDocument doc) {
    try {
        if (doc != null) {
            COSDocument cosDoc = doc.getDocument();
            Map<COSObjectKey, Long> xrefTable = cosDoc.getXrefTable();
            Set<COSObjectKey> keySet = xrefTable.keySet();
            long highestNumber = doc.getDocument().getHighestXRefObjectNumber();
            for (COSObjectKey cosObjectKey : keySet) {
                COSBase object = cosDoc.getObjectFromPool(cosObjectKey).getObject();
                if (object != null && cosObjectKey != null && !(object instanceof COSNumber)) {
                    objectKeys.put(object, cosObjectKey);
                    keyObject.put(cosObjectKey, object);
                }
                if (cosObjectKey != null) {
                    long num = cosObjectKey.getNumber();
                    if (num > highestNumber) {
                        highestNumber = num;
                    }
                }
            }
            setNumber(highestNumber);
        }
    } catch (IOException e) {
        Log.e("PdfBox-Android", e.getMessage(), e);
    }
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSDocument(com.tom_roush.pdfbox.cos.COSDocument) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Example 2 with COSNumber

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

the class COSParser method parseCOSStream.

/**
 * This will read a COSStream from the input stream using length attribute within dictionary. If
 * length attribute is a indirect reference it is first resolved to get the stream length. This
 * means we copy stream data without testing for 'endstream' or 'endobj' and thus it is no
 * problem if these keywords occur within stream. We require 'endstream' to be found after
 * stream data is read.
 *
 * @param dic dictionary that goes with this stream.
 *
 * @return parsed pdf stream.
 *
 * @throws IOException if an error occurred reading the stream, like problems with reading
 * length attribute, stream does not end with 'endstream' after data read, stream too short etc.
 */
protected COSStream parseCOSStream(COSDictionary dic) throws IOException {
    COSStream stream = document.createCOSStream(dic);
    // read 'stream'; this was already tested in parseObjectsDynamically()
    readString();
    skipWhiteSpaces();
    /*
         * This needs to be dic.getItem because when we are parsing, the underlying object might still be null.
         */
    COSNumber streamLengthObj = getLength(dic.getItem(COSName.LENGTH), dic.getCOSName(COSName.TYPE));
    if (streamLengthObj == null) {
        if (isLenient) {
            Log.w("PdfBox-Android", "The stream doesn't provide any stream length, using fallback readUntilEnd, at offset " + source.getPosition());
        } else {
            throw new IOException("Missing length for stream.");
        }
    }
    // get output stream to copy data to
    if (streamLengthObj != null && validateStreamLength(streamLengthObj.longValue())) {
        OutputStream out = stream.createRawOutputStream();
        try {
            readValidStream(out, streamLengthObj);
        } finally {
            out.close();
            // restore original (possibly incorrect) length
            stream.setItem(COSName.LENGTH, streamLengthObj);
        }
    } else {
        OutputStream out = stream.createRawOutputStream();
        try {
            readUntilEndStream(new EndstreamOutputStream(out));
        } finally {
            out.close();
            // restore original (possibly incorrect) length
            if (streamLengthObj != null) {
                stream.setItem(COSName.LENGTH, streamLengthObj);
            }
        }
    }
    String endStream = readString();
    if (endStream.equals("endobj") && isLenient) {
        Log.w("PdfBox-Android", "stream ends with 'endobj' instead of 'endstream' at offset " + source.getPosition());
        // avoid follow-up warning about missing endobj
        source.rewind(ENDOBJ.length);
    } else if (endStream.length() > 9 && isLenient && endStream.substring(0, 9).equals(ENDSTREAM_STRING)) {
        Log.w("PdfBox-Android", "stream ends with '" + endStream + "' instead of 'endstream' at offset " + source.getPosition());
        // unread the "extra" bytes
        source.rewind(endStream.substring(9).getBytes(ISO_8859_1).length);
    } else if (!endStream.equals(ENDSTREAM_STRING)) {
        throw new IOException("Error reading stream, expected='endstream' actual='" + endStream + "' at offset " + source.getPosition());
    }
    return stream;
}
Also used : COSStream(com.tom_roush.pdfbox.cos.COSStream) OutputStream(java.io.OutputStream) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) IOException(java.io.IOException)

Example 3 with COSNumber

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

the class PDPolygonAppearanceHandler method getLineWidth.

/**
 * Get the line with of the border.
 *
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 *
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    PDBorderStyleDictionary bs = annotation.getBorderStyle();
    if (bs != null) {
        return bs.getWidth();
    }
    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3) {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber) {
            return ((COSNumber) base).floatValue();
        }
    }
    return 1;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Example 4 with COSNumber

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

the class PDPolylineAppearanceHandler method getLineWidth.

// TODO DRY, this code is from polygonAppearanceHandler so it's double
/**
 * Get the line with of the border.
 *
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 *
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    PDBorderStyleDictionary bs = annotation.getBorderStyle();
    if (bs != null) {
        return bs.getWidth();
    }
    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3) {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber) {
            return ((COSNumber) base).floatValue();
        }
    }
    return 1;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Example 5 with COSNumber

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

the class PDSquareAppearanceHandler method getLineWidth.

/**
 * Get the line with of the border.
 *
 * Get the width of the line used to draw a border around the annotation.
 * This may either be specified by the annotation dictionaries Border
 * setting or by the W entry in the BS border style dictionary. If both are
 * missing the default width is 1.
 *
 * @return the line width
 */
// TODO: according to the PDF spec the use of the BS entry is annotation
// specific
// so we will leave that to be implemented by individual handlers.
// If at the end all annotations support the BS entry this can be handled
// here and removed from the individual handlers.
float getLineWidth() {
    PDAnnotationMarkup annotation = (PDAnnotationMarkup) getAnnotation();
    PDBorderStyleDictionary bs = annotation.getBorderStyle();
    if (bs != null) {
        return bs.getWidth();
    }
    COSArray borderCharacteristics = annotation.getBorder();
    if (borderCharacteristics.size() >= 3) {
        COSBase base = borderCharacteristics.getObject(2);
        if (base instanceof COSNumber) {
            return ((COSNumber) base).floatValue();
        }
    }
    return 1;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) PDAnnotationMarkup(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDBorderStyleDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)

Aggregations

COSNumber (com.tom_roush.pdfbox.cos.COSNumber)55 COSBase (com.tom_roush.pdfbox.cos.COSBase)29 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)18 COSArray (com.tom_roush.pdfbox.cos.COSArray)17 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)10 PointF (android.graphics.PointF)6 IOException (java.io.IOException)6 COSName (com.tom_roush.pdfbox.cos.COSName)5 PDBorderStyleDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDBorderStyleDictionary)5 COSObject (com.tom_roush.pdfbox.cos.COSObject)4 PDAnnotationMarkup (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup)4 COSFloat (com.tom_roush.pdfbox.cos.COSFloat)3 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)3 ArrayList (java.util.ArrayList)3 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)2 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)2 COSObjectKey (com.tom_roush.pdfbox.cos.COSObjectKey)2 COSStream (com.tom_roush.pdfbox.cos.COSStream)2 PDFStreamParser (com.tom_roush.pdfbox.pdfparser.PDFStreamParser)2 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)2