/** extracts attachments from PDF File */ @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; }
/** @see com.lowagie.toolbox.AbstractTool#execute() */ public void execute() { try { if (getValue("srcfile") == null) throw new InstantiationException("You need to choose a sourcefile"); File src = (File) getValue("srcfile"); // we create a reader for a certain document PdfReader reader = new PdfReader(src.getAbsolutePath()); final File parentFile = src.getParentFile(); final String outPath; if (parentFile != null) { outPath = parentFile.getAbsolutePath(); } else { outPath = ""; } PdfDictionary catalog = reader.getCatalog(); PdfDictionary names = catalog.getAsDict(PdfName.NAMES); if (names != null) { PdfDictionary embFiles = names.getAsDict(new PdfName("EmbeddedFiles")); if (embFiles != null) { HashMap<String, PdfObject> embMap = PdfNameTree.readTree(embFiles); for (Iterator<PdfObject> i = embMap.values().iterator(); i.hasNext(); ) { PdfDictionary filespec = (PdfDictionary) PdfReader.getPdfObject(i.next()); unpackFile(reader, filespec, outPath); } } } for (int k = 1; k <= reader.getNumberOfPages(); ++k) { PdfArray annots = reader.getPageN(k).getAsArray(PdfName.ANNOTS); if (annots == null) continue; for (Iterator<PdfObject> i = annots.listIterator(); i.hasNext(); ) { PdfDictionary annot = (PdfDictionary) PdfReader.getPdfObject(i.next()); PdfName subType = annot.getAsName(PdfName.SUBTYPE); if (!PdfName.FILEATTACHMENT.equals(subType)) continue; PdfDictionary filespec = annot.getAsDict(PdfName.FS); unpackFile(reader, filespec, outPath); } } } catch (Exception e) { e.printStackTrace(); } }
@SuppressWarnings("unchecked") public 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; } } } } 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; }