List of usage examples for org.apache.pdfbox.pdmodel PDDocument close
@Override public void close() throws IOException
From source file:net.sf.jabref.PdfPreviewPanel.java
License:Open Source License
private void renderPDFFile(File file) { InputStream input;/*from w ww . ja v a2s . c o m*/ try { input = new FileInputStream(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); return; } PDDocument document; try { document = PDDocument.load(input); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return; } @SuppressWarnings("unchecked") List<PDPage> pages = document.getDocumentCatalog().getAllPages(); PDPage page = pages.get(0); BufferedImage image; try { image = page.convertToImage(); } catch (Exception e1) { // silently ignores all rendering exceptions image = null; } if (image != null) { int width = this.getParent().getWidth(); int height = this.getParent().getHeight(); BufferedImage resImage = resizeImage(image, width, height, BufferedImage.TYPE_INT_RGB); ImageIcon icon = new ImageIcon(resImage); picLabel.setText(null); picLabel.setIcon(icon); } else { clearPreview(); } try { document.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:net.sf.jabref.plugins.pdftasks.PDFTaskSidePane.java
License:Open Source License
private void doTasks() { // PDF file type representation final ExternalFileType pdf_type = Globals.prefs.getExternalFileTypeByExt("pdf"); // get Bibtex database associated with the current tab final BasePanel db_panel = frame.basePanel(); final BibtexDatabase db = db_panel.database(); final MetaData db_meta = db_panel.metaData(); // get selected Bibtex entries from the current tab final BibtexEntry[] db_entries = db_panel.getSelectedEntries(); // get Bibtex database file for current tab final File db_file = db_panel.getFile(); if (db_file == null || db_file.getParentFile() == null) { JOptionPane.showMessageDialog(frame, "Bibtex database must be saved before performing PDF tasks.", title, JOptionPane.INFORMATION_MESSAGE); return;/* w w w . ja v a 2s. c o m*/ } // get array of directories that PDF files could possibly be in final List<File> db_dirs = new LinkedList<File>(); for (String dir : db_meta.getFileDirectory(GUIGlobals.FILE_FIELD)) { db_dirs.add(new File(dir)); } if (db_dirs.size() == 0 || !db_dirs.contains(db_file.getParentFile())) { db_dirs.add(db_file.getParentFile()); } // return if no entries are selected if (db_entries.length == 0) { JOptionPane.showMessageDialog(frame, "No entries selected for PDF tasks.", title, JOptionPane.INFORMATION_MESSAGE); return; } // get PDF file directory final File pdf_dir = absoluteFile(pdf_dir_txt.getText(), db_file.getParentFile()); // do tasks encapsulated in a worker-thread class AbstractWorker tasks = new AbstractWorker() { boolean cancelled = false; boolean confirmed = false; boolean erred = false; // get user confirmation for PDF file modifications private boolean getUserConfirmation() { if (!confirmed) { confirmed = JOptionPane.showConfirmDialog(frame, "Are you sure you want to rename, move, and/or modify PDF files?\n" + "This operations cannot be undone.", title, JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION; } return confirmed; } public void init() { // block main window frame.block(); } public void run() { // for debugging purposes final boolean modifyDatabase = true; // iterate over selected Bibtex entries int entry_count = 0; for (BibtexEntry entry : db_entries) { ++entry_count; // get Bibtex key, check is not null String key = entry.getCiteKey(); if (key == null || key.length() == 0) { JOptionPane.showMessageDialog(frame, "BibTeX entry '" + entry.getId() + "' does not have a key!", title, JOptionPane.ERROR_MESSAGE); erred = true; return; } // update status bar frame.output(String.format("Processing BibTeX entry: %s (%d of %d)...", key, entry_count, db_entries.length)); // get table of file links for this Bibtex entry FileListTableModel files = new FileListTableModel(); files.setContent(entry.getField(GUIGlobals.FILE_FIELD)); for (int fileindex = 0; fileindex < files.getRowCount(); ++fileindex) { FileListEntry file_entry = files.getEntry(fileindex); // skip if this is not a PDF file link if (!file_entry.getType().equals(pdf_type)) continue; // get PDF file File pdf_file = null; for (File db_dir : db_dirs) { pdf_file = absoluteFile(file_entry.getLink(), db_dir); if (pdf_file.isFile()) { break; } pdf_file = null; } if (pdf_file == null) { String errmsg = "Could not find PDF file '" + file_entry.getLink() + "' in '" + db_dirs.get(0); for (int i = 1; i < db_dirs.size(); ++i) { errmsg += "', '" + db_dirs.get(i); } errmsg += "'!"; JOptionPane.showMessageDialog(frame, errmsg, title, JOptionPane.ERROR_MESSAGE); erred = true; return; } // get PDF file description String pdf_desc = file_entry.getDescription(); // new PDF file File new_pdf_file = pdf_file; // rename PDF file if (rename_pdfs_chk.isSelected()) { // build new PDF name String new_name = key; if (!pdf_desc.isEmpty()) { new_name += "_" + pdf_desc.replace(" ", "_"); } new_name += "." + pdf_type.getExtension(); // set new PDF file new_pdf_file = absoluteFile(new_name, new_pdf_file.getParentFile()); } // move PDF file if (move_to_pdf_dir_chk.isSelected()) { new_pdf_file = absoluteFile(new_pdf_file.getName(), pdf_dir); } // if PDF file needs to be moved if (!new_pdf_file.equals(pdf_file)) { // get user confirmation if (!getUserConfirmation()) { cancelled = true; return; } // perform move/rename operations if (modifyDatabase) { String errmsg = ""; try { // create parent directories File new_pdf_dir = new_pdf_file.getParentFile(); if (new_pdf_dir != null && !new_pdf_dir.isDirectory()) { errmsg = "Could not create directory '" + new_pdf_file.getParentFile().getPath() + "'"; erred = !new_pdf_file.getParentFile().mkdirs(); } if (!erred) { // check if PDF file already exists, and ask for confirmation to replace it if (new_pdf_file.isFile()) { switch (JOptionPane.showConfirmDialog(frame, "PDF file '" + new_pdf_file.getPath() + "' already exists.\n" + "Are you sure you want to replace it with " + "PDF file '" + pdf_file.getPath() + "'?\n" + "This operation cannot be undone.", title, JOptionPane.YES_NO_CANCEL_OPTION)) { case JOptionPane.NO_OPTION: continue; case JOptionPane.CANCEL_OPTION: cancelled = true; return; case JOptionPane.YES_OPTION: errmsg = "Could not delete PDF file '" + new_pdf_file.getPath() + "'"; erred = !new_pdf_file.delete(); } } // otherwise test that we can create the new PDF file else { errmsg = "Could not access PDF file '" + new_pdf_file.getPath() + "'"; erred = !new_pdf_file.createNewFile() || !new_pdf_file.delete(); } if (!erred) { // try to move/rename PDF file errmsg = "Could not rename PDF file '" + pdf_file.getPath() + "' to '" + new_pdf_file.getPath() + "'"; erred = !pdf_file.renameTo(new_pdf_file); } } } // possible exceptions catch (SecurityException e) { erred = true; errmsg += ": insufficient permissions"; } catch (IOException e) { e.printStackTrace(); erred = true; errmsg += ": an I/O exception occurred"; } if (erred) { JOptionPane.showMessageDialog(frame, errmsg + ".", title, JOptionPane.ERROR_MESSAGE); return; } // everything was successful pdf_file = new_pdf_file; } } // update file entry table and Bibtex entry file_entry.setLink(relativePath(pdf_file, db_dirs.get(0))); if (modifyDatabase) { String new_files = files.getStringRepresentation(); if (!new_files.equals(entry.getField(GUIGlobals.FILE_FIELD))) { entry.setField(GUIGlobals.FILE_FIELD, new_files); db_panel.markNonUndoableBaseChanged(); } } // perform operations on PDF file contents if (write_pdf_docinfo_chk.isSelected()) { if (erase_pdf_docinfo_chk.isSelected()) { // get user confirmation if (!getUserConfirmation()) { cancelled = true; return; } // open PDF file PDDocument document = null; try { document = PDDocument.load(pdf_file); } catch (IOException e) { e.printStackTrace(); erred = true; JOptionPane.showMessageDialog(frame, "Could not open PDF file '" + pdf_file.getPath() + "': an I/O exception occurred.", title, JOptionPane.ERROR_MESSAGE); return; } // erase document information document.setDocumentInformation(new PDDocumentInformation()); // erase XML metadata document.getDocumentCatalog().setMetadata(null); // save and close PDF file try { document.save(pdf_file.getPath()); document.close(); } catch (COSVisitorException e) { e.printStackTrace(); erred = true; JOptionPane.showMessageDialog(frame, "Could not save PDF file '" + pdf_file.getPath() + "': an exception occurred.", title, JOptionPane.ERROR_MESSAGE); return; } catch (IOException e) { e.printStackTrace(); erred = true; JOptionPane.showMessageDialog(frame, "Could not save/close PDF file '" + pdf_file.getPath() + "': an I/O exception occurred.", title, JOptionPane.ERROR_MESSAGE); return; } } // write XMP / PDF document catalog metadata try { XMPUtil.writeXMP(pdf_file, entry, db); } catch (IOException e) { e.printStackTrace(); erred = true; JOptionPane.showMessageDialog(frame, "Could not write XMP to PDF file '" + pdf_file.getPath() + "': an I/O exception occurred.", title, JOptionPane.ERROR_MESSAGE); return; } catch (TransformerException e) { e.printStackTrace(); erred = true; JOptionPane .showMessageDialog(frame, "Could not write XMP to PDF file '" + pdf_file.getPath() + "': an exception occurred.", title, JOptionPane.ERROR_MESSAGE); return; } } } } } public void update() { // unblock main window frame.unblock(); // print to status bar if (erred) { frame.output("An error occurred during PDF Tasks"); } else if (cancelled) { frame.output("Cancelled PDF Tasks"); } else { frame.output("Completed PDF Tasks"); } } }; // run task thread (based on code in BasePanel.runCommand()) try { tasks.init(); tasks.getWorker().run(); tasks.getCallBack().update(); } catch (Throwable e) { frame.unblock(); e.printStackTrace(); } }
From source file:net.sf.jabref.util.XMPUtil.java
License:Open Source License
/** * Try to read the given BibTexEntry from the XMP-stream of the given * inputstream containing a PDF-file.// www. ja v a 2 s.c om * * @param inputStream * The inputstream to read from. * * @throws IOException * Throws an IOException if the file cannot be read, so the user * than remove a lock or cancel the operation. */ @SuppressWarnings("unchecked") public static List<BibtexEntry> readXMP(InputStream inputStream) throws IOException { List<BibtexEntry> result = new LinkedList<BibtexEntry>(); PDDocument document = null; try { document = PDDocument.load(inputStream); if (document.isEncrypted()) { throw new EncryptionNotSupportedException("Error: Cannot read metadata from encrypted document."); } XMPMetadata meta = XMPUtil.getXMPMetadata(document); // If we did not find any XMP metadata, search for non XMP metadata if (meta != null) { List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE); for (XMPSchema schema : schemas) { XMPSchemaBibtex bib = (XMPSchemaBibtex) schema; result.add(bib.getBibtexEntry()); } // If we did not find anything have a look if a Dublin Core exists if (result.isEmpty()) { schemas = meta.getSchemasByNamespaceURI(XMPSchemaDublinCore.NAMESPACE); for (XMPSchema schema : schemas) { XMPSchemaDublinCore dc = (XMPSchemaDublinCore) schema; BibtexEntry entry = XMPUtil.getBibtexEntryFromDublinCore(dc); if (entry != null) { result.add(entry); } } } } if (result.isEmpty()) { BibtexEntry entry = XMPUtil .getBibtexEntryFromDocumentInformation(document.getDocumentInformation()); if (entry != null) { result.add(entry); } } } finally { if (document != null) { document.close(); } } // return null, if no metadata was found if (result.isEmpty()) { return null; } return result; }
From source file:net.sf.jabref.util.XMPUtil.java
License:Open Source License
/** * Will read the XMPMetadata from the given pdf file, closing the file * afterwards./*w w w . j a va2 s . co m*/ * * @param inputStream * The inputStream representing a PDF-file to read the * XMPMetadata from. * @return The XMPMetadata object found in the file or null if none is * found. * @throws IOException */ private static XMPMetadata readRawXMP(InputStream inputStream) throws IOException { PDDocument document = null; try { document = PDDocument.load(inputStream); if (document.isEncrypted()) { throw new EncryptionNotSupportedException("Error: Cannot read metadata from encrypted document."); } return XMPUtil.getXMPMetadata(document); } finally { if (document != null) { document.close(); } } }
From source file:net.sf.jabref.util.XMPUtil.java
License:Open Source License
/** * Try to write the given BibTexEntry in the XMP-stream of the given * PDF-file./*from www . j av a2 s.c o m*/ * * Throws an IOException if the file cannot be read or written, so the user * can remove a lock or cancel the operation. * * The method will overwrite existing BibTeX-XMP-data, but keep other * existing metadata. * * @param file * The file to write the entries to. * @param bibtexEntries * The entries to write to the file. * * @param database * maybenull An optional database which the given bibtex entries * belong to, which will be used to resolve strings. If the * database is null the strings will not be resolved. * @param writePDFInfo * Write information also in PDF document properties * @throws TransformerException * If the entry was malformed or unsupported. * @throws IOException * If the file could not be written to or could not be found. */ @SuppressWarnings("unchecked") public static void writeXMP(File file, Collection<BibtexEntry> bibtexEntries, BibtexDatabase database, boolean writePDFInfo) throws IOException, TransformerException { if (database != null) { bibtexEntries = database.resolveForStrings(bibtexEntries, false); } PDDocument document = null; try { document = PDDocument.load(file.getAbsoluteFile()); if (document.isEncrypted()) { throw new EncryptionNotSupportedException("Error: Cannot add metadata to encrypted document."); } if (writePDFInfo && (bibtexEntries.size() == 1)) { XMPUtil.writeDocumentInformation(document, bibtexEntries.iterator().next(), null); XMPUtil.writeDublinCore(document, bibtexEntries, null); } PDDocumentCatalog catalog = document.getDocumentCatalog(); PDMetadata metaRaw = catalog.getMetadata(); XMPMetadata meta; if (metaRaw != null) { meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream())); } else { meta = new XMPMetadata(); } meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.class); // Remove all current Bibtex-schemas List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE); for (XMPSchema schema : schemas) { XMPSchemaBibtex bib = (XMPSchemaBibtex) schema; bib.getElement().getParentNode().removeChild(bib.getElement()); } for (BibtexEntry e : bibtexEntries) { XMPSchemaBibtex bibtex = new XMPSchemaBibtex(meta); meta.addSchema(bibtex); bibtex.setBibtexEntry(e, null); } // Save to stream and then input that stream to the PDF ByteArrayOutputStream os = new ByteArrayOutputStream(); meta.save(os); ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); PDMetadata metadataStream = new PDMetadata(document, is, false); catalog.setMetadata(metadataStream); // Save try { document.save(file.getAbsolutePath()); } catch (COSVisitorException e) { throw new TransformerException("Could not write XMP-metadata: " + e.getLocalizedMessage()); } } finally { if (document != null) { document.close(); } } }
From source file:net.sf.jsignpdf.preview.Pdf2Image.java
License:Mozilla Public License
/** * Returns image (or null if failed) generated from given page in PDF using * PDFBox tool./*from www . ja va 2s. co m*/ * * @param aPage * page in PDF (1 based) * @return image or null */ public BufferedImage getImageUsingPdfBox(final int aPage) { BufferedImage tmpResult = null; PDDocument tmpDoc = null; try { tmpDoc = PDDocument.load(options.getInFile()); if (tmpDoc.isEncrypted()) { tmpDoc.decrypt(options.getPdfOwnerPwdStrX()); } int resolution; try { resolution = Toolkit.getDefaultToolkit().getScreenResolution(); } catch (HeadlessException e) { resolution = 96; } final PDPage page = (PDPage) tmpDoc.getDocumentCatalog().getAllPages().get(aPage - 1); tmpResult = page.convertToImage(BufferedImage.TYPE_INT_RGB, resolution); } catch (Exception e) { e.printStackTrace(); } finally { if (tmpDoc != null) { try { tmpDoc.close(); } catch (Exception e) { e.printStackTrace(); } } } return tmpResult; }
From source file:net.sf.mmm.content.parser.impl.pdf.ContentParserPdf.java
License:Apache License
/** * {@inheritDoc}//ww w .ja va2 s .c om */ @Override public void parse(InputStream inputStream, long filesize, ContentParserOptions options, MutableGenericContext context) throws Exception { PDFParser parser = new PDFParser(inputStream); parser.parse(); PDDocument pdfDoc = parser.getPDDocument(); try { if (pdfDoc.isEncrypted()) { // pdfDoc.decrypt("password"); return; } PDDocumentInformation info = pdfDoc.getDocumentInformation(); String title = info.getTitle(); if (title != null) { context.setVariable(VARIABLE_NAME_TITLE, title); } String keywords = info.getKeywords(); if (keywords != null) { context.setVariable(VARIABLE_NAME_KEYWORDS, keywords); } String author = info.getAuthor(); if (author != null) { context.setVariable(VARIABLE_NAME_CREATOR, author); } if (filesize < options.getMaximumBufferSize()) { PDFTextStripper stripper = new PDFTextStripper(); context.setVariable(VARIABLE_NAME_TEXT, stripper.getText(pdfDoc)); } } finally { pdfDoc.close(); } }
From source file:net.sourceforge.docfetcher.model.parse.PdfParser.java
License:Open Source License
static void close(@Nullable PDDocument doc) { if (doc != null) { try {//from w w w. j av a 2 s.c o m doc.close(); } catch (IOException e) { } } }
From source file:net.sourceforge.docfetcher.parse.PDFParser.java
License:Open Source License
public String renderText(File file) throws ParseException { PDDocument pdfDoc = null; try {/*from w ww.j av a 2 s.co m*/ pdfDoc = PDDocument.load(file); if (pdfDoc.isEncrypted()) { try { pdfDoc.openProtection(new StandardDecryptionMaterial("")); } catch (Exception e) { throw new ParseException(file, Msg.no_extraction_permission.value()); } } PDFTextStripper stripper = new PDFTextStripper(); StringWriter writer = new StringWriter(); stripper.writeText(pdfDoc, writer); return writer.toString(); } catch (IOException e) { throw new ParseException(file, Msg.file_not_readable.value()); } finally { if (pdfDoc != null) { try { pdfDoc.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:net.sourceforge.docfetcher.parse.PDFParser.java
License:Open Source License
public Document parse(File file) throws ParseException { PDDocument pdfDoc = null; try {// w w w . j a v a 2 s . co m // Check if PDF file is encrypted pdfDoc = PDDocument.load(file); if (pdfDoc.isEncrypted()) { try { pdfDoc.openProtection(new StandardDecryptionMaterial("")); } catch (Exception e) { throw new ParseException(file, Msg.no_extraction_permission.value()); } } // Get tags and contents PDFTextStripper stripper = new PDFTextStripper(); StringWriter writer = new StringWriter(); stripper.writeText(pdfDoc, writer); DocFetcher.getInstance().setExceptionHandlerEnabled(true); PDDocumentInformation pdInfo = pdfDoc.getDocumentInformation(); String[] metaData = new String[] { pdInfo.getTitle(), pdInfo.getAuthor(), pdInfo.getSubject(), pdInfo.getKeywords(), }; for (String field : metaData) if (field != null) writer.append(" ").append(field); //$NON-NLS-1$ return new Document(file, metaData[0], writer.getBuffer()).addAuthor(metaData[1]); } catch (IOException e) { throw new ParseException(file, Msg.file_not_readable.value()); } finally { if (pdfDoc != null) { try { pdfDoc.close(); } catch (IOException e) { e.printStackTrace(); } } } }