Example usage for org.apache.pdfbox.cos COSDictionary getItem

List of usage examples for org.apache.pdfbox.cos COSDictionary getItem

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSDictionary getItem.

Prototype

public COSBase getItem(String key) 

Source Link

Document

This will do a lookup into the dictionary.

Usage

From source file:org.apache.fop.render.pdf.pdfbox.MergeFontsPDFWriter.java

License:Apache License

private String getUniqueFontName(COSDictionary fontData) throws IOException {
    FontContainer fontContainer = getFont(fontData);
    PDFont font = fontContainer.font;//from w  w  w  .  j  a va2 s  . c om
    if (font.getName() != null) {
        String extra = "";
        String name = getName(font.getName()) + "_" + ((COSName) fontData.getItem(COSName.SUBTYPE)).getName();
        if (font instanceof PDType0Font && ((PDType0Font) font).getDescendantFont() instanceof PDCIDFontType0) {
            CFFFont cffFont = ((PDCIDFontType0) ((PDType0Font) font).getDescendantFont()).getCFFFont();
            if (cffFont instanceof CFFCIDFont && ((CFFCIDFont) cffFont).getFdSelect().getClass().getName()
                    .equals("org.apache.fontbox.cff.CFFParser$Format0FDSelect")) {
                extra += "format0";
            }
            return name + extra;
        } else if (font instanceof PDType0Font && fontContainer.getToUnicode() != null
                && ((PDType0Font) font).getDescendantFont() instanceof PDCIDFontType2) {
            if (!isSubsetFont(font.getName())) {
                extra = "f3";
            }
            return name + extra;
        } else if (font instanceof PDTrueTypeFont && isSubsetFont(font.getName())) {
            TrueTypeFont tt = ((PDTrueTypeFont) font).getTrueTypeFont();
            for (CmapSubtable c : tt.getCmap().getCmaps()) {
                if (c.getGlyphId(1) > 0) {
                    extra = "cid";
                }
            }
            return name + extra;
        } else if (font instanceof PDType1CFont) {
            return getNamePDType1Font(name, (PDType1CFont) font);
        } else if (font instanceof PDType1Font) {
            return name;
        }
    }
    return null;
}

From source file:org.apache.fop.render.pdf.pdfbox.PageParentTreeFinder.java

License:Apache License

