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

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

Introduction

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

Prototype

public Set<Map.Entry<COSName, COSBase>> entrySet() 

Source Link

Document

Returns the name-value entries in this dictionary.

Usage

From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java

License:Apache License

/**
 * visitFromDictionary method comment./*www . java  2 s  .  com*/
 *
 * @param obj The object that is being visited.
 *
 * @throws COSVisitorException If there is an exception while visiting this
 * object.
 *
 * @return null
 */
public Object visitFromDictionary(COSDictionary obj) throws COSVisitorException {
    try {
        getStandardOutput().write(DICT_OPEN);
        getStandardOutput().writeEOL();
        for (Map.Entry<COSName, COSBase> entry : obj.entrySet()) {
            COSBase value = entry.getValue();
            if (value != null) {
                entry.getKey().accept(this);
                getStandardOutput().write(SPACE);
                if (value instanceof COSDictionary) {
                    COSDictionary dict = (COSDictionary) value;

                    // write all XObjects as direct objects, this will save some size
                    COSBase item = dict.getItem(COSName.XOBJECT);
                    if (item != null) {
                        item.setDirect(true);
                    }
                    item = dict.getItem(COSName.RESOURCES);
                    if (item != null) {
                        item.setDirect(true);
                    }

                    if (dict.isDirect()) {
                        // If the object should be written direct, we need
                        // to pass the dictionary to the visitor again.
                        visitFromDictionary(dict);
                    } else {
                        addObjectToWrite(dict);
                        writeReference(dict);
                    }
                } else if (value instanceof COSObject) {
                    COSBase subValue = ((COSObject) value).getObject();
                    if (subValue instanceof COSDictionary || subValue == null) {
                        addObjectToWrite(value);
                        writeReference(value);
                    } else {
                        subValue.accept(this);
                    }
                } else {
                    // If we reach the pdf signature, we need to determinate the position of the
                    // content and byterange
                    if (reachedSignature && COSName.CONTENTS.equals(entry.getKey())) {
                        signaturePosition = new int[2];
                        signaturePosition[0] = (int) getStandardOutput().getPos();
                        value.accept(this);
                        signaturePosition[1] = (int) getStandardOutput().getPos();
                    } else if (reachedSignature && COSName.BYTERANGE.equals(entry.getKey())) {
                        byterangePosition = new int[2];
                        byterangePosition[0] = (int) getStandardOutput().getPos() + 1;
                        value.accept(this);
                        byterangePosition[1] = (int) getStandardOutput().getPos() - 1;
                        reachedSignature = false;
                    } else {
                        value.accept(this);
                    }
                }
                getStandardOutput().writeEOL();

            } else {
                //then we won't write anything, there are a couple cases
                //were the value of an entry in the COSDictionary will
                //be a dangling reference that points to nothing
                //so we will just not write out the entry if that is the case
            }
        }
        getStandardOutput().write(DICT_CLOSE);
        getStandardOutput().writeEOL();
        return null;
    } catch (IOException e) {
        throw new COSVisitorException(e);
    }
}

From source file:net.sf.jabref.logic.xmp.XMPUtil.java

License:Open Source License

/**
 * Helper function for retrieving a BibEntry from the
 * PDDocumentInformation in a PDF file./*ww w.  ja v a2 s. c o  m*/
 *
 * To understand how to get hold of a PDDocumentInformation have a look in
 * the test cases for XMPUtil.
 *
 * The BibEntry is build by mapping individual fields in the document
 * information (like author, title, keywords) to fields in a bibtex entry.
 *
 * @param di
 *            The document information from which to build a BibEntry.
 *
 * @return The bibtex entry found in the document information.
 */
