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

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

Introduction

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

Prototype

PdfName NAMES

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

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  w w .j  av a2 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  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.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;
                }/*from  www . ja va 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<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 . j a  v a2s. 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.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  ww .j  a v a2  s .  com*/
            }
        }
    }
    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]);
                }/*from  w w  w .j a va  2s. 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 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  w w w.ja v  a 2s  .  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> getEmbeddedFilesDictionaries(PdfReader reader) {
    Set<PdfDictionary> retSet = new NullSafeSet<PdfDictionary>();
    PdfDictionary catalog = reader.getCatalog();
    PdfDictionary names = (PdfDictionary) PdfReader.getPdfObject(catalog.get(PdfName.NAMES));
    if (names != null) {
        PdfDictionary embFiles = (PdfDictionary) PdfReader.getPdfObject(names.get(PdfName.EMBEDDEDFILES));
        if (embFiles != null) {
            @SuppressWarnings("unchecked")
            HashMap<String, PdfObject> embMap = PdfNameTree.readTree(embFiles);
            for (PdfObject value : embMap.values()) {
                retSet.add((PdfDictionary) PdfReader.getPdfObject(value));
            }/*from   w w w  .  j a  va 2s  .  c  o  m*/
        }
    }
    return retSet;
}

From source file:org.xhtmlrenderer.pdf.ITextOutputDevice.java

License:Open Source License

private void writeNamedDestinations(RenderingContext c) {
    Map idMap = getSharedContext().getIdMap();
    if ((idMap != null) && (!idMap.isEmpty())) {
        PdfArray dests = new PdfArray();
        try {//from   ww  w.  j  a v  a2  s . c o m
            Iterator it = idMap.entrySet().iterator();
            while (it.hasNext()) {
                Entry entry = (Entry) it.next();

                Box targetBox = (Box) entry.getValue();

                if (targetBox.getStyle().isIdent(CSSName.FS_NAMED_DESTINATION, IdentValue.CREATE)) {
                    String anchorName = (String) entry.getKey();
                    dests.add(new PdfString(anchorName, PdfString.TEXT_UNICODE));

                    PdfDestination dest = createDestination(c, targetBox);
                    if (dest != null) {
                        PdfIndirectReference ref = _writer.addToBody(dest).getIndirectReference();
                        dests.add(ref);
                    }
                }
            }

            if (!dests.isEmpty()) {
                PdfDictionary nametree = new PdfDictionary();
                nametree.put(PdfName.NAMES, dests);
                PdfIndirectReference nameTreeRef = _writer.addToBody(nametree).getIndirectReference();

                PdfDictionary names = new PdfDictionary();
                names.put(PdfName.DESTS, nameTreeRef);
                PdfIndirectReference destinationsRef = _writer.addToBody(names).getIndirectReference();

                _writer.getExtraCatalog().put(PdfName.NAMES, destinationsRef);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:questions.javascript.RemoveJavaScript.java

public static void main(String[] args) throws DocumentException, IOException {
    // creating the form with JS
    AddJavaScriptToForm.main(args);/*w  w  w.  ja va2s. co  m*/
    // removing the document level JS
    PdfReader reader = new PdfReader(AddJavaScriptToForm.RESULT);
    PdfDictionary root = reader.getCatalog();
    PdfDictionary names = root.getAsDict(PdfName.NAMES);
    names.remove(PdfName.JAVASCRIPT);
    if (names.size() == 0) {
        root.remove(PdfName.NAMES);
    }
    reader.removeUnusedObjects();
    // filling out and flattening the form
    // (if you don't flatten, you'll get JS errors!)
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    AcroFields form = stamper.getAcroFields();
    form.setField("married", "no");
    stamper.setFormFlattening(true);
    stamper.close();
}