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

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

Introduction

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

Prototype

public COSBase getDictionaryObject(COSName key) 

Source Link

Document

This will get an object from this dictionary.

Usage

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

License:Apache License

private void checkForMap(COSDictionary structRootDict, String mapName) throws IOException {
    if (structRootDict.containsKey(mapName)) {
        COSDictionary addedMapDict = (COSDictionary) structRootDict.getDictionaryObject(mapName);
        PDFDictionary temp = (PDFDictionary) adapter.cloneForNewDocument(addedMapDict);
        PDFStructTreeRoot structTreeRoot = pdfDoc.getRoot().getStructTreeRoot();
        if (!structTreeRoot.containsKey(mapName)) {
            structTreeRoot.put(mapName, temp);
        } else {//from w  ww .  ja v  a  2s  . com
            PDFDictionary rootMap = (PDFDictionary) structTreeRoot.get(mapName);
            addMapToStructTreeRoot(rootMap, temp, mapName);
        }
    }
}

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

License:Apache License

private Map<Integer, String> getCodeToName(Encoding encoding) {
    Map<Integer, String> codeToName = new HashMap<Integer, String>();
    if (encoding != null) {
        COSBase cos = null;//from w ww.j  av a2 s.  c o m
        if (!(encoding instanceof BuiltInEncoding)) {
            cos = encoding.getCOSObject();
        }
        if (cos instanceof COSDictionary) {
            COSDictionary enc = (COSDictionary) cos;
            COSName baseEncodingName = (COSName) enc.getDictionaryObject(COSName.BASE_ENCODING);
            if (baseEncodingName != null) {
                Encoding baseEncoding = Encoding.getInstance(baseEncodingName);
                codeToName.putAll(baseEncoding.getCodeToNameMap());
            }
            COSArray differences = (COSArray) enc.getDictionaryObject(COSName.DIFFERENCES);
            int currentIndex = -1;
            for (int i = 0; differences != null && i < differences.size(); i++) {
                COSBase next = differences.getObject(i);
                if (next instanceof COSNumber) {
                    currentIndex = ((COSNumber) next).intValue();
                } else if (next instanceof COSName) {
                    COSName name = (COSName) next;
                    codeToName.put(currentIndex++, name.getName());
                }
            }
        } else {
            return encoding.getCodeToNameMap();
        }
    }
    return codeToName;
}

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  ww. ja  v a 2  s. 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 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 ww w  .ja  v a2  s  .  co  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 void updateXObj(COSDictionary sourcePageResources, PDFDictionary pageResources) throws IOException {
    COSDictionary xobj = (COSDictionary) sourcePageResources.getDictionaryObject(COSName.XOBJECT);
    if (xobj != null && pdfDoc.isMergeFontsEnabled()) {
        PDFDictionary target = (PDFDictionary) pageResources.get("XObject");
        for (COSName entry : xobj.keySet()) {
            if (newXObj.containsKey(entry)) {
                PDFStream s = (PDFStream) target.get(entry.getName());
                s.setData(newXObj.get(entry).getBytes("ISO-8859-1"));
                PDFDictionary xobjr = (PDFDictionary) s.get("Resources");
                xobjr.put("Font", pageResources.get("Font"));
            }/*from w  w w  .  j  a  v  a2s  . com*/
        }
    }
}

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

License:Apache License

private void handleAnnotations(PDDocument sourceDoc, PDPage page, AffineTransform at) throws IOException {
    PDDocumentCatalog srcCatalog = sourceDoc.getDocumentCatalog();
    PDAcroForm srcAcroForm = srcCatalog.getAcroForm();
    List pageAnnotations = page.getAnnotations();
    if (srcAcroForm == null && pageAnnotations.isEmpty()) {
        return;//from   ww  w. java  2  s .  co  m
    }

    moveAnnotations(page, pageAnnotations, at);

    //Pseudo-cache the target page in place of the original source page.
    //This essentially replaces the original page reference with the target page.
    COSObject cosPage = null;
    COSDictionary parentDic = (COSDictionary) page.getCOSObject().getDictionaryObject(COSName.PARENT,
            COSName.P);
    COSArray kids = (COSArray) parentDic.getDictionaryObject(COSName.KIDS);
    for (int i = 0; i < kids.size(); i++) {
        //Hopefully safe to cast, as kids need to be indirect objects
        COSObject kid = (COSObject) kids.get(i);
        if (!pageNumbers.containsKey(i)) {
            PDFArray a = new PDFArray();
            a.add(null);
            pdfDoc.assignObjectNumber(a);
            pdfDoc.addTrailerObject(a);
            pageNumbers.put(i, a);
        }
        cacheClonedObject(kid, pageNumbers.get(i));
        if (kid.getObject() == page.getCOSObject()) {
            cosPage = kid;
        }
    }
    if (cosPage == null) {
        throw new IOException("Illegal PDF. Page not part of parent page node.");
    }

    Set<COSObject> fields = copyAnnotations(page);

    boolean formAlreadyCopied = getCachedClone(srcAcroForm) != null;
    PDFRoot catalog = this.pdfDoc.getRoot();
    PDFDictionary destAcroForm = (PDFDictionary) catalog.get(COSName.ACRO_FORM.getName());
    if (formAlreadyCopied) {
        //skip, already copied
    } else if (destAcroForm == null) {
        if (srcAcroForm != null) {
            //With this, only the first PDF's AcroForm is copied over. If later AcroForms have
            //different properties besides the actual fields, these get lost. Only fields
            //get merged.
            Collection exclude = Collections.singletonList(COSName.FIELDS);
            destAcroForm = (PDFDictionary) cloneForNewDocument(srcAcroForm, srcAcroForm, exclude);
        } else {
            //Work-around for incorrectly split PDFs which lack an AcroForm but have widgets
            //on pages. This doesn't handle the case where field dicts have "C" entries
            //(for the "CO" entry), so this may produce problems, but we have almost no chance
            //to guess the calculation order.
            destAcroForm = new PDFDictionary(pdfDoc.getRoot());
        }
        pdfDoc.registerObject(destAcroForm);
        catalog.put(COSName.ACRO_FORM.getName(), destAcroForm);
    }
    PDFArray clonedFields = (PDFArray) destAcroForm.get(COSName.FIELDS.getName());
    if (clonedFields == null) {
        clonedFields = new PDFArray();
        destAcroForm.put(COSName.FIELDS.getName(), clonedFields);
    }
    for (COSObject field : fields) {
        PDFDictionary clone = (PDFDictionary) cloneForNewDocument(field, field, Arrays.asList(COSName.KIDS));
        clonedFields.add(clone);
    }
}

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

