Search in sources :

Example 6 with COSBase

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

the class BaseParser method parseCOSArray.

/**
 * This will parse a PDF array object.
 *
 * @return The parsed PDF array.
 *
 * @throws IOException If there is an error parsing the stream.
 */
protected COSArray parseCOSArray() throws IOException {
    long startPosition = seqSource.getPosition();
    readExpectedChar('[');
    COSArray po = new COSArray();
    COSBase pbo;
    skipSpaces();
    int i;
    while (((i = seqSource.peek()) > 0) && ((char) i != ']')) {
        pbo = parseDirObject();
        if (pbo instanceof COSObject) {
            // We have to check if the expected values are there or not PDFBOX-385
            if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger) {
                COSInteger genNumber = (COSInteger) po.remove(po.size() - 1);
                if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger) {
                    COSInteger number = (COSInteger) po.remove(po.size() - 1);
                    COSObjectKey key = new COSObjectKey(number.longValue(), genNumber.intValue());
                    pbo = getObjectFromPool(key);
                } else {
                    // the object reference is somehow wrong
                    pbo = null;
                }
            } else {
                pbo = null;
            }
        }
        if (pbo != null) {
            po.add(pbo);
        } else {
            // it could be a bad object in the array which is just skipped
            Log.w("PdfBox-Android", "Corrupt object reference at offset " + seqSource.getPosition() + ", start offset: " + startPosition);
            // This could also be an "endobj" or "endstream" which means we can assume that
            // the array has ended.
            String isThisTheEnd = readString();
            seqSource.unread(isThisTheEnd.getBytes(ISO_8859_1));
            if (ENDOBJ_STRING.equals(isThisTheEnd) || ENDSTREAM_STRING.equals(isThisTheEnd)) {
                return po;
            }
        }
        skipSpaces();
    }
    // read ']'
    seqSource.read();
    skipSpaces();
    return po;
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey) COSInteger(com.tom_roush.pdfbox.cos.COSInteger) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString)

Example 7 with COSBase

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

the class BaseParser method parseDirObject.

/**
 * This will parse a directory object from the stream.
 *
 * @return The parsed object.
 *
 * @throws IOException If there is an error during parsing.
 */
protected COSBase parseDirObject() throws IOException {
    COSBase retval = null;
    skipSpaces();
    int nextByte = seqSource.peek();
    char c = (char) nextByte;
    switch(c) {
        case '<':
            {
                // pull off first left bracket
                int leftBracket = seqSource.read();
                // check for second left bracket
                c = (char) seqSource.peek();
                seqSource.unread(leftBracket);
                if (c == '<') {
                    retval = parseCOSDictionary();
                    skipSpaces();
                } else {
                    retval = parseCOSString();
                }
                break;
            }
        case '[':
            {
                // array
                retval = parseCOSArray();
                break;
            }
        case '(':
            retval = parseCOSString();
            break;
        case '/':
            // name
            retval = parseCOSName();
            break;
        case 'n':
            {
                // null
                readExpectedString(NULL);
                retval = COSNull.NULL;
                break;
            }
        case 't':
            {
                String trueString = new String(seqSource.readFully(4), ISO_8859_1);
                if (trueString.equals(TRUE)) {
                    retval = COSBoolean.TRUE;
                } else {
                    throw new IOException("expected true actual='" + trueString + "' " + seqSource + "' at offset " + seqSource.getPosition());
                }
                break;
            }
        case 'f':
            {
                String falseString = new String(seqSource.readFully(5), ISO_8859_1);
                if (falseString.equals(FALSE)) {
                    retval = COSBoolean.FALSE;
                } else {
                    throw new IOException("expected false actual='" + falseString + "' " + seqSource + "' at offset " + seqSource.getPosition());
                }
                break;
            }
        case 'R':
            seqSource.read();
            retval = new COSObject(null);
            break;
        case (char) -1:
            return null;
        default:
            {
                if (Character.isDigit(c) || c == '-' || c == '+' || c == '.') {
                    StringBuilder buf = new StringBuilder();
                    int ic = seqSource.read();
                    c = (char) ic;
                    while (Character.isDigit(c) || c == '-' || c == '+' || c == '.' || c == 'E' || c == 'e') {
                        buf.append(c);
                        ic = seqSource.read();
                        c = (char) ic;
                    }
                    if (ic != -1) {
                        seqSource.unread(ic);
                    }
                    retval = COSNumber.get(buf.toString());
                } else {
                    // This is not suppose to happen, but we will allow for it
                    // so we are more compatible with POS writers that don't
                    // follow the spec
                    String badString = readString();
                    if (badString.isEmpty()) {
                        int peek = seqSource.peek();
                        // we can end up in an infinite loop otherwise
                        throw new IOException("Unknown dir object c='" + c + "' cInt=" + (int) c + " peek='" + (char) peek + "' peekInt=" + peek + " at offset " + seqSource.getPosition());
                    }
                    // if it's an endstream/endobj, we want to put it back so the caller will see it
                    if (ENDOBJ_STRING.equals(badString) || ENDSTREAM_STRING.equals(badString)) {
                        seqSource.unread(badString.getBytes(ISO_8859_1));
                    }
                }
            }
    }
    return retval;
}
Also used : COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) COSString(com.tom_roush.pdfbox.cos.COSString) IOException(java.io.IOException)

