List of usage examples for com.lowagie.text.pdf PdfName ROTATE
PdfName ROTATE
To view the source code for com.lowagie.text.pdf PdfName ROTATE.
Click Source Link
From source file:jPDFmelange.MelangeJFrame.java
License:Open Source License
/** * Main save method./*ww w.j a va 2s . c om*/ * <p> * Saves all elements of the main list represented with its {@link MelangeJFrame#listContentMain content} * to the specified file. * * @param fileName name of file to save. * @throws IOException on File IO error. * @throws DocumentException on itext PDF error. */ private void saveFile(String fileName) throws IOException, DocumentException { File file = new File(fileName); File tmpfile = File.createTempFile("Mixer", null, file.getParentFile()); String bakFileName = fileName.substring(0, fileName.lastIndexOf('.')).concat(".bak"); File bakfile = new File(bakFileName); // // prevent writing to a PDF that is blocked by the renderer. // jPanePreview.closePdfFile(); // itext usage System.out.println("Writing new content to <" + tmpfile.getName() + ">"); PdfReader reader = null; PdfDictionary dict = null; Document pdfDoc = new Document(); PdfCopy writer = new PdfCopy(pdfDoc, new FileOutputStream(tmpfile)); pdfDoc.open(); PageNode node = null; for (int i = 0; i < listContentMain.size(); i++) { node = (PageNode) listContentMain.get(i); if (node.password == null) reader = new PdfReader(node.filename); else reader = new PdfReader(node.filename, node.password.getBytes()); dict = reader.getPageN(node.pagenumber); dict.put(PdfName.ROTATE, new PdfNumber(node.rotation)); writer.addPage(writer.getImportedPage(reader, node.pagenumber)); reader.close(); // close input file System.out .println("Page " + node.pagenumber + " File:" + node.filename + " Rotation:" + node.rotation); } // // save page mode and layout preferences // if (jCheckBoxEnablePDFViewerPrefs.isSelected()) { String key = jPanelViewPrefs.getKeyPageMode(); writer.setViewerPreferences(PageMode.get(key)); key = jPanelViewPrefs.getKeyPageLayout(); writer.setViewerPreferences(PageLayout.get(key)); if (jPanelViewPrefs.jCheckBoxHideToolbar.isSelected()) writer.addViewerPreference(PdfName.HIDETOOLBAR, PdfBoolean.PDFTRUE); if (jPanelViewPrefs.jCheckBoxHideMenubar.isSelected()) writer.addViewerPreference(PdfName.HIDEMENUBAR, PdfBoolean.PDFTRUE); if (jPanelViewPrefs.jCheckBoxHideWindowUI.isSelected()) writer.addViewerPreference(PdfName.HIDEWINDOWUI, PdfBoolean.PDFTRUE); if (jPanelViewPrefs.jCheckBoxFitWindow.isSelected()) writer.addViewerPreference(PdfName.FITWINDOW, PdfBoolean.PDFTRUE); if (jPanelViewPrefs.jCheckBoxCenterWindow.isSelected()) writer.addViewerPreference(PdfName.CENTERWINDOW, PdfBoolean.PDFTRUE); if (jPanelViewPrefs.jCheckBoxDisplayDocTitle.isSelected()) writer.addViewerPreference(PdfName.DISPLAYDOCTITLE, PdfBoolean.PDFTRUE); } pdfDoc.close(); // close Helper Class writer.close(); // close output file // save old file to a XXX.bak file if (bakfile.exists()) bakfile.delete(); if (file.renameTo(bakfile.getCanonicalFile())) { System.out.println("Orginal File is saved in <" + bakfile.getName() + ">"); } // move new content to original file name file = new File(fileName); if (tmpfile.renameTo(file)) System.out.println("<" + tmpfile.getName() + "> is copied to <" + file.getName() + "> "); else { JOptionPane.showMessageDialog(MelangeJFrame.this, messages.getString("canNotWriteFile") + file.getName() + messages.getString("trySaveAs"), messages.getString("warning"), JOptionPane.WARNING_MESSAGE); System.out.println( messages.getString("canNotWriteFile") + file.getName() + messages.getString("trySaveAs")); } }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void rotate(int pageCount, PdfPageData currentPageData, RotatePDFPages current_selection) { File tempFile = null;/*from ww w . ja v a2s.co m*/ try { tempFile = File.createTempFile("temp", null); ObjectStore.copy(selectedFile, tempFile.getAbsolutePath()); } catch (Exception e) { return; } try { int[] pgsToRotate = current_selection.getRotatedPages(); if (pgsToRotate == null) return; int check = -1; if (pgsToRotate.length == 1) { check = currentGUI.showConfirmDialog(Messages.getMessage("PdfViewerMessage.ConfirmRotatePages"), Messages.getMessage("PdfViewerMessage.Confirm"), JOptionPane.YES_NO_OPTION); } else { check = currentGUI.showConfirmDialog(Messages.getMessage("PdfViewerMessage.ConfirmRotatePages"), Messages.getMessage("PdfViewerMessage.Confirm"), JOptionPane.YES_NO_OPTION); } if (check != 0) return; if (pgsToRotate == null) return; List pagesToRotate = new ArrayList(); for (int i = 0; i < pgsToRotate.length; i++) pagesToRotate.add(new Integer(pgsToRotate[i])); int direction = current_selection.getDirection(); PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); for (int page = 1; page <= pageCount; page++) { if (pagesToRotate.contains(new Integer(page))) { // int currentRotation = // Integer.parseInt(reader.getPageN(page).get(PdfName.ROTATE).toString()); int currentRotation = currentPageData.getRotation(page); if (direction == ROTATECLOCKWISE) reader.getPageN(page).put(PdfName.ROTATE, new PdfNumber((currentRotation + 90) % 360)); else if (direction == ROTATECOUNTERCLOCKWISE) reader.getPageN(page).put(PdfName.ROTATE, new PdfNumber((currentRotation - 90) % 360)); else if (direction == ROTATE180) reader.getPageN(page).put(PdfName.ROTATE, new PdfNumber((currentRotation + 180) % 360)); else throw new Exception("invalid desired rotation"); } } PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(selectedFile)); stamp.close(); } catch (Exception e) { ObjectStore.copy(tempFile.getAbsolutePath(), selectedFile); e.printStackTrace(); } finally { tempFile.delete(); } }
From source file:org.pdfsam.console.business.pdf.handlers.ConcatCmdExecutor.java
License:Open Source License
/** * Apply pages rotations/*from ww w .j a v a 2 s. c om*/ * * @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.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 ww w. ja v a2 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()); 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.sejda.impl.itext.component.PdfRotator.java
License:Apache License
/** * apply the rotation to the given page if necessary * //from w w w .j a v a 2 s .co m * @param pageNmber */ private void apply(int pageNmber) { if (pages.contains(pageNmber)) { PdfDictionary dictionary = reader.getPageN(pageNmber); dictionary.put(PdfName.ROTATE, new PdfNumber( rotation.addRotation(getRotation(reader.getPageRotation(pageNmber))).getDegrees())); } }