Search in sources :

Example 66 with COSDictionary

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

the class PDPageTree method add.

/**
 * Adds the given page to this page tree.
 *
 * @param page The page to add.
 */
public void add(PDPage page) {
    // set parent
    COSDictionary node = page.getCOSObject();
    node.setItem(COSName.PARENT, root);
    // todo: re-balance tree? (or at least group new pages into tree nodes of e.g. 20)
    // add to parent's kids
    COSArray kids = (COSArray) root.getDictionaryObject(COSName.KIDS);
    kids.add(node);
    // update ancestor counts
    do {
        node = (COSDictionary) node.getDictionaryObject(COSName.PARENT, COSName.P);
        if (node != null) {
            node.setInt(COSName.COUNT, node.getInt(COSName.COUNT) + 1);
        }
    } while (node != null);
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSArray(com.tom_roush.pdfbox.cos.COSArray)

Example 67 with COSDictionary

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

the class PDResources method add.

/**
 * Adds the given resource if it does not already exist.
 */
private COSName add(COSName kind, String prefix, COSObjectable object) {
    // return the existing key if the item exists already
    COSDictionary dict = (COSDictionary) resources.getDictionaryObject(kind);
    if (dict != null && dict.containsValue(object.getCOSObject())) {
        return dict.getKeyForValue(object.getCOSObject());
    }
    // AcroForm default resources of a loaded PDF.
    if (dict != null && COSName.FONT.equals(kind)) {
        for (Map.Entry<COSName, COSBase> entry : dict.entrySet()) {
            if (entry.getValue() instanceof COSObject && object.getCOSObject() == ((COSObject) entry.getValue()).getObject()) {
                return entry.getKey();
            }
        }
    }
    // add the item with a new key
    COSName name = createKey(kind, prefix);
    put(kind, name, object);
    return name;
}
Also used : 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) HashMap(java.util.HashMap) Map(java.util.Map)

Example 68 with COSDictionary

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

the class PDResources method getShading.

/**
 * Returns the shading resource with the given name, or null if none exists.
 *
 * @param name Name of the shading resource.
 *
 * @return the shading resource of the given name.
 *
 * @throws IOException if something went wrong.
 */
public PDShading getShading(COSName name) throws IOException {
    COSObject indirect = getIndirect(COSName.SHADING, name);
    if (cache != null && indirect != null) {
        PDShading cached = cache.getShading(indirect);
        if (cached != null) {
            return cached;
        }
    }
    // get the instance
    PDShading shading = null;
    COSDictionary dict = (COSDictionary) get(COSName.SHADING, name);
    if (dict != null) {
        shading = PDShading.create(dict);
    }
    if (cache != null) {
        cache.put(indirect, shading);
    }
    return shading;
}
Also used : PDShading(com.tom_roush.pdfbox.pdmodel.graphics.shading.PDShading) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSObject(com.tom_roush.pdfbox.cos.COSObject)

Example 69 with COSDictionary

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

the class PDResources method getPattern.

/**
 * Returns the pattern resource with the given name, or null if none exists.
 *
 * @param name Name of the pattern resource.
 *
 * @return the pattern resource of the given name.
 *
 * @throws IOException if something went wrong.
 */
public PDAbstractPattern getPattern(COSName name) throws IOException {
    COSObject indirect = getIndirect(COSName.PATTERN, name);
    if (cache != null && indirect != null) {
        PDAbstractPattern cached = cache.getPattern(indirect);
        if (cached != null) {
            return cached;
        }
    }
    // get the instance
    PDAbstractPattern pattern = null;
    COSDictionary dict = (COSDictionary) get(COSName.PATTERN, name);
    if (dict != null) {
        pattern = PDAbstractPattern.create(dict);
    }
    if (cache != null) {
        cache.put(indirect, pattern);
    }
    return pattern;
}
Also used : PDAbstractPattern(com.tom_roush.pdfbox.pdmodel.graphics.pattern.PDAbstractPattern) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) COSObject(com.tom_roush.pdfbox.cos.COSObject)

Example 70 with COSDictionary

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

the class PDResources method getExtGState.

/**
 * Returns the extended graphics state resource with the given name, or null if none exists.
 *
 * @param name Name of the graphics state resource.
 *
 * @return the extended graphics state resource with the given name.
 */
public PDExtendedGraphicsState getExtGState(COSName name) {
    COSObject indirect = getIndirect(COSName.EXT_G_STATE, name);
    if (cache != null && indirect != null) {
        PDExtendedGraphicsState cached = cache.getExtGState(indirect);
        if (cached != null) {
            return cached;
        }
    }
    // get the instance
    PDExtendedGraphicsState extGState = null;
    COSDictionary dict = (COSDictionary) get(COSName.EXT_G_STATE, name);
    if (dict != null) {
        extGState = new PDExtendedGraphicsState(dict);
    }
    if (cache != null) {
        cache.put(indirect, extGState);
    }
    return extGState;
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDExtendedGraphicsState(com.tom_roush.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState) COSObject(com.tom_roush.pdfbox.cos.COSObject)

Aggregations

COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)221 COSArray (com.tom_roush.pdfbox.cos.COSArray)68 COSBase (com.tom_roush.pdfbox.cos.COSBase)68 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)24 COSName (com.tom_roush.pdfbox.cos.COSName)23 COSObject (com.tom_roush.pdfbox.cos.COSObject)22 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)15 HashMap (java.util.HashMap)14 COSStream (com.tom_roush.pdfbox.cos.COSStream)13 Map (java.util.Map)12 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)10 COSString (com.tom_roush.pdfbox.cos.COSString)7 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)6 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 List (java.util.List)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 Test (org.junit.Test)5