Example usage for com.lowagie.text.pdf PdfReader getPdfObject

List of usage examples for com.lowagie.text.pdf PdfReader getPdfObject

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfReader getPdfObject.

Prototype

public PdfObject getPdfObject(int idx) 

Source Link

Usage

From source file:net.sqs2.omr.master.sqm.PDFAttachmentExtractor.java

License:Apache License

@SuppressWarnings("unchecked")
private static byte[] extractAttachmentFiles(PdfReader reader, String suffix) throws IOException {
    PdfDictionary catalog = reader.getCatalog();
    PdfDictionary names = (PdfDictionary) PdfReader.getPdfObject(catalog.get(PdfName.NAMES));
    if (names != null) {
        PdfDictionary embFiles = (PdfDictionary) PdfReader
                .getPdfObject(names.get(new PdfName("EmbeddedFiles")));
        if (embFiles != null) {
            HashMap<?, PdfObject> embMap = PdfNameTree.readTree(embFiles);
            for (Iterator<PdfObject> i = embMap.values().iterator(); i.hasNext();) {
                PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(i.next());
                byte[] ret = unpackFile(reader, filespec, suffix);
                if (ret != null) {
                    return ret;
                }/*from   w  w  w.j a  v  a  2 s.co  m*/
            }
        }
    }
    for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
        PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS));
        if (annots == null) {
            continue;
        }
        for (Iterator<PdfObject> i = annots.listIterator(); i.hasNext();) {
            PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(i.next());
            PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
            if (!PdfName.FILEATTACHMENT.equals(subType)) {
                continue;
            }
            PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS));
            byte[] ret = unpackFile(reader, filespec, suffix);
            if (ret != null) {
                return ret;
            }
        }
    }
    return null;
}

From source file:net.sqs2.omr.master.sqm.PDFAttachmentExtractor.java

License:Apache License

/**
 * Unpacks a file attachment./*from  w w  w  .j  av  a2s. com*/
 * 
 * @param reader
 *            The object that reads the PDF document
 * @param filespec
 *            The dictionary containing the file specifications
 * @param outPath
 *            The path where the attachment has to be written
 * @throws IOException
 */
private static byte[] unpackFile(PdfReader reader, PdfDictionary filespec, String suffix) throws IOException {
    if (filespec == null) {
        return null;
    }
    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));
    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) {
        return null;
    }
    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null) {
        return null;
    }
    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null) {
        return null;
    }
    File fLast = new File(fn.toUnicodeString());
    String filename = fLast.getName();

    if (!filename.endsWith(suffix)) {
        return null;
    }

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null) {
        return null;
    }
    return PdfReader.getStreamBytes(prs);
}

From source file:net.sqs2.omr.master.sqm.PDFAttachmentToSQMTranslator.java

License:Apache License

private static byte[] extractAttachmentFiles(PdfReader reader, String suffix) throws IOException {
    PdfDictionary catalog = reader.getCatalog();
    PdfDictionary names = (PdfDictionary) PdfReader.getPdfObject(catalog.get(PdfName.NAMES));
    if (names != null) {
        PdfDictionary embFiles = (PdfDictionary) PdfReader
                .getPdfObject(names.get(new PdfName("EmbeddedFiles")));
        if (embFiles != null) {
            HashMap<?, PdfObject> embMap = PdfNameTree.readTree(embFiles);
            for (Iterator<PdfObject> i = embMap.values().iterator(); i.hasNext();) {
                PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(i.next());
                byte[] ret = unpackFile(reader, filespec, suffix);
                if (ret != null) {
                    return ret;
                }/*w  w w .ja  va2s  .c o m*/
            }
        }
    }
    for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
        PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS));
        if (annots == null) {
            continue;
        }
        for (Iterator<PdfObject> i = annots.listIterator(); i.hasNext();) {
            PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(i.next());
            PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
            if (!PdfName.FILEATTACHMENT.equals(subType)) {
                continue;
            }
            PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS));
            byte[] ret = unpackFile(reader, filespec, suffix);
            if (ret != null) {
                return ret;
            }
        }
    }
    return null;
}

From source file:org.archive.modules.extractor.PDFParser.java

License:Apache License

/**
 * Parse a PdfDictionary, looking for URIs recursively and adding
 * them to foundURIs/*from  w w w .  java  2s .co  m*/
 * @param entity
 */