private COSArray traverseParentTree(COSDictionary numberTreeNodeDict, int position) {
    COSArray numberTree;//from  w  w w  .j a v a  2s.  c  o m
    COSArray parentTree;
    List<COSArray> nums = new ArrayList<COSArray>();
    if (numberTreeNodeDict.containsKey(COSName.NUMS)) {
        numberTree = (COSArray) numberTreeNodeDict.getItem(COSName.NUMS);
        return extractMarkedContentParents(numberTree, position);
    } else {
        parentTree = (COSArray) numberTreeNodeDict.getDictionaryObject(COSName.KIDS);
        traverseKids(parentTree, position, nums);
    }
    return nums.get(0);
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

private void transferDict(COSDictionary orgDict, PDFStream targetDict, Set filter, boolean inclusive)
        throws IOException {
    Set<COSName> keys = orgDict.keySet();
    for (COSName key : keys) {
        if (inclusive && !filter.contains(key.getName())) {
            continue;
        } else if (!inclusive && filter.contains(key.getName())) {
            continue;
        }//  ww w.j  av a 2s. com
        targetDict.put(key.getName(), cloneForNewDocument(orgDict.getItem(key)));
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

private void mergeXObj(COSDictionary sourcePageResources, FontInfo fontinfo, UniqueName uniqueName)
        throws IOException {
    COSDictionary xobj = (COSDictionary) sourcePageResources.getDictionaryObject(COSName.XOBJECT);
    if (xobj != null && pdfDoc.isMergeFontsEnabled()) {
        for (Map.Entry<COSName, COSBase> i : xobj.entrySet()) {
            COSObject v = (COSObject) i.getValue();
            COSStream stream = (COSStream) v.getObject();
            COSDictionary res = (COSDictionary) stream.getDictionaryObject(COSName.RESOURCES);
            if (res != null) {
                COSDictionary src = (COSDictionary) res.getDictionaryObject(COSName.FONT);
                if (src != null) {
                    COSDictionary target = (COSDictionary) sourcePageResources
                            .getDictionaryObject(COSName.FONT);
                    if (target == null) {
                        sourcePageResources.setItem(COSName.FONT, src);
                    } else {
                        for (Map.Entry<COSName, COSBase> entry : src.entrySet()) {
                            if (!target.keySet().contains(entry.getKey())) {
                                target.setItem(uniqueName.getName(entry.getKey()), entry.getValue());
                            }//from  w  ww. j  a  v a  2s  . c  o  m
                        }
                    }
                    PDFWriter writer = new MergeFontsPDFWriter(src, fontinfo, uniqueName, parentFonts, 0);
                    String c = writer.writeText(new PDStream(stream));
                    if (c != null) {
                        stream.removeItem(COSName.FILTER);
                        newXObj.put(i.getKey(), c);
                        for (Object e : src.keySet().toArray()) {
                            COSName name = (COSName) e;
                            src.setItem(uniqueName.getName(name), src.getItem(name));
                            src.removeItem(name);
                        }
                    }
                }
            }
        }
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxAdapter.java

License:Apache License

private Set<COSObject> copyAnnotations(PDPage page) throws IOException {
    COSArray annots = (COSArray) page.getCOSObject().getDictionaryObject(COSName.ANNOTS);
    Set<COSObject> fields = Collections.emptySet();
    if (annots != null) {
        fields = new TreeSet<COSObject>(new CompareFields());
        for (Object annot1 : annots) {
            Collection<COSName> exclude = new ArrayList<COSName>();
            exclude.add(COSName.P);//from ww  w  . j a v  a 2  s.  c  om
            if (annot1 instanceof COSObject) {
                COSObject annot = (COSObject) annot1;
                COSObject fieldObject = annot;
                COSDictionary field = (COSDictionary) fieldObject.getObject();
                COSObject parent;
                while ((parent = (COSObject) field.getItem(COSName.PARENT)) != null) {
                    fieldObject = parent;
                    field = (COSDictionary) fieldObject.getObject();
                }
                fields.add(fieldObject);

                if (((COSDictionary) annot.getObject()).getItem(COSName.getPDFName("StructParent")) != null) {
                    exclude.add(COSName.PARENT);
                }
            }

            PDFObject clonedAnnot = (PDFObject) cloneForNewDocument(annot1, annot1, exclude);
            if (clonedAnnot instanceof PDFDictionary) {
                clonedAnnot.setParent(targetPage);
                updateAnnotationLink((PDFDictionary) clonedAnnot);
            }
            targetPage.addAnnotation(clonedAnnot);
        }
    }
    return fields;
}

From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java

License:Apache License

public void createDirectDescendants(COSBase base, PDFStructElem parent) throws IOException {
    if (base instanceof COSDictionary) {
        COSDictionary baseDict = (COSDictionary) base;
        if (baseDict.keySet().contains(COSName.K)) {
            createDirectDescendants(baseDict.getItem(COSName.K), parent);
        }//from w  ww .ja  v a  2s  . c o m
    } else if (base instanceof COSArray) {
        COSArray array = (COSArray) base;
        for (int i = 0; i < array.size(); i++) {
            createDirectDescendants(array.get(i), parent);
        }
    } else {
        assert base instanceof COSObject;
        COSObject obj = (COSObject) base;
        createAndRegisterStructElem(obj);
        PDFStructElem elem = structElemCache.get((int) obj.getObjectNumber());
        copyElemEntries(obj, elem);
        parent.addKid(elem);
        elem.setParent(parent);
        COSBase objKid = obj.getItem(COSName.K);
        if (objKid != null) {
            createDirectDescendants(objKid, elem);
        }
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java

License:Apache License

private void copyElemEntries(COSBase base, PDFStructElem elem) throws IOException {
    assert base instanceof COSObject;
    COSObject baseObj = (COSObject) base;
    COSDictionary baseDic = (COSDictionary) baseObj.getObject();
    COSName[] names = { COSName.TYPE, COSName.S, COSName.PG, COSName.ALT, COSName.LANG, COSName.A,
            COSName.ACTUAL_TEXT, COSName.T, COSName.E, COSName.C };
    for (COSName name : names) {
        if (baseDic.keySet().contains(name)) {
            if (name.equals(COSName.PG)) {
                elem.put(COSName.PG.getName(), targetPage.makeReference());
            } else {
                elem.put(name.getName(), adapter.cloneForNewDocument(baseDic.getItem(name)));
            }/*from   w  ww  . java  2s  .  co  m*/
        }
    }
    adapter.cacheClonedObject(base, elem);
}

From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java

License:Apache License

private PDFStructElem createPageStructElements(COSObject entry) throws IOException {
    int objID = (int) entry.getObjectNumber();
    if (structElemCache.containsKey(objID)) {
        return null;
    }/*from ww  w . j  ava  2s.com*/
    PDFStructElem elem = createAndRegisterStructElem(entry);
    copyElemEntries(entry, elem);
    COSDictionary baseDict = (COSDictionary) entry.getObject();
    COSBase kid = baseDict.getItem(COSName.K);
    createKids(kid, baseDict, elem, false);
    return elem;
}

From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java

License:Apache License

private void createParents(COSObject cosElem, COSObject cosParentElem, PDFStructElem elem) throws IOException {
    int elemObjectID = (int) cosParentElem.getObjectNumber();
    COSDictionary parentElemDictionary = (COSDictionary) cosParentElem.getObject();
    PDFStructElem elemParent = structElemCache.get(elemObjectID);
    if (isStructureTreeRoot(parentElemDictionary)) {
        elem.setParent(currentSessionElem);
        currentSessionElem.addKid(elem);
        topElems.add(cosElem);//from ww  w  . java  2 s . c o  m
    } else if (elemParent != null) {
        if (!checkIfStructureTypeIsPresent(parentElemDictionary, StandardStructureTypes.TR)) {
            elem.setParent(elemParent);
            int position = StructureTreeMergerUtil.findObjectPositionInKidsArray(cosElem);
            elemParent.addKidInSpecificOrder(position, elem);
        }
    } else if (!checkIfStructureTypeIsPresent(parentElemDictionary, StandardStructureTypes.DOCUMENT)) {
        elemParent = createAndRegisterStructElem(cosParentElem);
        copyElemEntries(cosParentElem, elemParent);
        elem.setParent(elemParent);
        fillKidsWithNull(elemParent, (COSDictionary) cosParentElem.getObject());
        if (((COSName) parentElemDictionary.getDictionaryObject(COSName.S)).getName()
                .equals(StandardStructureTypes.TR)) {
            COSBase rowKids = parentElemDictionary.getItem(COSName.K);
            createKids(rowKids, parentElemDictionary, elemParent, true);
        } else {
            int position = StructureTreeMergerUtil.findObjectPositionInKidsArray(cosElem);
            elemParent.addKidInSpecificOrder(position, elem);
        }
        COSObject parentObj = (COSObject) parentElemDictionary.getItem(COSName.P);
        createParents(cosParentElem, parentObj, elemParent);
    } else {
        elem.setParent(currentSessionElem);
        int position = StructureTreeMergerUtil.findObjectPositionInKidsArray(cosElem);
        currentSessionElem.addKidInSpecificOrder(position, elem);
        topElems.add(cosElem);
    }
}

From source file:org.apache.fop.render.pdf.pdfbox.StructureTreeMerger.java

License:Apache License

private void createkidEntryFromCosObjectForRow(COSObject entree, PDFStructElem parent) throws IOException {
    int entreeObjID = (int) entree.getObjectNumber();
    PDFStructElem elemRef = structElemCache.get(entreeObjID);
    if (elemRef == null) {
        elemRef = createAndRegisterStructElem(entree);
        copyElemEntries(entree, elemRef);
        COSDictionary baseDict = (COSDictionary) entree.getObject();
        COSBase kid = baseDict.getItem(COSName.K);
        createKids(kid, baseDict, elemRef, true);
        parent.addKid(elemRef);/*from w w  w .j a v a2 s.com*/
    } else {
        parent.addKid(elemRef);
    }
    elemRef.setParent(parent);
}