public static Optional<BibEntry> getBibtexEntryFromDocumentInformation(PDDocumentInformation di) {

    BibEntry entry = new BibEntry();
    entry.setType("misc");

    String s = di.getAuthor();
    if (s != null) {
        entry.setField("author", s);
    }

    s = di.getTitle();
    if (s != null) {
        entry.setField("title", s);
    }

    s = di.getKeywords();
    if (s != null) {
        entry.setField("keywords", s);
    }

    s = di.getSubject();
    if (s != null) {
        entry.setField("abstract", s);
    }

    COSDictionary dict = di.getDictionary();
    for (Map.Entry<COSName, COSBase> o : dict.entrySet()) {
        String key = o.getKey().getName();
        if (key.startsWith("bibtex/")) {
            String value = dict.getString(key);
            key = key.substring("bibtex/".length());
            if ("entrytype".equals(key)) {
                entry.setType(value);
            } else {
                entry.setField(key, value);
            }
        }
    }

    // Return empty Optional if no values were found
    return entry.getFieldNames().isEmpty() ? Optional.empty() : Optional.of(entry);
}

From source file:net.sf.jabref.util.XMPUtil.java

License:Open Source License

/**
 * Helper function for retrieving a BibtexEntry from the
 * PDDocumentInformation in a PDF file./*from  www.j  a v a 2s  .c om*/
 * 
 * To understand how to get hold of a PDDocumentInformation have a look in
 * the test cases for XMPUtil.
 * 
 * The BibtexEntry is build by mapping individual fields in the document
 * information (like author, title, keywords) to fields in a bibtex entry.
 * 
 * @param di
 *            The document information from which to build a BibtexEntry.
 * 
 * @return The bibtex entry found in the document information.
 */
@SuppressWarnings("unchecked")
public static BibtexEntry getBibtexEntryFromDocumentInformation(PDDocumentInformation di) {

    BibtexEntry entry = new BibtexEntry();

    String s = di.getAuthor();
    if (s != null) {
        entry.setField("author", s);
    }

    s = di.getTitle();
    if (s != null) {
        entry.setField("title", s);
    }

    s = di.getKeywords();
    if (s != null) {
        entry.setField("keywords", s);
    }

    s = di.getSubject();
    if (s != null) {
        entry.setField("abstract", s);
    }

    COSDictionary dict = di.getDictionary();
    for (Map.Entry<COSName, COSBase> o : dict.entrySet()) {
        String key = o.getKey().getName();
        if (key.startsWith("bibtex/")) {
            String value = dict.getString(key);
            key = key.substring("bibtex/".length());
            if (key.equals("entrytype")) {
                BibtexEntryType type = BibtexEntryType.getStandardType(value);
                if (type != null) {
                    entry.setType(type);
                }
            } else {
                entry.setField(key, value);
            }
        }
    }

    // Return null if no values were found
    return (!entry.getAllFields().isEmpty() ? entry : null);
}

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 ww  .  j  av  a 2s.co m*/
            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 PDFDictionary readCOSDictionary(COSDictionary dic, Object keyBase, Collection exclude)
        throws IOException {
    PDFDictionary newDict = new PDFDictionary();
    cacheClonedObject(keyBase, newDict);
    for (Map.Entry<COSName, COSBase> e : dic.entrySet()) {
        if (!exclude.contains(e.getKey())) {
            newDict.put(e.getKey().getName(), cloneForNewDocument(e.getValue(), e.getValue(), exclude));
        }/*from  ww w . j  a  v a 2s.  c  o  m*/
    }
    return newDict;
}

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 a va 2  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 transferPageDict(COSDictionary fonts, UniqueName uniqueName, PDResources sourcePageResources)
        throws IOException {
    if (fonts != null) {
        for (Map.Entry<COSName, COSBase> f : fonts.entrySet()) {
            String name = uniqueName.getName(f.getKey());
            targetPage.getPDFResources().addFont(name, (PDFDictionary) cloneForNewDocument(f.getValue()));
        }/*w ww.j a  va2 s.  c  o  m*/
    }
    for (Map.Entry<COSName, COSBase> e : sourcePageResources.getCOSObject().entrySet()) {
        transferDict(e, uniqueName);
    }
}

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

License:Apache License