@SuppressWarnings("unchecked")
protected void extractURIs(PdfObject entity) {

    // deal with dictionaries
    if (entity.isDictionary()) {

        PdfDictionary dictionary = (PdfDictionary) entity;

        Set<PdfName> allkeys = dictionary.getKeys();
        for (PdfName key : allkeys) {
            PdfObject value = dictionary.get(key);

            // see if it's the key is a UR[I,L]
            if (key.toString().equals("/URI") || key.toString().equals("/URL")) {
                foundURIs.add(value.toString());

            } else {
                this.extractURIs(value);
            }

        }

        // deal with arrays
    } else if (entity.isArray()) {

        PdfArray array = (PdfArray) entity;
        for (PdfObject pdfObject : (Iterable<PdfObject>) array.getArrayList()) {
            this.extractURIs(pdfObject);
        }

        // deal with indirect references
    } else if (entity.getClass() == PRIndirectReference.class) {

        PRIndirectReference indirect = (PRIndirectReference) entity;

        // if we've already seen a reference to this object
        if (haveSeen(indirect.getGeneration(), indirect.getNumber())) {
            return;

            // note that we've seen it if it's new
        } else {
            markAsSeen(indirect.getGeneration(), indirect.getNumber());
        }

        // dereference the "pointer" and process the object
        indirect.getReader(); // FIXME: examine side-effects
        PdfObject direct = PdfReader.getPdfObject(indirect);

        this.extractURIs(direct);
    }
}

From source file:org.kuali.coeus.propdev.impl.budget.subaward.PropDevPropDevBudgetSubAwardServiceImpl.java

License:Open Source License

/**
 * extracts attachments from PDF File/*  www  .  jav  a 2  s.  c o  m*/
 */

@SuppressWarnings("unchecked")
protected Map extractAttachments(PdfReader reader) throws IOException {
    Map fileMap = new HashMap();
    PdfDictionary catalog = reader.getCatalog();
    PdfDictionary names = (PdfDictionary) PdfReader.getPdfObject(catalog.get(PdfName.NAMES));
    if (names != null) {
        PdfDictionary embFiles = (PdfDictionary) PdfReader
                .getPdfObject(names.get(new PdfName("EmbeddedFiles")));
        if (embFiles != null) {
            HashMap embMap = PdfNameTree.readTree(embFiles);
            for (Iterator i = embMap.values().iterator(); i.hasNext();) {
                PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
                Object fileInfo[] = unpackFile(reader, filespec);
                if (fileMap.containsKey(fileInfo[0])) {
                    throw new RuntimeException(DUPLICATE_FILE_NAMES);
                }
                fileMap.put(fileInfo[0], fileInfo[1]);
            }
        }
    }
    for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
        PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS));
        if (annots == null) {
            continue;
        }
        for (Iterator i = annots.getArrayList().listIterator(); i.hasNext();) {
            PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
            PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
            if (!PdfName.FILEATTACHMENT.equals(subType)) {
                continue;
            }
            PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS));
            Object fileInfo[] = unpackFile(reader, filespec);
            if (fileMap.containsKey(fileInfo[0])) {
                throw new RuntimeException(DUPLICATE_FILE_NAMES);
            }

            fileMap.put(fileInfo[0], fileInfo[1]);
        }
    }

    return fileMap;
}

From source file:org.kuali.coeus.propdev.impl.budget.subaward.PropDevPropDevBudgetSubAwardServiceImpl.java

License:Open Source License

/**
 * Unpacks a file attachment./*from www  . j av  a2 s  .  com*/
 * @param reader The object that reads the PDF document
 * @param filespec The dictonary containing the file specifications
 * @throws IOException
 */

protected static Object[] unpackFile(PdfReader reader, PdfDictionary filespec) throws IOException {
    Object arr[] = new Object[2]; //use to store name and file bytes
    if (filespec == null) {
        return null;
    }

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));
    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type)) {
        return null;
    }

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null) {
        return null;
    }

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null) {
        return null;
    }

    File fLast = new File(fn.toUnicodeString());
    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null) {
        return null;
    }

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    arr[0] = fLast.getName();
    arr[1] = attachmentByte;

    return arr;

}

From source file:org.kuali.coeus.propdev.impl.s2s.S2sUserAttachedFormServiceImpl.java

License:Open Source License

