Package org.pdfsam.console.business.dto.commands

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


  private PdfStamper pdfStamper = null;
 
  public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException {
    if((parsedCommand != null) && (parsedCommand instanceof SetViewerParsedCommand)){
     
      SetViewerParsedCommand inputCommand = (SetViewerParsedCommand) parsedCommand;

      PrefixParser prefixParser;
      setPercentageOfWorkDone(0);
      try{
        PdfFile[] fileList = arraysConcat(inputCommand.getInputFileList(), getPdfFiles(inputCommand.getInputDirectory()));
        //no input file found
        if (fileList== null || !(fileList.length >0)){
          throw new SetViewerException(SetViewerException.CMD_NO_INPUT_FILE);
        }
        for(int i = 0; i<fileList.length; i++){
          try{
            prefixParser = new PrefixParser(inputCommand.getOutputFilesPrefix(), fileList[i].getFile().getName());
            File tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile());
            pdfReader = new PdfReader(new RandomAccessFileOrArray(fileList[i].getFile().getAbsolutePath()),fileList[i].getPasswordBytes());
            pdfReader.removeUnusedObjects();
            pdfReader.consolidateNamedDestinations();
           
            //version
            LOG.debug("Creating a new document.");
            Character pdfVersion = inputCommand.getOutputPdfVersion();
            if(pdfVersion != null){
              pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(tmpFile), inputCommand.getOutputPdfVersion().charValue());
            }else{
              pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(tmpFile), pdfReader.getPdfVersion());
            }
 
            HashMap meta = pdfReader.getInfo();
            meta.put("Creator", ConsoleServicesFacade.CREATOR);
           
            setCompressionSettingOnStamper(inputCommand, pdfStamper);
           
            pdfStamper.setMoreInfo(meta);
            pdfStamper.setViewerPreferences(inputCommand.getDirection() | inputCommand.getLayout() | inputCommand.getMode() | inputCommand.getNfsmode() | getVewerOptions(inputCommand));
            pdfStamper.close();
            pdfReader.close();
           
            File outFile = new File(inputCommand.getOutputFile() ,prefixParser.generateFileName());
              FileUtility.renameTemporaryFile(tmpFile, outFile, inputCommand.isOverwrite());
                    LOG.debug("File "+outFile.getCanonicalPath()+" created.");
            setPercentageOfWorkDone(((i+1)*WorkDoneDataModel.MAX_PERGENTAGE)/fileList.length)
          }
            catch(Exception e){
              LOG.error("Error setting options for file "+fileList[i].getFile().getName(), e);
            }
        }
        LOG.info("Viewer options set. Pdf files created in "+inputCommand.getOutputFile().getAbsolutePath()+".");
      }catch(Exception e){       
        throw new SetViewerException(e);
      }finally{
        setWorkCompleted();
      }   
View Full Code Here


*/
public class SetViewerCmdValidator extends AbstractCmdValidator {

  public AbstractParsedCommand validateArguments(CmdLineHandler cmdLineHandler) throws ConsoleException {
     
      SetViewerParsedCommand parsedCommandDTO = new SetViewerParsedCommand();
     
      if(cmdLineHandler != null){
        //-o
        FileParam oOption = (FileParam) cmdLineHandler.getOption(SetViewerParsedCommand.O_ARG);
        if ((oOption.isSet())){
                File outFile = oOption.getFile();
                ValidationUtility.assertValidDirectory(outFile);
                parsedCommandDTO.setOutputFile(outFile);             
            }else{
              throw new ParseException(ParseException.ERR_NO_O);
            }
       
        //-p
            StringParam pOption = (StringParam) cmdLineHandler.getOption(SetViewerParsedCommand.P_ARG);
            if(pOption.isSet()){
              parsedCommandDTO.setOutputFilesPrefix(pOption.getValue());
            }
           
        //-mode
            StringParam mOption = (StringParam) cmdLineHandler.getOption(SetViewerParsedCommand.M_ARG);
            if(mOption.isSet()){
              parsedCommandDTO.setMode(getMode(mOption.getValue()));        
            }
        //-layout
            StringParam lOption = (StringParam) cmdLineHandler.getOption(SetViewerParsedCommand.L_ARG);
            if(lOption.isSet()){
              parsedCommandDTO.setLayout(getLayout(lOption.getValue()));        
            }
           
        //-nfsmode
            StringParam nfsmOption = (StringParam) cmdLineHandler.getOption(SetViewerParsedCommand.NFSM_ARG);         
            if(nfsmOption.isSet()){
              parsedCommandDTO.setNfsmode(getNFSMode(nfsmOption.getValue()));
            }
           
        //-direction
            StringParam directionOption = (StringParam) cmdLineHandler.getOption(SetViewerParsedCommand.DIRECTION_ARG);
            if(directionOption.isSet()){
              parsedCommandDTO.setDirection(getDirection(directionOption.getValue()));
            }
           
         //-f -d
        PdfFileParam fOption = (PdfFileParam) cmdLineHandler.getOption(SetViewerParsedCommand.F_ARG);
        FileParam dOption = (FileParam) cmdLineHandler.getOption(SetViewerParsedCommand.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);
        }
           
            //-hidemenu
            parsedCommandDTO.setHideMenu(((BooleanParam) cmdLineHandler.getOption(SetViewerParsedCommand.HIDEMENU_ARG)).isTrue());
            //-hidetoolbar
            parsedCommandDTO.setHideToolBar(((BooleanParam) cmdLineHandler.getOption(SetViewerParsedCommand.HIDETOOLBAR_ARG)).isTrue());
            //-hide window ui
            parsedCommandDTO.setHideWindowUI(((BooleanParam) cmdLineHandler.getOption(SetViewerParsedCommand.HIDEWINDOWUI_ARG)).isTrue());
            //-fit window
            parsedCommandDTO.setFitWindow(((BooleanParam) cmdLineHandler.getOption(SetViewerParsedCommand.FITWINDOW_ARG)).isTrue());
            //-center window
            parsedCommandDTO.setCenterWindow(((BooleanParam) cmdLineHandler.getOption(SetViewerParsedCommand.CENTERWINDOW_ARG)).isTrue());
            //-display doc title
            parsedCommandDTO.setDisplayDocTitle(((BooleanParam) cmdLineHandler.getOption(SetViewerParsedCommand.DOCTITLE_ARG)).isTrue());
            //-noprintscaling
            parsedCommandDTO.setNoPrintScaling(((BooleanParam) cmdLineHandler.getOption(SetViewerParsedCommand.NOPRINTSCALING_ARG)).isTrue());
          
      }else{
        throw new ConsoleException(ConsoleException.CMD_LINE_HANDLER_NULL);
      }       
      return parsedCommandDTO;
View Full Code Here

TOP

Related Classes of org.pdfsam.console.business.dto.commands.SetViewerParsedCommand

Copyright © 2018 www.massapicom. 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.