Example usage for com.lowagie.text.pdf PdfName ANNOTS

List of usage examples for com.lowagie.text.pdf PdfName ANNOTS

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfName ANNOTS.

Prototype

PdfName ANNOTS

To view the source code for com.lowagie.text.pdf PdfName ANNOTS.

Click Source Link

Document

A name

Usage

From source file:eu.europa.cedefop.europass.jtool.util.ExtractAttachments.java

License:EUPL

/**
  * Extract the attachment file/*  w ww.j  ava  2  s  .  c om*/
  * @throws Exception
  */
public void execute() throws Exception {
    boolean hasAttachment = false;
    try {
        PdfReader reader = new PdfReader(in);
        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());
                    unpackFile(filespec);
                }
            }
        }
        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));
                hasAttachment = true;
                unpackFile(filespec);
            }
        }
    } catch (Exception e) {
        log.error("Error while extracting PDF attachements: " + e);
    }
    if (!hasAttachment)
        throw new Exception("PDF file does not have attachment.");
}

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  av a2 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.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  ww . 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<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.kuali.coeus.propdev.impl.budget.subaward.PropDevPropDevBudgetSubAwardServiceImpl.java

License:Open Source License

/**
 * extracts attachments from PDF File//  ww w. ja v  a  2s.  c om
 */

@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.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]);
                }//  w  w w.ja v  a  2 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.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]);
                }//ww w.  j  a va  2s  .  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.pdfsam.console.business.pdf.handlers.UnpackCmdExecutor.java

License:Open Source License

public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException {
    if ((parsedCommand != null) && (parsedCommand instanceof UnpackParsedCommand)) {

        UnpackParsedCommand inputCommand = (UnpackParsedCommand) parsedCommand;
        try {//from  ww w  .  j  a v  a2s .c  o  m
            PdfFile[] fileList = arraysConcat(inputCommand.getInputFileList(),
                    getPdfFiles(inputCommand.getInputDirectory()));
            //check if empty
            if (fileList == null || !(fileList.length > 0)) {
                throw new UnpackException(UnpackException.CMD_NO_INPUT_FILE);
            }

            for (int i = 0; i < fileList.length; i++) {
                int unpackedFiles = 0;
                try {
                    pdfReader = PdfUtility.readerFor(fileList[i]);
                    pdfReader.removeUnusedObjects();
                    pdfReader.consolidateNamedDestinations();

                    PdfDictionary catalog = pdfReader.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 iter = embMap.values().iterator(); iter.hasNext();) {
                                PdfDictionary filespec = (PdfDictionary) PdfReader
                                        .getPdfObject((PdfObject) iter.next());
                                unpackedFiles += unpackFile(filespec, inputCommand.getOutputFile(),
                                        inputCommand.isOverwrite());
                            }
                        }
                    }
                    for (int k = 1; k <= pdfReader.getNumberOfPages(); ++k) {
                        PdfArray annots = (PdfArray) PdfReader
                                .getPdfObject(pdfReader.getPageN(k).get(PdfName.ANNOTS));
                        if (annots != null) {
                            for (Iterator iter = annots.listIterator(); iter.hasNext();) {
                                PdfDictionary annot = (PdfDictionary) PdfReader
                                        .getPdfObject((PdfObject) iter.next());
                                PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
                                if (PdfName.FILEATTACHMENT.equals(subType)) {
                                    PdfDictionary filespec = (PdfDictionary) PdfReader
                                            .getPdfObject(annot.get(PdfName.FS));
                                    unpackedFiles += unpackFile(filespec, inputCommand.getOutputFile(),
                                            inputCommand.isOverwrite());
                                }
                            }
                        }
                    }
                    pdfReader.close();
                    if (unpackedFiles > 0) {
                        LOG.info("File " + fileList[i].getFile().getName() + " unpacked, found " + unpackedFiles
                                + " attachments.");
                    } else {
                        LOG.info("No attachments in " + fileList[i].getFile().getName() + ".");
                    }
                    setPercentageOfWorkDone(((i + 1) * WorkDoneDataModel.MAX_PERGENTAGE) / fileList.length);
                } catch (Exception e) {
                    LOG.error("Error unpacking file " + fileList[i].getFile().getName(), e);
                }
            }
        } catch (Exception e) {
            throw new UnpackException(e);
        } finally {
            setWorkCompleted();
        }
    } else {
        throw new ConsoleException(ConsoleException.ERR_BAD_COMMAND);
    }
}

From source file:org.sejda.impl.itext.component.PdfUnpacker.java

License:Apache License

private Set<PdfDictionary> getFileAttachmentsDictionaries(PdfReader reader) {
    Set<PdfDictionary> retSet = new NullSafeSet<PdfDictionary>();
    for (int k = 1; k <= reader.getNumberOfPages(); ++k) {
        PdfArray annots = (PdfArray) PdfReader.getPdfObject(reader.getPageN(k).get(PdfName.ANNOTS));
        if (annots != null) {
            for (@SuppressWarnings("unchecked")
            Iterator<PdfObject> iter = annots.listIterator(); iter.hasNext();) {
                PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(iter.next());
                PdfName subType = (PdfName) PdfReader.getPdfObject(annot.get(PdfName.SUBTYPE));
                if (PdfName.FILEATTACHMENT.equals(subType)) {
                    retSet.add((PdfDictionary) PdfReader.getPdfObject(annot.get(PdfName.FS)));
                }/*from  www  . j ava  2 s. com*/
            }
        }
    }
    return retSet;
}

From source file:questions.stamppages.RemoveAttachmentAnnotations.java

public static void main(String[] args) throws IOException, DocumentException {
    createPdfWithAttachments();//  ww  w.jav  a  2 s .  c om
    PdfReader reader = new PdfReader(RESOURCE);
    PdfDictionary page;
    PdfDictionary annotation;
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        page = reader.getPageN(i);
        PdfArray annots = page.getAsArray(PdfName.ANNOTS);
        if (annots != null) {
            for (int j = annots.size() - 1; j >= 0; j--) {
                annotation = annots.getAsDict(j);
                if (PdfName.FILEATTACHMENT.equals(annotation.get(PdfName.SUBTYPE))) {
                    annots.remove(j);
                }
            }
        }
    }
    reader.removeUnusedObjects();
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    stamper.close();
}