private PdfReader pdfReader = null;
public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException {
if((parsedCommand != null) && (parsedCommand instanceof UnpackParsedCommand)){
UnpackParsedCommand inputCommand = (UnpackParsedCommand) parsedCommand;
try{
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 = new PdfReader(new RandomAccessFileOrArray(fileList[i].getFile().getAbsolutePath()),fileList[i].getPasswordBytes());
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();