private void transferDict(Map.Entry<COSName, COSBase> dict, UniqueName uniqueName) throws IOException {
    COSBase src;/* w  w w.  j a va 2  s .  co  m*/
    if (dict.getValue() instanceof COSObject) {
        src = ((COSObject) dict.getValue()).getObject();
    } else {
        src = dict.getValue();
    }
    if (dict.getKey() != COSName.FONT && src instanceof COSDictionary) {
        String name = dict.getKey().getName();
        PDFDictionary newDict = (PDFDictionary) targetPage.getPDFResources().get(name);
        if (newDict == null) {
            newDict = new PDFDictionary(targetPage.getPDFResources());
        }
        COSDictionary srcDict = (COSDictionary) src;
        for (Map.Entry<COSName, COSBase> v : srcDict.entrySet()) {
            newDict.put(uniqueName.getName(v.getKey()), cloneForNewDocument(v.getValue()));
        }
        targetPage.getPDFResources().put(name, newDict);
    }
}

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

License:Apache License

public static List<String> findRoleMapKeyByValue(String type, COSDictionary roleMap) {
    List<String> keys = new ArrayList<String>();
    if (roleMap != null) {
        for (Entry<COSName, COSBase> entry : roleMap.entrySet()) {
            String value = ((COSName) entry.getValue()).getName();
            String key = entry.getKey().getName();
            if (type.equals(value)) {
                keys.add(key);// ww  w  . j a v  a  2 s.  c o  m
            }
        }
    }
    return keys;
}

From source file:org.lockss.pdf.MockPdfTokenStream.java

License:Open Source License

/**
 * <p>//from w  w w.jav a 2  s  .co m
 * Converts one PDFBox token into a {@link PdfToken} instance, more
 * specifically a {@link MockPdfToken} instance.
 * </p>
 * 
 * @param pdfBoxToken A token from PDFBox.
 * @return A {@link PdfToken} ({@link MockPdfToken}) instance.
 * @throws IOException if the PDFBox token type is unexpected.
 * @since 1.67
 */
public static PdfToken convert(Object pdfBoxToken) throws IOException {
    PdfTokenFactory tf = new MockPdfTokenFactory();
    if (pdfBoxToken instanceof COSArray) {
        COSArray cosArray = (COSArray) pdfBoxToken;
        List<PdfToken> lst = new ArrayList<PdfToken>(cosArray.size());
        for (Object obj : cosArray) {
            lst.add(convert(obj));
        }
        return tf.makeArray(lst);
    } else if (pdfBoxToken instanceof COSBoolean) {
        return tf.makeBoolean(((COSBoolean) pdfBoxToken).getValue());
    } else if (pdfBoxToken instanceof COSDictionary) {
        COSDictionary cosDictionary = (COSDictionary) pdfBoxToken;
        Map<String, PdfToken> map = new LinkedHashMap<String, PdfToken>(cosDictionary.size());
        for (Map.Entry<COSName, COSBase> ent : cosDictionary.entrySet()) {
            map.put(ent.getKey().getName(), convert(ent.getValue()));
        }
        return tf.makeDictionary(map);
    } else if (pdfBoxToken instanceof COSFloat) {
        return tf.makeFloat(((COSFloat) pdfBoxToken).floatValue());
    } else if (pdfBoxToken instanceof COSInteger) {
        return tf.makeFloat(((COSInteger) pdfBoxToken).intValue());
    } else if (pdfBoxToken instanceof COSName) {
        return tf.makeName(((COSName) pdfBoxToken).getName());
    } else if (pdfBoxToken instanceof COSNull) {
        return tf.makeNull();
    } else if (pdfBoxToken instanceof COSString) {
        return tf.makeString(((COSString) pdfBoxToken).getString());
    } else if (pdfBoxToken instanceof PDFOperator) {
        return tf.makeOperator(((PDFOperator) pdfBoxToken).getOperation());
    } else {
        throw new IOException(String.format("Unexpected type: %s \"%s\"", pdfBoxToken.getClass().getName(),
                pdfBoxToken.toString()));
    }
}