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);