Examples of UnpackParsedCommand


Examples of org.pdfsam.console.business.dto.commands.UnpackParsedCommand

  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();
View Full Code Here

Examples of org.pdfsam.console.business.dto.commands.UnpackParsedCommand

*/
public class UnpackCmdValidator extends AbstractCmdValidator {

  protected AbstractParsedCommand validateArguments(CmdLineHandler cmdLineHandler) throws ConsoleException {
   
    UnpackParsedCommand parsedCommandDTO = new UnpackParsedCommand();
   
    if(cmdLineHandler != null){ 
      //-o
      FileParam oOption = (FileParam) cmdLineHandler.getOption(UnpackParsedCommand.O_ARG);
      if ((oOption.isSet())){
              File outFile = oOption.getFile();
              ValidationUtility.assertValidDirectory(outFile);
              parsedCommandDTO.setOutputFile(outFile)
          }else{
            throw new ParseException(ParseException.ERR_NO_O);
          }
     
      //-f and -d
      PdfFileParam fOption = (PdfFileParam) cmdLineHandler.getOption(UnpackParsedCommand.F_ARG);
          FileParam dOption = (FileParam) cmdLineHandler.getOption(UnpackParsedCommand.D_ARG);
      if (fOption.isSet() || dOption.isSet()) {
        // -f
        if (fOption.isSet()) {
          // validate file extensions
          for (Iterator fIterator = fOption.getPdfFiles().iterator(); fIterator.hasNext();) {
            PdfFile currentFile = (PdfFile) fIterator.next();
            ValidationUtility.assertValidPdfExtension(currentFile.getFile().getName());           
          }
          parsedCommandDTO.setInputFileList(FileUtility.getPdfFiles(fOption.getPdfFiles()));
        }
        // -d
        if ((dOption.isSet())) {
          File inputDir = dOption.getFile();
          ValidationUtility.assertValidDirectory(inputDir);
          parsedCommandDTO.setInputDirectory(inputDir);
        }
      } else {
        throw new ParseException(ParseException.ERR_NO_F_OR_D);
      }
    }else{
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.