Example usage for org.apache.pdfbox.cos COSObject getObject

List of usage examples for org.apache.pdfbox.cos COSObject getObject

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSObject getObject.

Prototype

public COSBase getObject() 

Source Link

Document

This will get the object that this object encapsulates.

Usage

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

License:Apache License

private void readFontBBox(COSBase b) throws IOException {
    if (b instanceof COSDictionary) {
        COSDictionary dict = (COSDictionary) b;
        for (Map.Entry<COSName, COSBase> n : dict.entrySet()) {
            readFontBBox(n.getValue());/*from w w w . j  a va2 s.com*/
            if (n.getKey() == COSName.FONT_BBOX) {
                COSArray w = (COSArray) n.getValue();
                float[] bboxf = w.toFloatArray();
                int[] bbox = new int[bboxf.length];
                for (int i = 0; i < bbox.length; i++) {
                    bbox[i] = (int) bboxf[i];
                }
                setFontBBox(bbox);
            }
        }
    } else if (b instanceof COSObject) {
        COSObject o = (COSObject) b;
        readFontBBox(o.getObject());
    } else if (b instanceof COSArray) {
        COSArray o = (COSArray) b;
        for (int i = 0; i < o.size(); i++) {
            readFontBBox(o.get(i));
        }
    }
}

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

License:Apache License

private Object readCOSObject(COSObject object, Collection exclude) throws IOException {
    if (log.isTraceEnabled()) {
        log.trace("Cloning indirect object: " + object.getObjectNumber() + " " + object.getGenerationNumber());
    }//  ww w.j ava  2s .c o  m
    Object obj = cloneForNewDocument(object.getObject(), object, exclude);
    if (obj instanceof PDFObject) {
        PDFObject pdfobj = (PDFObject) obj;
        //pdfDoc.registerObject(pdfobj);
        if (!pdfobj.hasObjectNumber()) {
            throw new IllegalStateException("PDF object was not registered!");
        }
        if (log.isTraceEnabled()) {
            log.trace("Object registered: " + pdfobj.getObjectNumber() + " " + pdfobj.getGeneration()
                    + " for COSObject: " + object.getObjectNumber() + " " + object.getGenerationNumber());
        }
    }
    return obj;
}

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  w  w . j av a2 s.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 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.  j ava 2  s . c o  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.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);/* w ww  .  ja  va 2  s.  c  o  m*/
            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

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)));
            }/* www. j av  a2 s  . com*/
        }
    }
    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;
    }/*ww w. j a v  a 2 s  . c o m*/
    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);// w  w w.  j  a  v a  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 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 {//from w w w  .j a  va  2 s  . 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 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);//  w w  w.ja v a 2s .c o  m
    } else {
        parent.addKid(elemRef);
    }
    elemRef.setParent(parent);
}