List of usage examples for com.lowagie.text.pdf PdfReader close
public void close()
From source file:it.pdfsam.plugin.merge.GUI.MergeMainGUI.java
License:Open Source License
private void addTableRowsFromNode(Node file_node) { if (file_node != null) { boolean encrypt = false; String num_pages = ""; String page_selection = ""; File file_to_add = null;/*w w w .j av a 2 s.co m*/ try { Node file_name = (Node) file_node.selectSingleNode("@name"); if (file_name != null) { file_to_add = new File(file_name.getText()); } } catch (Exception ex) { file_to_add = null; fireLogPropertyChanged(GettextResource.gettext(i18n_messages, "Error: ") + ex.getMessage(), LogPanel.LOG_ERROR); } try { if (file_to_add != null) { PdfReader pdf_reader = new PdfReader(new RandomAccessFileOrArray(file_to_add.getAbsolutePath()), null); encrypt = pdf_reader.isEncrypted(); // we retrieve the total number of pages num_pages = Integer.toString(pdf_reader.getNumberOfPages()); pdf_reader.close(); } } catch (Exception ex) { num_pages = ex.getMessage(); } try { Node file_pages = (Node) file_node.selectSingleNode("@pageselection"); if (file_pages != null) { page_selection = file_pages.getText(); } else { page_selection = MergeMainGUI.ALL_STRING; } } catch (Exception ex) { page_selection = MergeMainGUI.ALL_STRING; } try { modello_merge_table.addRow(new MergeItemType(file_to_add.getName(), file_to_add.getAbsolutePath(), num_pages, page_selection, encrypt)); fireLogPropertyChanged( GettextResource.gettext(i18n_messages, "File selected: ") + file_to_add.getName(), LogPanel.LOG_INFO); } catch (Exception ex) { fireLogPropertyChanged(GettextResource.gettext(i18n_messages, "Error: ") + ex.getMessage(), LogPanel.LOG_ERROR); } } }
From source file:jPDFmelange.MelangeJFrame.java
License:Open Source License
/** * Main save method.//from ww w .jav a 2s .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:lucee.runtime.tag.PDF.java
License:Open Source License
private void doActionThumbnail() throws PageException, IOException, DocumentException { required("pdf", "thumbnail", "source", source); PDFDocument doc = toPDFDocument(source, password, null); PdfReader pr = doc.getPdfReader(); boolean isEnc = pr.isEncrypted(); pr.close(); if (isEnc) {//from www .j a v a 2 s . c om ByteArrayOutputStream baos = new ByteArrayOutputStream(); //PDFUtil.concat(new PDFDocument[]{doc}, baos, true, true, true, (char)0); PDFUtil.encrypt(doc, baos, null, null, 0, PDFUtil.ENCRYPT_NONE); baos.close(); doc = new PDFDocument(baos.toByteArray(), doc.getResource(), null); } doc.setPages(pages); // scale if (scale < 1) throw new ApplicationException("value of attribute scale [" + scale + "] should be at least 1"); // destination if (destination == null) destination = ResourceUtil.toResourceNotExisting(pageContext, "thumbnails"); // imagePrefix if (imagePrefix == null) { Resource res = doc.getResource(); if (res != null) { String n = res.getName(); int index = n.lastIndexOf('.'); if (index != -1) imagePrefix = n.substring(0, index); else imagePrefix = n; } else imagePrefix = "memory"; } // MUST password PDFUtil.writeImages(doc.getRaw(), doc.getPages(), destination, imagePrefix, format, scale, overwrite, resolution == RESOLUTION_HIGH, transparent); }
From source file:lucee.runtime.tag.PDF.java
License:Open Source License
private void doActionSetInfo() throws PageException, IOException, DocumentException { required("pdf", "setInfo", "info", info); required("pdf", "getInfo", "source", source); PDFDocument doc = toPDFDocument(source, password, null); PdfReader pr = doc.getPdfReader(); OutputStream os = null;//from w ww. j a v a 2s . c om try { if (destination == null) { if (doc.getResource() == null) throw new ApplicationException( "source is not based on a resource, destination file is required"); destination = doc.getResource(); } else if (destination.exists() && !overwrite) throw new ApplicationException("destination file [" + destination + "] already exists"); PdfStamper stamp = new PdfStamper(pr, os = destination.getOutputStream()); HashMap moreInfo = new HashMap(); //Key[] keys = info.keys(); Iterator<Entry<Key, Object>> it = info.entryIterator(); Entry<Key, Object> e; while (it.hasNext()) { e = it.next(); moreInfo.put(StringUtil.ucFirst(e.getKey().getLowerString()), Caster.toString(e.getValue())); } // author Object value = info.get(KeyConstants._author, null); if (value != null) moreInfo.put("Author", Caster.toString(value)); // keywords value = info.get("keywords", null); if (value != null) moreInfo.put("Keywords", Caster.toString(value)); // title value = info.get(KeyConstants._title, null); if (value != null) moreInfo.put("Title", Caster.toString(value)); // subject value = info.get(KeyConstants._subject, null); if (value != null) moreInfo.put("Subject", Caster.toString(value)); // creator value = info.get("creator", null); if (value != null) moreInfo.put("Creator", Caster.toString(value)); // trapped value = info.get("Trapped", null); if (value != null) moreInfo.put("Trapped", Caster.toString(value)); // Created value = info.get("Created", null); if (value != null) moreInfo.put("Created", Caster.toString(value)); // Language value = info.get("Language", null); if (value != null) moreInfo.put("Language", Caster.toString(value)); stamp.setMoreInfo(moreInfo); stamp.close(); } finally { IOUtil.closeEL(os); pr.close(); } }
From source file:lucee.runtime.text.pdf.PDFDocument.java
License:Open Source License
public Struct getInfo() { PdfReader pr = null; try {/*from ww w . j a v a 2 s. co m*/ pr = getPdfReader(); //PdfDictionary catalog = pr.getCatalog(); int permissions = pr.getPermissions(); boolean encrypted = pr.isEncrypted(); Struct info = new StructImpl(); info.setEL("FilePath", getFilePath()); // access info.setEL("ChangingDocument", allowed(encrypted, permissions, PdfWriter.ALLOW_MODIFY_CONTENTS)); info.setEL("Commenting", allowed(encrypted, permissions, PdfWriter.ALLOW_MODIFY_ANNOTATIONS)); info.setEL("ContentExtraction", allowed(encrypted, permissions, PdfWriter.ALLOW_SCREENREADERS)); info.setEL("CopyContent", allowed(encrypted, permissions, PdfWriter.ALLOW_COPY)); info.setEL("DocumentAssembly", allowed(encrypted, permissions, PdfWriter.ALLOW_ASSEMBLY + PdfWriter.ALLOW_MODIFY_CONTENTS)); info.setEL("FillingForm", allowed(encrypted, permissions, PdfWriter.ALLOW_FILL_IN + PdfWriter.ALLOW_MODIFY_ANNOTATIONS)); info.setEL("Printing", allowed(encrypted, permissions, PdfWriter.ALLOW_PRINTING)); info.setEL("Secure", ""); info.setEL("Signing", allowed(encrypted, permissions, PdfWriter.ALLOW_MODIFY_ANNOTATIONS + PdfWriter.ALLOW_MODIFY_CONTENTS + PdfWriter.ALLOW_FILL_IN)); info.setEL("Encryption", encrypted ? "Password Security" : "No Security");// MUST info.setEL("TotalPages", Caster.toDouble(pr.getNumberOfPages())); info.setEL("Version", "1." + pr.getPdfVersion()); info.setEL("permissions", "" + permissions); info.setEL("permiss", "" + PdfWriter.ALLOW_FILL_IN); info.setEL("Application", ""); info.setEL("Author", ""); info.setEL("CenterWindowOnScreen", ""); info.setEL("Created", ""); info.setEL("FitToWindow", ""); info.setEL("HideMenubar", ""); info.setEL("HideToolbar", ""); info.setEL("HideWindowUI", ""); info.setEL("Keywords", ""); info.setEL("Language", ""); info.setEL("Modified", ""); info.setEL("PageLayout", ""); info.setEL("Producer", ""); info.setEL("Properties", ""); info.setEL("ShowDocumentsOption", ""); info.setEL("ShowWindowsOption", ""); info.setEL("Subject", ""); info.setEL("Title", ""); info.setEL("Trapped", ""); // info HashMap imap = pr.getInfo(); Iterator it = imap.entrySet().iterator(); Map.Entry entry; while (it.hasNext()) { entry = (Entry) it.next(); info.setEL(Caster.toString(entry.getKey(), null), entry.getValue()); } return info; } catch (PageException pe) { throw new PageRuntimeException(pe); } finally { if (pr != null) pr.close(); } }
From source file:net.laubenberger.bogatyr.helper.HelperPdf.java
License:Open Source License
/** * Modifies the meta data of given PDF and stores it in a new {@link File}. * <strong>Meta data example:"</strong> metadata.put("Title", "Example"); * metadata.put("Author", "Stefan Laubenberger"); metadata.put("Subject", * "Example PDF meta data"); metadata.put("Keywords", "Example, PDF"); * metadata.put("Creator", "http://www.laubenberger.net/"); * metadata.put("Producer", "Silvan Spross"); * /*from ww w. j a v a 2s.c o m*/ * @param source * {@link File} * @param dest * {@link File} for the modified PDF * @param metadata * list with the new meta data informations * @throws DocumentException * @throws IOException * @since 0.7.0 */ @SuppressWarnings("unchecked") public static void setMetaData(final File source, final File dest, final Map<String, String> metadata) throws IOException, DocumentException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(source, dest, metadata)); if (null == source) { throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$ } if (null == dest) { throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$ } if (HelperObject.isEquals(source, dest)) { throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$ } if (null == metadata) { throw new RuntimeExceptionIsNull("metadata"); //$NON-NLS-1$ } PdfReader reader = null; PdfStamper stamper = null; try { reader = new PdfReader(source.getAbsolutePath()); stamper = new PdfStamper(reader, new FileOutputStream(dest)); final HashMap<String, String> info = reader.getInfo(); info.putAll(metadata); stamper.setMoreInfo(info); } finally { if (null != stamper) { stamper.close(); } if (null != reader) { reader.close(); } } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit()); }
From source file:net.laubenberger.bogatyr.helper.HelperPdf.java
License:Open Source License
/** * Returns the metadata of a given PDF as {@link Map}. * //from w w w . j ava 2 s . c o m * @param file * input as PDF * @return {@link Map} containinfg the metadata of the given PDF * @throws IOException * @see File * @since 0.9.4 */ @SuppressWarnings("unchecked") public static Map<String, String> getMetaData(final File file) throws IOException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(file)); if (null == file) { throw new RuntimeExceptionIsNull("file"); //$NON-NLS-1$ } PdfReader reader = null; Map<String, String> result; try { reader = new PdfReader(file.getAbsolutePath()); result = reader.getInfo(); } finally { if (null != reader) { reader.close(); } } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result)); return result; }
From source file:net.laubenberger.bogatyr.helper.HelperPdf.java
License:Open Source License
/** * Encrypt a PDF with a given user and password. * // ww w .j a v a2 s.c o m * @param source * {@link File} * @param dest * {@link File} for the encrypted PDF * @param user * of the dest {@link File} * @param password * of the dest {@link File} * @throws DocumentException * @throws IOException * @since 0.9.4 */ public static void encrypt(final File source, final File dest, final byte[] user, final byte[] password) throws IOException, DocumentException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(source, dest, user, password)); if (null == source) { throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$ } if (null == dest) { throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$ } if (HelperObject.isEquals(source, dest)) { throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$ } if (null == user) { throw new RuntimeExceptionIsNull("user"); //$NON-NLS-1$ } if (!HelperArray.isValid(user)) { throw new RuntimeExceptionIsEmpty("user"); //$NON-NLS-1$ } if (null == password) { throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$ } if (!HelperArray.isValid(password)) { throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$ } PdfReader reader = null; OutputStream os = null; try { reader = new PdfReader(source.getAbsolutePath()); os = new FileOutputStream(dest); PdfEncryptor.encrypt(reader, os, user, password, PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_DEGRADED_PRINTING | PdfWriter.ALLOW_FILL_IN | PdfWriter.ALLOW_MODIFY_ANNOTATIONS | PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_SCREENREADERS, true); } finally { if (null != os) { os.close(); } if (null != reader) { reader.close(); } } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit()); }
From source file:net.laubenberger.bogatyr.helper.HelperPdf.java
License:Open Source License
/** * Adds a all restrictions to a PDF with a given password. * /*from ww w. j av a2 s .c o m*/ * @param source * {@link File} * @param dest * {@link File} for the locked PDF * @param password * of the dest {@link File} * @throws DocumentException * @throws IOException * @since 0.9.4 */ public static void lock(final File source, final File dest, final byte[] password) throws IOException, DocumentException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(source, dest, password)); if (null == source) { throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$ } if (null == dest) { throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$ } if (HelperObject.isEquals(source, dest)) { throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$ } if (null == password) { throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$ } if (!HelperArray.isValid(password)) { throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$ } PdfReader reader = null; OutputStream os = null; try { reader = new PdfReader(source.getAbsolutePath()); os = new FileOutputStream(dest); PdfEncryptor.encrypt(reader, os, null, password, 0, true); } finally { if (null != os) { os.close(); } if (null != reader) { reader.close(); } } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit()); }
From source file:net.laubenberger.bogatyr.helper.HelperPdf.java
License:Open Source License
/** * Removes all restrictions from a PDF with a given password. * // w w w . j a v a 2 s . c o m * @param source * {@link File} * @param dest * {@link File} for the unlocked PDF * @param password * of the source {@link File} * @throws DocumentException * @throws IOException * @since 0.9.0 */ public static void unlock(final File source, final File dest, final byte[] password) throws IOException, DocumentException { // $JUnit$ if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(source, dest, password)); if (null == source) { throw new RuntimeExceptionIsNull("source"); //$NON-NLS-1$ } if (null == dest) { throw new RuntimeExceptionIsNull("dest"); //$NON-NLS-1$ } if (HelperObject.isEquals(source, dest)) { throw new RuntimeExceptionIsEquals("source", "dest"); //$NON-NLS-1$ //$NON-NLS-2$ } if (null == password) { throw new RuntimeExceptionIsNull("password"); //$NON-NLS-1$ } if (!HelperArray.isValid(password)) { throw new RuntimeExceptionIsEmpty("password"); //$NON-NLS-1$ } PdfReader reader = null; OutputStream os = null; try { reader = new PdfReader(source.getAbsolutePath(), password); os = new FileOutputStream(dest); PdfEncryptor.encrypt(reader, os, null, null, PdfWriter.ALLOW_ASSEMBLY | PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_DEGRADED_PRINTING | PdfWriter.ALLOW_FILL_IN | PdfWriter.ALLOW_MODIFY_ANNOTATIONS | PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_SCREENREADERS, true); } finally { if (null != os) { os.close(); } if (null != reader) { reader.close(); } } if (log.isDebugEnabled()) log.debug(HelperLog.methodExit()); }