Search in sources :

Example 51 with COSName

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

the class COSParser method checkPagesDictionary.

private int checkPagesDictionary(COSDictionary pagesDict, Set<COSObject> set) {
    // check for kids
    COSBase kids = pagesDict.getDictionaryObject(COSName.KIDS);
    int numberOfPages = 0;
    if (kids instanceof COSArray) {
        COSArray kidsArray = (COSArray) kids;
        List<? extends COSBase> kidsList = kidsArray.toList();
        for (COSBase kid : kidsList) {
            if (!(kid instanceof COSObject) || set.contains((COSObject) kid)) {
                kidsArray.remove(kid);
                continue;
            }
            COSObject kidObject = (COSObject) kid;
            COSBase kidBaseobject = kidObject.getObject();
            // object wasn't dereferenced -> remove it
            if (kidBaseobject == null || kidBaseobject.equals(COSNull.NULL)) {
                Log.w("PdfBox-Android", "Removed null object " + kid + " from pages dictionary");
                kidsArray.remove(kid);
            } else if (kidBaseobject instanceof COSDictionary) {
                COSDictionary kidDictionary = (COSDictionary) kidBaseobject;
                COSName type = kidDictionary.getCOSName(COSName.TYPE);
                if (COSName.PAGES.equals(type)) {
                    // process nested pages dictionaries
                    set.add(kidObject);
                    numberOfPages += checkPagesDictionary(kidDictionary, set);
                } else if (COSName.PAGE.equals(type)) {
                    // count pages
                    numberOfPages++;
                }
            }
        }
    }
    // fix counter
    pagesDict.setInt(COSName.COUNT, numberOfPages);
    return numberOfPages;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSName(com.tom_roush.pdfbox.cos.COSName) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 52 with COSName

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

the class FDFParser method initialParse.

/**
 * The initial parse will first parse only the trailer, the xrefstart and all xref tables to have a pointer (offset)
 * to all the pdf's objects. It can handle linearized pdfs, which will have an xref at the end pointing to an xref
 * at the beginning of the file. Last the root object is parsed.
 *
 * @throws IOException If something went wrong.
 */
private void initialParse() throws IOException {
    COSDictionary trailer = null;
    boolean rebuildTrailer = false;
    try {
        // parse startxref
        long startXRefOffset = getStartxrefOffset();
        if (startXRefOffset > 0) {
            trailer = parseXref(startXRefOffset);
        } else if (isLenient()) {
            rebuildTrailer = true;
        }
    } catch (IOException exception) {
        if (isLenient()) {
            rebuildTrailer = true;
        } else {
            throw exception;
        }
    }
    if (rebuildTrailer) {
        trailer = rebuildTrailer();
    }
    COSBase rootObject = parseTrailerValuesDynamically(trailer);
    // A FDF doesn't have a catalog, all FDF fields are within the root object
    if (rootObject instanceof COSDictionary) {
        parseDictObjects((COSDictionary) rootObject, (COSName[]) null);
    }
    initialParseDone = true;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSName(com.tom_roush.pdfbox.cos.COSName) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Example 53 with COSName

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

the class PDFParser method initialParse.

/**
 * The initial parse will first parse only the trailer, the xrefstart and all xref tables to have a pointer (offset)
 * to all the pdf's objects. It can handle linearized pdfs, which will have an xref at the end pointing to an xref
 * at the beginning of the file. Last the root object is parsed.
 *
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException If something went wrong.
 */
protected void initialParse() throws IOException {
    COSDictionary trailer = retrieveTrailer();
    COSBase base = parseTrailerValuesDynamically(trailer);
    if (!(base instanceof COSDictionary)) {
        throw new IOException("Expected root dictionary, but got this: " + base);
    }
    COSDictionary root = (COSDictionary) base;
    // in some pdfs the type value "Catalog" is missing in the root object
    if (isLenient() && !root.containsKey(COSName.TYPE)) {
        root.setItem(COSName.TYPE, COSName.CATALOG);
    }
    // parse all objects, starting at the root dictionary
    parseDictObjects(root, (COSName[]) null);
    // parse all objects of the info dictionary
    COSBase infoBase = trailer.getDictionaryObject(COSName.INFO);
    if (infoBase instanceof COSDictionary) {
        parseDictObjects((COSDictionary) infoBase, (COSName[]) null);
    }
    // check pages dictionaries
    checkPages(root);
    if (!(root.getDictionaryObject(COSName.PAGES) instanceof COSDictionary)) {
        throw new IOException("Page tree root must be a dictionary");
    }
    document.setDecrypted();
    initialParseDone = true;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSName(com.tom_roush.pdfbox.cos.COSName) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Example 54 with COSName

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

the class PDFXRefStream method getStream.

/**
 * Returns the stream of the XRef.
 * @return the XRef stream
 * @throws IOException if something went wrong
 */
public COSStream getStream() throws IOException {
    stream.setItem(COSName.TYPE, COSName.XREF);
    if (size == -1) {
        throw new IllegalArgumentException("size is not set in xrefstream");
    }
    stream.setLong(COSName.SIZE, size);
    List<Long> indexEntry = getIndexEntry();
    COSArray indexAsArray = new COSArray();
    for (Long i : indexEntry) {
        indexAsArray.add(COSInteger.get(i));
    }
    stream.setItem(COSName.INDEX, indexAsArray);
    int[] wEntry = getWEntry();
    COSArray wAsArray = new COSArray();
    for (int j : wEntry) {
        wAsArray.add(COSInteger.get(j));
    }
    stream.setItem(COSName.W, wAsArray);
    OutputStream outputStream = this.stream.createOutputStream(COSName.FLATE_DECODE);
    writeStreamData(outputStream, wEntry);
    outputStream.flush();
    outputStream.close();
    Set<COSName> keySet = this.stream.keySet();
    for (COSName cosName : keySet) {
        // some (such as Root in Table 15) shall be indirect."
        if (COSName.ROOT.equals(cosName) || COSName.INFO.equals(cosName) || COSName.PREV.equals(cosName)) {
            continue;
        }
        // this one too, because it has already been written in COSWriter.doWriteBody()
        if (COSName.ENCRYPT.equals(cosName)) {
            continue;
        }
        COSBase dictionaryObject = this.stream.getDictionaryObject(cosName);
        dictionaryObject.setDirect(true);
    }
    return this.stream;
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) COSName(com.tom_roush.pdfbox.cos.COSName) OutputStream(java.io.OutputStream) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 55 with COSName

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

the class PDInlineImage method createColorSpace.

private PDColorSpace createColorSpace(COSBase cs) throws IOException {
    if (cs instanceof COSName) {
        return PDColorSpace.create(toLongName(cs), resources);
    }
    if (cs instanceof COSArray && ((COSArray) cs).size() > 1) {
        COSArray srcArray = (COSArray) cs;
        COSBase csType = srcArray.get(0);
        if (COSName.I.equals(csType) || COSName.INDEXED.equals(csType)) {
            COSArray dstArray = new COSArray();
            dstArray.addAll(srcArray);
            dstArray.set(0, COSName.INDEXED);
            dstArray.set(1, toLongName(srcArray.get(1)));
            return PDColorSpace.create(dstArray, resources);
        }
        throw new IOException("Illegal type of inline image color space: " + csType);
    }
    throw new IOException("Illegal type of object for inline image color space: " + cs);
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSArray(com.tom_roush.pdfbox.cos.COSArray) COSBase(com.tom_roush.pdfbox.cos.COSBase) IOException(java.io.IOException)

Aggregations

COSName (com.tom_roush.pdfbox.cos.COSName)83 COSBase (com.tom_roush.pdfbox.cos.COSBase)50 COSArray (com.tom_roush.pdfbox.cos.COSArray)27 IOException (java.io.IOException)24 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)23 COSObject (com.tom_roush.pdfbox.cos.COSObject)11 COSString (com.tom_roush.pdfbox.cos.COSString)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)9 COSStream (com.tom_roush.pdfbox.cos.COSStream)7 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)7 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)5 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)5 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)5 PDXObject (com.tom_roush.pdfbox.pdmodel.graphics.PDXObject)5 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)4 PDTransparencyGroup (com.tom_roush.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)4 OutputStream (java.io.OutputStream)4 Operator (com.tom_roush.pdfbox.contentstream.operator.Operator)3