List of usage examples for com.lowagie.text.pdf PdfStamper PdfStamper
public PdfStamper(PdfReader reader, OutputStream os, char pdfVersion) throws DocumentException, IOException
From source file:net.sf.jsignpdf.UncompressPdf.java
License:Mozilla Public License
/** * The main 'main'./*from ww w. j av a2 s . c om*/ * * @param args */ public static void main(String[] args) { if (args == null || args.length == 0) { System.out.println("Usage:\njava " + UncompressPdf.class.getName() + " file.pdf [file2.pdf [...]]"); return; } Document.compress = false; for (String tmpFile : args) { String newFileName = null; if (tmpFile.toLowerCase().endsWith(".pdf")) { newFileName = tmpFile.substring(0, tmpFile.length() - 4) + "_uncompressed.pdf"; } else { newFileName = tmpFile + "_uncompressed.pdf"; } System.out.println("Uncompressing " + tmpFile + " to " + newFileName); try { PdfReader reader = new PdfReader(tmpFile); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(newFileName), '\0'); int total = reader.getNumberOfPages() + 1; for (int i = 1; i < total; i++) { reader.setPageContent(i, reader.getPageContent(i)); } stamper.close(); } catch (NullPointerException npe) { npe.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:org.pdfsam.console.business.pdf.handlers.ConcatCmdExecutor.java
License:Open Source License
/** * Apply pages rotations//from ww w. ja va2 s .c o m * * @param inputFile * @param inputCommand * @return temporary file with pages rotation */ private File applyRotations(File inputFile, ConcatParsedCommand inputCommand) throws Exception { rotationReader = new PdfReader(inputFile.getAbsolutePath()); rotationReader.removeUnusedObjects(); rotationReader.consolidateNamedDestinations(); int pdfNumberOfPages = rotationReader.getNumberOfPages(); PageRotation[] rotations = inputCommand.getRotations(); if (rotations != null && rotations.length > 0) { if (rotations.length > 1) { for (int i = 0; i < rotations.length; i++) { if (pdfNumberOfPages >= rotations[i].getPageNumber() && rotations[i].getPageNumber() > 0) { PdfDictionary dictionary = rotationReader.getPageN(rotations[i].getPageNumber()); int rotation = (rotations[i].getDegrees() + rotationReader.getPageRotation(rotations[i].getPageNumber())) % 360; dictionary.put(PdfName.ROTATE, new PdfNumber(rotation)); } else { LOG.warn("Rotation for page " + rotations[i].getPageNumber() + " ignored."); } } } else { // rotate all if (rotations[0].getType() == PageRotation.ALL_PAGES) { int pageRotation = rotations[0].getDegrees(); for (int i = 1; i <= pdfNumberOfPages; i++) { PdfDictionary dictionary = rotationReader.getPageN(i); int rotation = (pageRotation + rotationReader.getPageRotation(i)) % 360; dictionary.put(PdfName.ROTATE, new PdfNumber(rotation)); } } else if (rotations[0].getType() == PageRotation.SINGLE_PAGE) { // single page rotation if (pdfNumberOfPages >= rotations[0].getPageNumber() && rotations[0].getPageNumber() > 0) { PdfDictionary dictionary = rotationReader.getPageN(rotations[0].getPageNumber()); int rotation = (rotations[0].getDegrees() + rotationReader.getPageRotation(rotations[0].getPageNumber())) % 360; dictionary.put(PdfName.ROTATE, new PdfNumber(rotation)); } else { LOG.warn("Rotation for page " + rotations[0].getPageNumber() + " ignored."); } } else if (rotations[0].getType() == PageRotation.ODD_PAGES) { // odd pages rotation int pageRotation = rotations[0].getDegrees(); for (int i = 1; i <= pdfNumberOfPages; i = i + 2) { PdfDictionary dictionary = rotationReader.getPageN(i); int rotation = (pageRotation + rotationReader.getPageRotation(i)) % 360; dictionary.put(PdfName.ROTATE, new PdfNumber(rotation)); } } else if (rotations[0].getType() == PageRotation.EVEN_PAGES) { // even pages rotation int pageRotation = rotations[0].getDegrees(); for (int i = 2; i <= pdfNumberOfPages; i = i + 2) { PdfDictionary dictionary = rotationReader.getPageN(i); int rotation = (pageRotation + rotationReader.getPageRotation(i)) % 360; dictionary.put(PdfName.ROTATE, new PdfNumber(rotation)); } } else { LOG.warn("Unable to find the rotation type. " + rotations[0]); } } LOG.info("Pages rotation applied."); } File rotatedTmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile()); Character pdfVersion = inputCommand.getOutputPdfVersion(); if (pdfVersion != null) { rotationStamper = new PdfStamper(rotationReader, new FileOutputStream(rotatedTmpFile), inputCommand.getOutputPdfVersion().charValue()); } else { rotationStamper = new PdfStamper(rotationReader, new FileOutputStream(rotatedTmpFile), rotationReader.getPdfVersion()); } HashMap meta = rotationReader.getInfo(); meta.put("Creator", ConsoleServicesFacade.CREATOR); setCompressionSettingOnStamper(inputCommand, rotationStamper); rotationStamper.setMoreInfo(meta); rotationStamper.close(); rotationReader.close(); return rotatedTmpFile; }
From source file:org.pdfsam.console.business.pdf.handlers.DecryptCmdExecutor.java
License:Open Source License
public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException { if ((parsedCommand != null) && (parsedCommand instanceof DecryptParsedCommand)) { DecryptParsedCommand inputCommand = (DecryptParsedCommand) parsedCommand; setPercentageOfWorkDone(0);//from w ww . ja v a 2 s . c o m 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()); pdfReader = PdfUtility.readerFor(fileList[i]); 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.close(); pdfReader.close(); File outFile = new File(inputCommand.getOutputFile(), prefixParser.generateFileName()); FileUtility.renameTemporaryFile(tmpFile, outFile, inputCommand.isOverwrite()); LOG.debug("Decrypted file " + outFile.getCanonicalPath() + " created."); setPercentageOfWorkDone(((i + 1) * WorkDoneDataModel.MAX_PERGENTAGE) / fileList.length); } catch (Exception e) { LOG.error("Error decrypting file " + fileList[i].getFile().getName(), e); } } LOG.info("Pdf files decrypted in " + inputCommand.getOutputFile().getAbsolutePath() + "."); } finally { setWorkCompleted(); } } else { throw new ConsoleException(ConsoleException.ERR_BAD_COMMAND); } }
From source file:org.pdfsam.console.business.pdf.handlers.DocumentInfoCmdExecutor.java
License:Open Source License
public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException { if ((parsedCommand != null) && (parsedCommand instanceof DocumentInfoParsedCommand)) { DocumentInfoParsedCommand inputCommand = (DocumentInfoParsedCommand) parsedCommand; setPercentageOfWorkDone(0);/*from w w w .j ava 2 s. c o m*/ try { File tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile()); pdfReader = PdfUtility.readerFor(inputCommand.getInputFile()); 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); if (inputCommand.getAuthor() != null) { meta.put(AUTHOR, inputCommand.getAuthor()); } if (inputCommand.getSubject() != null) { meta.put(SUBJECT, inputCommand.getSubject()); } if (inputCommand.getTitle() != null) { meta.put(TITLE, inputCommand.getTitle()); } if (inputCommand.getKeywords() != null) { meta.put(KEYWORDS, inputCommand.getKeywords()); } setCompressionSettingOnStamper(inputCommand, pdfStamper); pdfStamper.setMoreInfo(meta); pdfStamper.close(); pdfReader.close(); FileUtility.renameTemporaryFile(tmpFile, inputCommand.getOutputFile(), inputCommand.isOverwrite()); LOG.debug("File " + inputCommand.getOutputFile().getCanonicalPath() + " created."); } catch (Exception e) { throw new ConsoleException(e); } finally { setWorkCompleted(); } } else { throw new ConsoleException(ConsoleException.ERR_BAD_COMMAND); } }
From source file:org.pdfsam.console.business.pdf.handlers.EncryptCmdExecutor.java
License:Open Source License
public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException { if ((parsedCommand != null) && (parsedCommand instanceof EncryptParsedCommand)) { EncryptParsedCommand inputCommand = (EncryptParsedCommand) parsedCommand; setPercentageOfWorkDone(0);/*from ww w .j a v a2 s. c om*/ int encType = PdfWriter.STANDARD_ENCRYPTION_40; PrefixParser prefixParser; try { PdfFile[] fileList = arraysConcat(inputCommand.getInputFileList(), getPdfFiles(inputCommand.getInputDirectory())); // check if empty if (fileList == null || !(fileList.length > 0)) { throw new EncryptException(EncryptException.CMD_NO_INPUT_FILE); } for (int i = 0; i < fileList.length; i++) { try { // set the encryption type if (EncryptParsedCommand.E_AES_128.equals(inputCommand.getEncryptionType())) { encType = PdfWriter.ENCRYPTION_AES_128; } else if (EncryptParsedCommand.E_RC4_128.equals(inputCommand.getEncryptionType())) { encType = PdfWriter.STANDARD_ENCRYPTION_128; } prefixParser = new PrefixParser(inputCommand.getOutputFilesPrefix(), fileList[i].getFile().getName()); File tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile()); pdfReader = PdfUtility.readerFor(fileList[i]); 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.setEncryption(encType, inputCommand.getUserPwd(), inputCommand.getOwnerPwd(), inputCommand.getPermissions()); pdfStamper.close(); pdfReader.close(); File outFile = new File(inputCommand.getOutputFile(), prefixParser.generateFileName()); FileUtility.renameTemporaryFile(tmpFile, outFile, inputCommand.isOverwrite()); LOG.debug("Encrypted file " + outFile.getCanonicalPath() + " created."); setPercentageOfWorkDone(((i + 1) * WorkDoneDataModel.MAX_PERGENTAGE) / fileList.length); } catch (Exception e) { LOG.error("Error encrypting file " + fileList[i].getFile().getName(), e); } } LOG.info("Pdf files encrypted in " + inputCommand.getOutputFile().getAbsolutePath() + "."); LOG.info("Permissions: " + PdfEncryptor.getPermissionsVerbose(inputCommand.getPermissions()) + "."); } catch (Exception e) { throw new EncryptException(e); } finally { setWorkCompleted(); } } else { throw new ConsoleException(ConsoleException.ERR_BAD_COMMAND); } }
From source file:org.pdfsam.console.business.pdf.handlers.RotateCmdExecutor.java
License:Open Source License
public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException { if ((parsedCommand != null) && (parsedCommand instanceof RotateParsedCommand)) { RotateParsedCommand inputCommand = (RotateParsedCommand) parsedCommand; setPercentageOfWorkDone(0);//from w w w.j a va 2s. c om 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 = PdfUtility.fullReaderFor(fileList[i]); 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(); } } else { throw new ConsoleException(ConsoleException.ERR_BAD_COMMAND); } }
From source file:org.pdfsam.console.business.pdf.handlers.SetViewerCmdExecutor.java
License:Open Source License
public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException { if ((parsedCommand != null) && (parsedCommand instanceof SetViewerParsedCommand)) { SetViewerParsedCommand inputCommand = (SetViewerParsedCommand) parsedCommand; PrefixParser prefixParser;//from ww w. j av a 2s . co m 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 = PdfUtility.readerFor(fileList[i]); 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(); } } else { throw new ConsoleException(ConsoleException.ERR_BAD_COMMAND); } }
From source file:org.pdfsam.console.business.pdf.handlers.SlideShowCmdExecutor.java
License:Open Source License
public void execute(AbstractParsedCommand parsedCommand) throws ConsoleException { if ((parsedCommand != null) && (parsedCommand instanceof SlideShowParsedCommand)) { SlideShowParsedCommand inputCommand = (SlideShowParsedCommand) parsedCommand; PrefixParser prefixParser;/* w w w . j av a 2 s .c o m*/ setPercentageOfWorkDone(0); Transitions transitions = new Transitions(); transitions.setDefaultTransition(inputCommand.getDefaultTransition()); transitions.setTransitions(inputCommand.getTransitions()); transitions = parseXmlInput(inputCommand.getInputXmlFile(), transitions); try { prefixParser = new PrefixParser(inputCommand.getOutputFilesPrefix(), inputCommand.getInputFile().getFile().getName()); File tmpFile = FileUtility.generateTmpFile(inputCommand.getOutputFile()); pdfReader = PdfUtility.readerFor(inputCommand.getInputFile()); 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()); } //creator HashMap meta = pdfReader.getInfo(); meta.put("Creator", ConsoleServicesFacade.CREATOR); //compression setCompressionSettingOnStamper(inputCommand, pdfStamper); pdfStamper.setMoreInfo(meta); //fullscreen if (inputCommand.isFullScreen()) { pdfStamper.setViewerPreferences(PdfWriter.PageModeFullScreen); } //sets transitions if (transitions.getDefaultTransition() == null) { setTransitionsWithoutDefault(transitions); } else { int totalPages = pdfReader.getNumberOfPages(); setTransitionsWithDefault(transitions, totalPages); } pdfStamper.close(); pdfReader.close(); File outFile = new File(inputCommand.getOutputFile(), prefixParser.generateFileName()); FileUtility.renameTemporaryFile(tmpFile, outFile, inputCommand.isOverwrite()); LOG.debug("File " + outFile.getCanonicalPath() + " created."); LOG.info("Slide show options set."); } catch (Exception e) { throw new SlideShowException(e); } finally { setWorkCompleted(); } } else { throw new ConsoleException(ConsoleException.ERR_BAD_COMMAND); } }
From source file:org.sejda.impl.itext.component.PdfStamperHandler.java
License:Apache License
/** * Creates a new instance initializing the inner {@link PdfStamper} instance. * //from w w w . ja v a2 s . c om * @param reader * input reader * @param ouputFile * {@link File} to stamp on * @param version * version for the created stamper, if null the version number is taken from the input {@link PdfReader} * @throws TaskException * in case of error */ public PdfStamperHandler(PdfReader reader, File ouputFile, PdfVersion version) throws TaskException { try { ouputStream = new FileOutputStream(ouputFile); if (version != null) { stamper = new PdfStamper(reader, ouputStream, version.getVersionAsCharacter()); } else { stamper = new PdfStamper(reader, ouputStream, reader.getPdfVersion()); } } catch (DocumentException e) { throw new TaskException("An error occurred opening the PdfStamper.", e); } catch (IOException e) { throw new TaskIOException("An IO error occurred opening the PdfStamper.", e); } }