Example 8 with COSBase

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

the class Overlay method getLayoutPage.

private LayoutPage getLayoutPage(PDDocument doc) throws IOException {
    PDPage page = doc.getPage(0);
    COSBase contents = page.getCOSObject().getDictionaryObject(COSName.CONTENTS);
    PDResources resources = page.getResources();
    if (resources == null) {
        resources = new PDResources();
    }
    return new LayoutPage(page.getMediaBox(), createCombinedContentStream(contents), resources.getCOSObject(), page.getRotation());
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources)

Example 9 with COSBase

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

the class Overlay method processPages.

private void processPages(PDDocument document) throws IOException {
    int pageCounter = 0;
    for (PDPage page : document.getPages()) {
        pageCounter++;
        COSDictionary pageDictionary = page.getCOSObject();
        COSBase originalContent = pageDictionary.getDictionaryObject(COSName.CONTENTS);
        COSArray newContentArray = new COSArray();
        LayoutPage layoutPage = getLayoutPage(pageCounter, document.getNumberOfPages());
        if (layoutPage == null) {
            continue;
        }
        switch(position) {
            case FOREGROUND:
                // save state
                newContentArray.add(createStream("q\n"));
                addOriginalContent(originalContent, newContentArray);
                // restore state
                newContentArray.add(createStream("Q\n"));
                // overlay content last
                overlayPage(page, layoutPage, newContentArray);
                break;
            case BACKGROUND:
                // overlay content first
                overlayPage(page, layoutPage, newContentArray);
                addOriginalContent(originalContent, newContentArray);
                break;
            default:
                throw new IOException("Unknown type of position:" + position);
        }
        pageDictionary.setItem(COSName.CONTENTS, newContentArray);
    }
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Example 10 with COSBase

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

the class Overlay method getLayoutPages.

private Map<Integer, LayoutPage> getLayoutPages(PDDocument doc) throws IOException {
    int numberOfPages = doc.getNumberOfPages();
    Map<Integer, LayoutPage> layoutPages = new HashMap<Integer, Overlay.LayoutPage>(numberOfPages);
    for (int i = 0; i < numberOfPages; i++) {
        PDPage page = doc.getPage(i);
        COSBase contents = page.getCOSObject().getDictionaryObject(COSName.CONTENTS);
        PDResources resources = page.getResources();
        if (resources == null) {
            resources = new PDResources();
        }
        layoutPages.put(i, new LayoutPage(page.getMediaBox(), createCombinedContentStream(contents), resources.getCOSObject(), page.getRotation()));
    }
    return layoutPages;
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) HashMap(java.util.HashMap) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources)

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