List of usage examples for org.apache.pdfbox.pdmodel PDDocument isEncrypted
public boolean isEncrypted()
From source file:fr.paris.lutece.plugins.lucene.service.indexer.PdfFileIndexer.java
License:Open Source License
/** * /*from ww w .j a va2 s. c om*/ * {@inheritDoc} */ public String getContentToIndex(InputStream is) { String strContent = ""; PDDocument pdfDocument = null; try { pdfDocument = PDDocument.load(is); if (pdfDocument.isEncrypted()) { pdfDocument.decrypt(""); } StringWriter writer = new StringWriter(); PDFTextStripper stripper = new PDFTextStripper(); stripper.writeText(pdfDocument, writer); strContent = writer.getBuffer().toString(); } catch (CryptographyException e) { _log.error(e.getMessage(), e); } catch (IOException e) { _log.error(e.getMessage(), e); } catch (InvalidPasswordException e) { _log.error(e.getMessage(), e); } finally { if (pdfDocument != null) { try { pdfDocument.close(); } catch (IOException e) { _log.error(e.getMessage(), e); } } } return strContent; }
From source file:function.PrintImageLocations.java
License:Apache License
/** * This will print the documents data./*from w ww .ja v a 2s. co m*/ * * @param args The command line arguments. * * @throws Exception If there is an error parsing the document. */ public static void main(String[] args) throws Exception { PDDocument document = null; try { document = PDDocument.load(new File("C:/Users/ATUL/Desktop/Page-layout/output1.pdf")); if (document.isEncrypted()) { document.decrypt(""); } PrintImageLocations printer = new PrintImageLocations(); List allPages = document.getDocumentCatalog().getAllPages(); for (int i = 0; i < allPages.size(); i++) { System.out.println("\n***********************************************************"); PDPage page = (PDPage) allPages.get(i); System.out.println("Processing page: " + (i + 1)); printer.processStream(page, page.findResources(), page.getContents().getStream()); } } finally { if (document != null) { document.close(); } } }
From source file:gui.dialog.PlainTextDialog.java
License:Apache License
/** * @Method: pdfParser//from w ww . j a v a2s . c om * * input : File * output: String * * Diese Methode liet den Text aus der Pdf Datei aus und gibt den Text als String zurck */ private String pdfParser(File pdfFile) { NDC.push("pdfParser"); PDDocument document = null; try { document = PDDocument.load(pdfFile); } catch (IOException e) { logger.error("Could not load document", e); NDC.pop(); return null; } if (document.isEncrypted()) { NDC.pop(); return "Encrypted documents are not supported"; } PDFTextStripper stripper; try { stripper = new PDFTextStripper(); } catch (IOException e) { logger.error("Could not create stripper", e); NDC.pop(); return null; } stripper.setStartPage(1); stripper.setEndPage(2); String text; try { text = stripper.getText(document); } catch (Exception e) { logger.error("Could not parse PDF", e); NDC.pop(); return null; } NDC.pop(); return text; }
From source file:khoji.PDFdoc.java
License:Apache License
/** * * @param doc_path/*from w ww . java 2 s . c o m*/ * @return * @throws Exception */ public String extractPDF(String doc_path) throws Exception { PDDocument document = null; try { document = PDDocument.load(doc_path); if (document.isEncrypted()) { try { document.decrypt(""); } catch (InvalidPasswordException e) { System.err.println("Error: Document is encrypted with a password."); System.exit(1); } } PDFTextStripper stripper = new PDFTextStripper(); String text = ""; text = stripper.getText(document); // System.out.println("text:"+text); return text; } finally { if (document != null) { document.close(); } } }
From source file:merge_split.MergeSplit.java
License:Apache License
private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed String fileName;//from ww w. j a v a 2 s . c o m int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource()); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = jFileChooser1.getSelectedFile(); fileName = file.toString(); PDDocument doc = null; String code = ""; try { doc = PDDocument.load(file); if (doc.isEncrypted()) { doc.setAllSecurityToBeRemoved(true); } } catch (IOException ex) { } if (doc == null) { JFrame frame = new JFrame("Input Dialog Example 3"); code = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted", JOptionPane.WARNING_MESSAGE); try { doc = PDDocument.load(file, code); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Wrong Password.", "Wrong Password", JOptionPane.WARNING_MESSAGE); } } if (doc != null) { int count = doc.getNumberOfPages(); String currentpages; if (count > 1) { currentpages = "1 - " + count; } else { currentpages = "1"; } boolean isOriginalDocEncrypted = doc.isEncrypted(); String column4; if (isOriginalDocEncrypted) { column4 = code; } else { column4 = "ok"; } dtm.addRow(new Object[] { fileName, count, currentpages, column4 }); try { doc.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Problem accessing file.", "Problem accessing file", JOptionPane.WARNING_MESSAGE); } arr.add(file); } } else { System.out.println("File access cancelled by user."); } }
From source file:merge_split.MergeSplit.java
License:Apache License
private void RotateFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RotateFileButtonActionPerformed String fileName;//w ww . ja v a2 s . co m int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource()); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = jFileChooser1.getSelectedFile(); fileName = file.toString(); PDDocument doc = null; try { doc = PDDocument.load(file); if (doc.isEncrypted()) { doc.setAllSecurityToBeRemoved(true); } } catch (IOException ex) { } rotatecode = ""; if (doc == null) { JFrame frame = new JFrame("Input Dialog Example 3"); rotatecode = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted", JOptionPane.WARNING_MESSAGE); try { doc = PDDocument.load(file, rotatecode); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Wrong Password.", "Wrong Password", JOptionPane.WARNING_MESSAGE); } } if (doc != null) { int count = doc.getNumberOfPages(); String currentpages; if (count > 1) { currentpages = "1 - " + count; } else { currentpages = "1"; } RotatePagesField.setText(currentpages); RotateFileField.setText(fileName); String name = file.getName(); int pos = name.lastIndexOf("."); if (pos > 0) { name = name.substring(0, pos); } name = name + "Rotated"; RotateNameField.setText(name); try { doc.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Problem finishing process.", "Problem finishing process", JOptionPane.WARNING_MESSAGE); } } } else { System.out.println("File access cancelled by user."); } }
From source file:merge_split.MergeSplit.java
License:Apache License
private void ConvertFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ConvertFileButtonActionPerformed String fileName;/* www. j a v a2 s.c om*/ int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource()); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = jFileChooser1.getSelectedFile(); fileName = file.toString(); PDDocument doc = null; try { doc = PDDocument.load(file); if (doc.isEncrypted()) { doc.setAllSecurityToBeRemoved(true); } } catch (IOException ex) { } convertcode = ""; if (doc == null) { JFrame frame = new JFrame("Input Dialog Example 3"); convertcode = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted", JOptionPane.WARNING_MESSAGE); try { doc = PDDocument.load(file, rotatecode); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Wrong Password.", "Wrong Password", JOptionPane.WARNING_MESSAGE); } } if (doc != null) { int count = doc.getNumberOfPages(); String currentpages; if (count > 1) { currentpages = "1 - " + count; } else { currentpages = "1"; } ConvertPagesField.setText(currentpages); ConvertFileField.setText(fileName); String name = file.getName(); int pos = name.lastIndexOf("."); if (pos > 0) { name = name.substring(0, pos); } ConvertNameField.setText(name); try { doc.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Problem accessing file.", "Problem accessing file", JOptionPane.WARNING_MESSAGE); } } } else { System.out.println("File access cancelled by user."); } }
From source file:merge_split.MergeSplit.java
License:Apache License
private void SplitFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SplitFileButtonActionPerformed String fileName;//from w w w . j a v a 2 s . c o m int returnVal = jFileChooser1.showOpenDialog((Component) evt.getSource()); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = jFileChooser1.getSelectedFile(); fileName = file.toString(); PDDocument doc = null; try { doc = PDDocument.load(file); if (doc.isEncrypted()) { doc.setAllSecurityToBeRemoved(true); } } catch (IOException ex) { } splitcode = ""; if (doc == null) { JFrame frame = new JFrame("Input Dialog Example 3"); splitcode = JOptionPane.showInputDialog(frame, "Enter password", "PDF is encrypted", JOptionPane.WARNING_MESSAGE); try { doc = PDDocument.load(file, rotatecode); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Wrong Password.", "Wrong Password", JOptionPane.WARNING_MESSAGE); } } if (doc != null) { int count = doc.getNumberOfPages(); String currentpages; if (count > 1) { currentpages = "1 - " + count; } else { currentpages = "1"; } SplitPagesField.setText(currentpages); SplitFileField.setText(fileName); String name = file.getName(); int pos = name.lastIndexOf("."); if (pos > 0) { name = name.substring(0, pos); } name = name + "Split"; SplitNameField.setText(name); try { doc.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Problem finishing process.", "Problem finishing process", JOptionPane.WARNING_MESSAGE); } } } else { System.out.println("File access cancelled by user."); } }
From source file:mj.ocraptor.extraction.tika.parser.pdf.PDFParser.java
License:Apache License
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { PDDocument pdfDocument = null; TemporaryResources tmp = new TemporaryResources(); // config from context, or default if not set via context PDFParserConfig localConfig = context.get(PDFParserConfig.class, defaultConfig); try {//from w w w . ja v a2 s . c om // PDFBox can process entirely in memory, or can use a temp file // for unpacked / processed resources // Decide which to do based on if we're reading from a file or not // already TikaInputStream tstream = TikaInputStream.cast(stream); if (tstream != null && tstream.hasFile()) { // File based, take that as a cue to use a temporary file RandomAccess scratchFile = new RandomAccessFile(tmp.createTemporaryFile(), "rw"); if (localConfig.getUseNonSequentialParser() == true) { pdfDocument = PDDocument.loadNonSeq(new CloseShieldInputStream(stream), scratchFile); } else { pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), scratchFile, true); } } else { // Go for the normal, stream based in-memory parsing if (localConfig.getUseNonSequentialParser() == true) { pdfDocument = PDDocument.loadNonSeq(new CloseShieldInputStream(stream), new RandomAccessBuffer()); } else { pdfDocument = PDDocument.load(new CloseShieldInputStream(stream), true); } } if (pdfDocument.isEncrypted()) { String password = null; // Did they supply a new style Password Provider? PasswordProvider passwordProvider = context.get(PasswordProvider.class); if (passwordProvider != null) { password = passwordProvider.getPassword(metadata); } // Fall back on the old style metadata if set if (password == null && metadata.get(PASSWORD) != null) { password = metadata.get(PASSWORD); } // If no password is given, use an empty string as the default if (password == null) { password = ""; } try { pdfDocument.decrypt(password); } catch (Exception e) { // Ignore } } metadata.set(Metadata.CONTENT_TYPE, "application/pdf"); extractMetadata(pdfDocument, metadata); PDF2XHTML.process(pdfDocument, handler, context, metadata, localConfig); } catch (Exception e) { // TODO: logging e.printStackTrace(); } finally { if (pdfDocument != null) { pdfDocument.close(); } if (tmp != null) { tmp.dispose(); tmp.close(); } } handler.endDocument(); }
From source file:net.dstserbak.dataindexer.tokenizer.PDFTokenizer.java
/** * Reads all pages of the PDF file and splits text to words, which are * returned as TokensMap object./* w w w . j a v a2 s .c om*/ * @param pd PDDocument object that is created from stream * @return Map that contains tokens, which are belong to PDF document * @throws IOException If an I/O error occurs */ private static TokensMap tokenizeInput(PDDocument pd) throws IOException { int numberOfPages = pd.getNumberOfPages(); if (pd.isEncrypted()) { log.log(Level.SEVERE, "PDF is ecrypted"); return null; } else if (numberOfPages < 1) { log.log(Level.SEVERE, "PDF number of pages is less than 1"); return null; } PDFTextStripper stripper = new PDFTextStripper(); stripper.setStartPage(1); stripper.setEndPage(numberOfPages); StringTokenizer st = new StringTokenizer(stripper.getText(pd)); TokensMap tokensMap = new TokensMap(); TokenizerUtils.addTokensToMap(tokensMap, st); return tokensMap; }