License:Apache License

private static Function getFunction(PDFunction f) throws IOException {
    if (f instanceof PDFunctionType3) {
        PDFunctionType3 sourceFT3 = (PDFunctionType3) f;
        float[] bounds = sourceFT3.getBounds().toFloatArray();
        COSArray sourceFunctions = sourceFT3.getFunctions();
        List<Function> targetFunctions = new ArrayList<Function>();
        for (int j = 0; j < sourceFunctions.size(); j++) {
            targetFunctions.add(getFunction(PDFunction.create(sourceFunctions.get(j))));
        }//from  ww  w  . j  a  v  a 2 s . c  o m
        return new Function(null, null, targetFunctions, toList(bounds), null);
    } else if (f instanceof PDFunctionType2) {
        PDFunctionType2 sourceFT2 = (PDFunctionType2) f;
        double interpolation = (double) sourceFT2.getN();
        float[] c0 = sourceFT2.getC0().toFloatArray();
        float[] c1 = sourceFT2.getC1().toFloatArray();
        return new Function(null, null, c0, c1, interpolation);
    } else if (f instanceof PDFunctionType0) {
        COSDictionary s = f.getCOSObject();
        assert s instanceof COSStream;
        COSStream stream = (COSStream) s;
        COSArray encode = (COSArray) s.getDictionaryObject(COSName.ENCODE);
        COSArray domain = (COSArray) s.getDictionaryObject(COSName.DOMAIN);
        COSArray range = (COSArray) s.getDictionaryObject(COSName.RANGE);
        int bits = ((COSInteger) s.getDictionaryObject(COSName.BITS_PER_SAMPLE)).intValue();
        COSArray size = (COSArray) s.getDictionaryObject(COSName.SIZE);
        byte[] x = IOUtils.toByteArray(stream.getUnfilteredStream());
        for (byte y : x) {
            if (y != 0) {
                return new Function(floatArrayToDoubleList(domain.toFloatArray()),
                        floatArrayToDoubleList(range.toFloatArray()),
                        floatArrayToDoubleList(encode.toFloatArray()), x, bits, toList(size));
            }
        }
        return null;
    }
    throw new IOException("Unsupported " + f.toString());
}

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   w w  w  .jav  a 2s.com*/
    } 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 createKidFromCOSObject(COSObject baseObj, COSDictionary parentDict, PDFStructElem parent,
        boolean originatedFromTableRow) throws IOException {
    COSBase baseKid = baseObj.getObject();
    if (baseKid instanceof COSInteger) {
        COSInteger number = (COSInteger) baseKid;
        createKids(number, parentDict, parent, originatedFromTableRow);
    } else {/*w  w w . java  2s.c o  m*/
        COSDictionary unwrappedDict = (COSDictionary) baseKid;
        if (unwrappedDict.getDictionaryObject(COSName.S) == null) {
            COSDictionary mcrDict = (COSDictionary) baseKid;
            createKidFromCOSDictionary(mcrDict, parent, parentDict);
        } else if (originatedFromTableRow) {
            int objID = (int) baseObj.getObjectNumber();
            if (structElemCache.get(objID) != null) {
                PDFStructElem kidElem = structElemCache.get(objID);
                parent.addKid(kidElem);
                kidElem.setParent(parent);
            } else {
                createkidEntryFromCosObjectForRow(baseObj, parent);
            }
        } else {
            parent.addKid(null);
        }
    }
}

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

License:Apache License

private boolean checkPageEntryInAncestorsRecursively(COSDictionary elem) {
    if (elem.containsKey(COSName.PG)) {
        COSDictionary pageDict = (COSDictionary) elem.getDictionaryObject(COSName.PG);
        return srcPage.getCOSObject() == pageDict;
    } else if (elem.containsKey(COSName.P)) {
        COSDictionary parent = (COSDictionary) elem.getDictionaryObject(COSName.P);
        return checkPageEntryInAncestorsRecursively(parent);
    } else {/* w w w. j ava2s .  c o  m*/
        return true;
    }
}