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

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


    public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException {

        if ((parsedCommand != null) && (parsedCommand instanceof RotateParsedCommand)) {

            RotateParsedCommand inputCommand = (RotateParsedCommand) parsedCommand;
            setPercentageOfWorkDone(0);
            PrefixParser prefixParser;

            try {
                PdfFile[] fileList = inputCommand.getInputFileList();
                for (int i = 0; i < fileList.length; i++) {
                    try {

                        prefixParser = new PrefixParser(inputCommand.getOutputFilesPrefix(), fileList[i].getFile()
                                .getName());
                        File tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile());
                        LOG.debug("Opening " + fileList[i].getFile().getAbsolutePath());
                        pdfReader = new PdfReader(new FileInputStream(fileList[i].getFile().getAbsolutePath()),
                                fileList[i].getPasswordBytes());
                        pdfReader.removeUnusedObjects();
                        pdfReader.consolidateNamedDestinations();

                        int pdfNumberOfPages = pdfReader.getNumberOfPages();
                        PageRotation rotation = inputCommand.getRotation();
                        // rotate all
                        if (rotation.getType() == PageRotation.ALL_PAGES) {
                            int pageRotation = rotation.getDegrees();
                            LOG.debug("Applying rotation of " + pageRotation + " for all pages");
                            for (int j = 1; j <= pdfNumberOfPages; j++) {
                                PdfDictionary dictionary = pdfReader.getPageN(j);
                                int rotationDegrees = (pageRotation + pdfReader.getPageRotation(j)) % 360;
                                dictionary.put(PdfName.ROTATE, new PdfNumber(rotationDegrees));
                            }
                        } else if (rotation.getType() == PageRotation.ODD_PAGES) {
                            // odd pages rotation
                            int pageRotation = rotation.getDegrees();
                            LOG.debug("Applying rotation of " + pageRotation + " for odd pages");
                            for (int j = 1; j <= pdfNumberOfPages; j = j + 2) {
                                PdfDictionary dictionary = pdfReader.getPageN(j);
                                int rotationDegrees = (pageRotation + pdfReader.getPageRotation(j)) % 360;
                                dictionary.put(PdfName.ROTATE, new PdfNumber(rotationDegrees));
                            }
                        } else if (rotation.getType() == PageRotation.EVEN_PAGES) {
                            // even pages rotation
                            int pageRotation = rotation.getDegrees();
                            LOG.debug("Applying rotation of " + pageRotation + " for even pages");
                            for (int j = 2; j <= pdfNumberOfPages; j = j + 2) {
                                PdfDictionary dictionary = pdfReader.getPageN(j);
                                int rotationDegrees = (pageRotation + pdfReader.getPageRotation(j)) % 360;
                                dictionary.put(PdfName.ROTATE, new PdfNumber(rotationDegrees));
                            }
                        } else {
                            LOG.warn("Unable to find the rotation type. " + rotation);
                        }

                        // 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.close();
                        pdfReader.close();
                        File outFile = new File(inputCommand.getOutputFile(), prefixParser.generateFileName());
                        FileUtility.renameTemporaryFile(tmpFile, outFile, inputCommand.isOverwrite());
                        LOG.debug("Rotated file " + outFile.getCanonicalPath() + " created.");
                        setPercentageOfWorkDone(((i + 1) * WorkDoneDataModel.MAX_PERGENTAGE) / fileList.length);
                    } catch (Exception e) {
                        LOG.error("Error rotating file " + fileList[i].getFile().getName(), e);
                    }
                }
                LOG.info("Pdf files rotated in " + inputCommand.getOutputFile().getAbsolutePath() + ".");
            } catch (Exception e) {
                throw new EncryptException(e);
            } finally {
                setWorkCompleted();
            }
View Full Code Here


*/
public class RotateCmdValidator extends AbstractCmdValidator {

protected AbstractParsedCommand validateArguments(CmdLineHandler cmdLineHandler) throws ConsoleException {
   
    RotateParsedCommand parsedCommandDTO = new RotateParsedCommand();
   
    if(cmdLineHandler != null){
      //-o
      FileParam oOption = (FileParam) cmdLineHandler.getOption(RotateParsedCommand.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(RotateParsedCommand.P_ARG);
          if(pOption.isSet()){
            parsedCommandDTO.setOutputFilesPrefix(pOption.getValue());
          }         
         
          //-f
          PdfFileParam fOption = (PdfFileParam) cmdLineHandler.getOption(RotateParsedCommand.F_ARG);
          if(fOption.isSet()){
        //validate file extensions
            for(Iterator fIterator = fOption.getPdfFiles().iterator(); fIterator.hasNext();){
              PdfFile currentFile = (PdfFile) fIterator.next();
                //checking extension
                ValidationUtility.assertValidPdfExtension(currentFile.getFile().getName());
            }
            parsedCommandDTO.setInputFileList(FileUtility.getPdfFiles(fOption.getPdfFiles()));
          }
         
          //-r
          StringParam rOption = (StringParam) cmdLineHandler.getOption(RotateParsedCommand.R_ARG);
          if(rOption.isSet()){
            PageRotation[] rotations = ValidationUtility.getPagesRotation(rOption.getValue(), false);
            if(rotations.length == 1){
              parsedCommandDTO.setRotation(rotations[0]);
            }else{
              throw new RotateException(RotateException.ERR_NOT_SINGLE_ROTATION, new String[]{rOption.getValue()});
            }         
          }
    }else{
View Full Code Here

TOP

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

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.