List of usage examples for com.lowagie.text.pdf PdfNumber PdfNumber
public PdfNumber(float value)
PdfNumber
-object of type real. From source file:classroom.newspaper_b.Newspaper09.java
public static void main(String[] args) { try {/* ww w. jav a2 s. co m*/ PdfReader reader = new PdfReader(NEWSPAPER); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); PdfAnnotation annotation1 = PdfAnnotation.createSquareCircle(stamper.getWriter(), new Rectangle(LLX1, LLY1, URX1, URY1), MESSAGE, true); annotation1.put(PdfName.T, new PdfString("Advertisement 1")); annotation1.put(PdfName.C, new PdfArray(new float[] { 1, 0, 0 })); stamper.addAnnotation(annotation1, 1); PdfAnnotation annotation2 = PdfAnnotation.createText(stamper.getWriter(), new Rectangle(LLX2, LLY2, URX2, URY2), "Advertisement 2", MESSAGE, false, null); annotation2.put(PdfName.NM, new PdfString("ad2")); // the text must be read only, and the annotation set to NOVIEW annotation2.put(PdfName.F, new PdfNumber(PdfAnnotation.FLAGS_READONLY | PdfAnnotation.FLAGS_NOVIEW)); // we create a popup annotation that will define where the rectangle will appear PdfAnnotation popup = PdfAnnotation.createPopup(stamper.getWriter(), new Rectangle(LLX2 + 50, LLY2 + 120, URX2 - 80, URY2 - 120), null, false); // we add a reference to the text annotation to the popup annotation popup.put(PdfName.PARENT, annotation2.getIndirectReference()); // we add a reference to the popup annotation to the text annotation annotation2.put(PdfName.POPUP, popup.getIndirectReference()); // we add both annotations to the writer stamper.addAnnotation(annotation2, 1); stamper.addAnnotation(popup, 1); // the text annotation can't be viewed (it's invisible) // we create a widget annotation named mywidget (it's a button field) PushbuttonField field = new PushbuttonField(stamper.getWriter(), new Rectangle(LLX2, LLY2, URX2, URY2), "button"); PdfAnnotation widget = field.getField(); PdfDictionary dict = new PdfDictionary(); // we write some javascript that makes the popup of the text annotation visible/invisible on mouse enter/exit String js1 = "var t = this.getAnnot(this.pageNum, 'ad2'); t.popupOpen = true; var w = this.getField('button'); w.setFocus();"; PdfAction enter = PdfAction.javaScript(js1, stamper.getWriter()); dict.put(PdfName.E, enter); String js2 = "var t = this.getAnnot(this.pageNum, 'ad2'); t.popupOpen = false;"; PdfAction exit = PdfAction.javaScript(js2, stamper.getWriter()); dict.put(PdfName.X, exit); // we add the javascript as additional action widget.put(PdfName.AA, dict); // we add the button field stamper.addAnnotation(widget, 1); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:jPDFmelange.MelangeJFrame.java
License:Open Source License
/** * Main save method./*from w w w. jav a 2 s . c o m*/ * <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:net.sf.dynamicreports.examples.genericelement.openflashchart.OpenFlashChartPdfHandler.java
License:Open Source License
@Override public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) { try {/*ww w . j a v a2 s . co m*/ PdfWriter writer = exporterContext.getPdfWriter(); PdfIndirectObject swfRef; boolean newContext = !existingContexts.containsKey(exporterContext); if (newContext) { PdfDictionary extensions = new PdfDictionary(); PdfDictionary adobeExtension = new PdfDictionary(); adobeExtension.put(new PdfName("BaseVersion"), PdfWriter.PDF_VERSION_1_7); adobeExtension.put(new PdfName("ExtensionLevel"), new PdfNumber(3)); extensions.put(new PdfName("ADBE"), adobeExtension); writer.getExtraCatalog().put(new PdfName("Extensions"), extensions); byte[] swfData = getChartSwf(); PdfFileSpecification swfFile = PdfFileSpecification.fileEmbedded(writer, null, "Open Flash Chart", swfData); swfRef = writer.addToBody(swfFile); existingContexts.put(exporterContext, swfRef); } else { swfRef = (PdfIndirectObject) existingContexts.get(exporterContext); } Rectangle rect = new Rectangle(element.getX() + exporterContext.getOffsetX(), exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY(), element.getX() + exporterContext.getOffsetX() + element.getWidth(), exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY() - element.getHeight()); PdfAnnotation ann = new PdfAnnotation(writer, rect); ann.put(PdfName.SUBTYPE, new PdfName("RichMedia")); PdfDictionary settings = new PdfDictionary(); PdfDictionary activation = new PdfDictionary(); activation.put(new PdfName("Condition"), new PdfName("PV")); settings.put(new PdfName("Activation"), activation); ann.put(new PdfName("RichMediaSettings"), settings); PdfDictionary content = new PdfDictionary(); HashMap<String, PdfIndirectReference> assets = new HashMap<String, PdfIndirectReference>(); assets.put("map.swf", swfRef.getIndirectReference()); PdfDictionary assetsDictionary = PdfNameTree.writeTree(assets, writer); content.put(new PdfName("Assets"), assetsDictionary); PdfArray configurations = new PdfArray(); PdfDictionary configuration = new PdfDictionary(); PdfArray instances = new PdfArray(); PdfDictionary instance = new PdfDictionary(); instance.put(new PdfName("Subtype"), new PdfName("Flash")); PdfDictionary params = new PdfDictionary(); String chartData = ((ChartGenerator) element .getParameterValue(ChartGenerator.PARAMETER_CHART_GENERATOR)).generateChart(); String vars = "inline_data=" + chartData; params.put(new PdfName("FlashVars"), new PdfString(vars)); instance.put(new PdfName("Params"), params); instance.put(new PdfName("Asset"), swfRef.getIndirectReference()); PdfIndirectObject instanceRef = writer.addToBody(instance); instances.add(instanceRef.getIndirectReference()); configuration.put(new PdfName("Instances"), instances); PdfIndirectObject configurationRef = writer.addToBody(configuration); configurations.add(configurationRef.getIndirectReference()); content.put(new PdfName("Configurations"), configurations); ann.put(new PdfName("RichMediaContent"), content); writer.addAnnotation(ann); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:net.sf.fspdfs.components.ofc.ChartPdfHandler.java
License:Open Source License
public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) { try {//from w w w. j a v a2s. co m PdfWriter writer = exporterContext.getPdfWriter(); PdfIndirectObject swfRef; boolean newContext = !existingContexts.containsKey(exporterContext); if (newContext) { // add the Adobe 1.7 extensions catalog dictionary PdfDictionary extensions = new PdfDictionary(); PdfDictionary adobeExtension = new PdfDictionary(); adobeExtension.put(new PdfName("BaseVersion"), PdfWriter.PDF_VERSION_1_7); adobeExtension.put(new PdfName("ExtensionLevel"), new PdfNumber(3)); extensions.put(new PdfName("ADBE"), adobeExtension); writer.getExtraCatalog().put(new PdfName("Extensions"), extensions); // add the swf file byte[] swfData = readSwf(); PdfFileSpecification swfFile = PdfFileSpecification.fileEmbedded(writer, null, "Open Flash Chart", swfData); swfRef = writer.addToBody(swfFile); existingContexts.put(exporterContext, swfRef); } else { swfRef = (PdfIndirectObject) existingContexts.get(exporterContext); } Rectangle rect = new Rectangle(element.getX() + exporterContext.getOffsetX(), exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY(), element.getX() + exporterContext.getOffsetX() + element.getWidth(), exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY() - element.getHeight()); PdfAnnotation ann = new PdfAnnotation(writer, rect); ann.put(PdfName.SUBTYPE, new PdfName("RichMedia")); PdfDictionary settings = new PdfDictionary(); PdfDictionary activation = new PdfDictionary(); activation.put(new PdfName("Condition"), new PdfName("PV")); settings.put(new PdfName("Activation"), activation); ann.put(new PdfName("RichMediaSettings"), settings); PdfDictionary content = new PdfDictionary(); HashMap assets = new HashMap(); assets.put("map.swf", swfRef.getIndirectReference()); PdfDictionary assetsDictionary = PdfNameTree.writeTree(assets, writer); content.put(new PdfName("Assets"), assetsDictionary); PdfArray configurations = new PdfArray(); PdfDictionary configuration = new PdfDictionary(); PdfArray instances = new PdfArray(); PdfDictionary instance = new PdfDictionary(); instance.put(new PdfName("Subtype"), new PdfName("Flash")); PdfDictionary params = new PdfDictionary(); String chartData = (String) element.getParameterValue(PARAMETER_CHART_DATA); String vars = "inline_data=" + chartData; params.put(new PdfName("FlashVars"), new PdfString(vars)); instance.put(new PdfName("Params"), params); instance.put(new PdfName("Asset"), swfRef.getIndirectReference()); PdfIndirectObject instanceRef = writer.addToBody(instance); instances.add(instanceRef.getIndirectReference()); configuration.put(new PdfName("Instances"), instances); PdfIndirectObject configurationRef = writer.addToBody(configuration); configurations.add(configurationRef.getIndirectReference()); content.put(new PdfName("Configurations"), configurations); ann.put(new PdfName("RichMediaContent"), content); writer.addAnnotation(ann); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:net.sf.jasperreports.components.ofc.ChartPdfHandler.java
License:Open Source License
public void exportElement(JRPdfExporterContext exporterContext, JRGenericPrintElement element) { try {/*from w ww. jav a 2s . c om*/ PdfWriter writer = exporterContext.getPdfWriter(); PdfIndirectObject swfRef; boolean newContext = !existingContexts.containsKey(exporterContext); if (newContext) { // add the Adobe 1.7 extensions catalog dictionary PdfDictionary extensions = new PdfDictionary(); PdfDictionary adobeExtension = new PdfDictionary(); adobeExtension.put(new PdfName("BaseVersion"), PdfWriter.PDF_VERSION_1_7); adobeExtension.put(new PdfName("ExtensionLevel"), new PdfNumber(3)); extensions.put(new PdfName("ADBE"), adobeExtension); writer.getExtraCatalog().put(new PdfName("Extensions"), extensions); // add the swf file byte[] swfData = readSwf(); PdfFileSpecification swfFile = PdfFileSpecification.fileEmbedded(writer, null, "Open Flash Chart", swfData); swfRef = writer.addToBody(swfFile); existingContexts.put(exporterContext, swfRef); } else { swfRef = (PdfIndirectObject) existingContexts.get(exporterContext); } Rectangle rect = new Rectangle(element.getX() + exporterContext.getOffsetX(), exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY(), element.getX() + exporterContext.getOffsetX() + element.getWidth(), exporterContext.getExportedReport().getPageHeight() - element.getY() - exporterContext.getOffsetY() - element.getHeight()); PdfAnnotation ann = new PdfAnnotation(writer, rect); ann.put(PdfName.SUBTYPE, new PdfName("RichMedia")); PdfDictionary settings = new PdfDictionary(); PdfDictionary activation = new PdfDictionary(); activation.put(new PdfName("Condition"), new PdfName("PV")); settings.put(new PdfName("Activation"), activation); ann.put(new PdfName("RichMediaSettings"), settings); PdfDictionary content = new PdfDictionary(); HashMap<String, PdfIndirectReference> assets = new HashMap<String, PdfIndirectReference>(); assets.put("map.swf", swfRef.getIndirectReference()); PdfDictionary assetsDictionary = PdfNameTree.writeTree(assets, writer); content.put(new PdfName("Assets"), assetsDictionary); PdfArray configurations = new PdfArray(); PdfDictionary configuration = new PdfDictionary(); PdfArray instances = new PdfArray(); PdfDictionary instance = new PdfDictionary(); instance.put(new PdfName("Subtype"), new PdfName("Flash")); PdfDictionary params = new PdfDictionary(); String chartData = (String) element.getParameterValue(PARAMETER_CHART_DATA); String vars = "inline_data=" + chartData; params.put(new PdfName("FlashVars"), new PdfString(vars)); instance.put(new PdfName("Params"), params); instance.put(new PdfName("Asset"), swfRef.getIndirectReference()); PdfIndirectObject instanceRef = writer.addToBody(instance); instances.add(instanceRef.getIndirectReference()); configuration.put(new PdfName("Instances"), instances); PdfIndirectObject configurationRef = writer.addToBody(configuration); configurations.add(configurationRef.getIndirectReference()); content.put(new PdfName("Configurations"), configurations); ann.put(new PdfName("RichMediaContent"), content); writer.addAnnotation(ann); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:net.sf.jasperreports.engine.export.JRPdfExporterTagHelper.java
License:Open Source License
protected void createSpanTags(JRPrintElement element, PdfStructureElement parentTag) { int colSpan = 0; int rowSpan = 0; try {//from w w w . j a v a2 s . c o m colSpan = Integer.valueOf(element.getPropertiesMap().getProperty(PROPERTY_TAG_COLSPAN)).intValue(); } catch (NumberFormatException e) { try { colSpan = Integer .valueOf(element.getPropertiesMap().getProperty(JRCellContents.PROPERTY_COLUMN_SPAN)) .intValue(); } catch (NumberFormatException ex) { } } try { rowSpan = Integer.valueOf(element.getPropertiesMap().getProperty(PROPERTY_TAG_ROWSPAN)).intValue(); } catch (NumberFormatException e) { try { rowSpan = Integer.valueOf(element.getPropertiesMap().getProperty(JRCellContents.PROPERTY_ROW_SPAN)) .intValue(); } catch (NumberFormatException ex) { } } if (colSpan > 1 || rowSpan > 1) { PdfArray a = new PdfArray(); PdfDictionary dict = new PdfDictionary(); if (colSpan > 1) { dict.put(new PdfName("ColSpan"), new PdfNumber(colSpan)); } if (rowSpan > 1) { dict.put(new PdfName("RowSpan"), new PdfNumber(rowSpan)); } dict.put(PdfName.O, new PdfName("Table")); a.add(dict); parentTag.put(PdfName.A, a); } }
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 w ww . j ava2 s. 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//w w w .java2 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.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 ww . j av 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()); 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 ww w .j av a 2 s . c om*/ * @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())); } }