Boa noite galera;
Eu estava procurando um exemplo que pudesse me ajudar a converter um arquivo pdf para texto. Nos post que encontrei aqui no forum eles não respondem as minhas dúvidas que esta basicamente no componente RandomAccessRead que ele me solicita quando eu vou utilizar o metodo parse. Vou postar o código que estou tentando implementar e o erro que estou obtendo. Se puderem me ajudar agradeço muito:
Erro:
Exception in thread “main” java.lang.ClassCastException: java.io.FileInputStream cannot be cast to org.apache.pdfbox.io.RandomAccessRead
at com.blogspot.computergodzilla.parsers.PdfFileParser.PdfFileParser(PdfFileParser.java:34)
at com.blogspot.computergodzilla.parsers.PdfFileParser.main(PdfFileParser.java:52)
/home/wirlei/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)
Código:
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package com.blogspot.computergodzilla.parsers;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.io.RandomAccessRead;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
/**
-
This class parses the pdf file.
-
i.e this class returns the text from the pdf file.
-
@author Mubin Shrestha
/
public class PdfFileParser {
/*- This method returns the pdf content in text form.
- @param pdffilePath : pdf file path of which you want to parse text
- @return : texts from pdf file
- @throws FileNotFoundException
-
@throws IOException
*/
public String PdfFileParser(String pdffilePath) throws FileNotFoundException, IOException
{
String content;
FileInputStream fi = new FileInputStream(new File(pdffilePath));
PDFParser parser = new PDFParser((RandomAccessRead) fi);
parser.parse();
COSDocument cd = parser.getDocument();
PDFTextStripper stripper = new PDFTextStripper();
content = stripper.getText(new PDDocument(cd));
cd.close();
return content;
}
/**
- Main method.
- @param args
- @throws FileNotFoundException
-
@throws IOException
*/
public static void main(String args[]) throws FileNotFoundException, IOException
{
String filepath = “/home/wirlei/dados_aplicacao/Pão_de_Queijo.pdf”;
System.out.println(new PdfFileParser().PdfFileParser(filepath));
}
}
Desde já agradeço