private Map extractAttachments(PdfReader reader) throws IOException {
    Map fileMap = new HashMap();

    PdfDictionary catalog = reader.getCatalog();
    PdfDictionary names = (PdfDictionary) PdfReader.getPdfObject(catalog.get(PdfName.NAMES));
    if (names != null) {
        PdfDictionary embFiles = (PdfDictionary) PdfReader
                .getPdfObject(names.get(new PdfName("EmbeddedFiles")));
        if (embFiles != null) {
            HashMap embMap = PdfNameTree.readTree(embFiles);

            for (Iterator i = embMap.values().iterator(); i.hasNext();) {
                PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
                Object[] fileInfo = unpackFile(filespec);
                if (!fileMap.containsKey(fileInfo[0])) {
                    fileMap.put(fileInfo[0], fileInfo[1]);
                }/*from  ww w.j a  v  a2  s.c  o  m*/
            }
        }
    }
    for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
        PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS));
        if (annots == null)
            continue;
        for (Iterator i = annots.listIterator(); i.hasNext();) {
            PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
            PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
            if (!PdfName.FILEATTACHMENT.equals(subType))
                continue;
            PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS));
            Object[] fileInfo = unpackFile(filespec);
            if (fileMap.containsKey(fileInfo[0])) {
                throw new RuntimeException(DUPLICATE_FILE_NAMES);
            }
            fileMap.put(fileInfo[0], fileInfo[1]);
        }
    }

    return fileMap;
}

From source file:org.kuali.coeus.propdev.impl.s2s.S2sUserAttachedFormServiceImpl.java

License:Open Source License

/**
 * Unpacks a file attachment./*from   ww  w . j  a  va 2 s.  c  o m*/
 * @param filespec
 *            The dictonary containing the file specifications
 * @throws IOException
 */
private Object[] unpackFile(PdfDictionary filespec) throws IOException {

    if (filespec == null)
        return null;

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));

    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type))
        return null;

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null)
        return null;

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null)
        return null;

    File fLast = new File(fn.toUnicodeString());

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null)
        return null;

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    Object[] fileInfo = new Object[2];
    fileInfo[0] = fLast.getName();
    fileInfo[1] = attachmentByte;
    return fileInfo;
}

From source file:org.kuali.kra.s2s.service.impl.S2SUserAttachedFormServiceImpl.java

License:Educational Community License

private Map extractAttachments(PdfReader reader) throws IOException {
    Map fileMap = new HashMap();

    PdfDictionary catalog = reader.getCatalog();
    PdfDictionary names = (PdfDictionary) PdfReader.getPdfObject(catalog.get(PdfName.NAMES));
    if (names != null) {
        PdfDictionary embFiles = (PdfDictionary) PdfReader
                .getPdfObject(names.get(new PdfName("EmbeddedFiles")));
        if (embFiles != null) {
            HashMap embMap = PdfNameTree.readTree(embFiles);

            for (Iterator i = embMap.values().iterator(); i.hasNext();) {
                PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
                Object[] fileInfo = unpackFile(reader, filespec);
                if (!fileMap.containsKey(fileInfo[0])) {
                    fileMap.put(fileInfo[0], fileInfo[1]);
                }//from   w w  w.  j ava2  s.c  o  m
            }
        }
    }
    for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
        PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS));
        if (annots == null)
            continue;
        for (Iterator i = annots.listIterator(); i.hasNext();) {
            PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject((PdfObject) i.next());
            PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
            if (!PdfName.FILEATTACHMENT.equals(subType))
                continue;
            PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS));
            Object[] fileInfo = unpackFile(reader, filespec);
            if (fileMap.containsKey(fileInfo[0])) {
                throw new RuntimeException(DUPLICATE_FILE_NAMES);
            }
            fileMap.put(fileInfo[0], fileInfo[1]);
        }
    }

    return fileMap;
}

From source file:org.kuali.kra.s2s.service.impl.S2SUserAttachedFormServiceImpl.java

License:Educational Community License

/**
 * Unpacks a file attachment./*from ww w.jav  a 2s. c  o m*/
 *
 * @param reader
 *            The object that reads the PDF document
 * @param filespec
 *            The dictonary containing the file specifications
 * @throws IOException
 */
private Object[] unpackFile(PdfReader reader, PdfDictionary filespec) throws IOException {
    S2sUserAttachedFormAtt userAttachedS2SFormAttachmentBean = new S2sUserAttachedFormAtt();

    if (filespec == null)
        return null;

    PdfName type = (PdfName) PdfReader.getPdfObject(filespec.get(PdfName.TYPE));

    if (!PdfName.F.equals(type) && !PdfName.FILESPEC.equals(type))
        return null;

    PdfDictionary ef = (PdfDictionary) PdfReader.getPdfObject(filespec.get(PdfName.EF));
    if (ef == null)
        return null;

    PdfString fn = (PdfString) PdfReader.getPdfObject(filespec.get(PdfName.F));
    if (fn == null)
        return null;

    File fLast = new File(fn.toUnicodeString());

    PRStream prs = (PRStream) PdfReader.getPdfObject(ef.get(PdfName.F));
    if (prs == null)
        return null;

    byte attachmentByte[] = PdfReader.getStreamBytes(prs);
    Object[] fileInfo = new Object[2];
    fileInfo[0] = fLast.getName();
    fileInfo[1] = attachmentByte;
    return fileInfo;
}