Dúvida PDFBOX

0 respostas
D

Se deixo chamar pelo main funciona, agora inicializando por outro lugar e mandando ler não funciona apresenta o seguinte erro

Quando chamo direto comento todo o main

CtrlPDF pdf = new CtrlPDF();  
        
        
        pdf.lerPDF("/Downloads/arquivo.pdf","/Downloads/final.txt");

Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
ERRO: Um erro ocorreu enquanto tentava obter o conteúdo do PDFjava.lang.NoClassDefFoundError: org/apache/pdfbox/pdfparser/PDFParser
at view.FrmConverterPDFVivoPTXT.btExecutarActionPerformed(FrmConverterPDFVivoPTXT.java:181)

package pdf;

import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.io.InputStreamReader;  
import java.util.logging.Level;
import java.util.logging.Logger;
  
import org.apache.pdfbox.pdfparser.PDFParser;  
import org.apache.pdfbox.pdmodel.PDDocument;  
import org.apache.pdfbox.util.PDFTextStripper;  

public class CtrlPDF {
    
    public static void main(String[] args) {  
  
        CtrlPDF pdf = new CtrlPDF();  
        
        
        pdf.lerPDF("/Downloads/arquivo.pdf","/Downloads/final.txt");
          
        FileInputStream stream = new FileInputStream("/Downloads/final.txt");  
          
        StringBuilder txt = new StringBuilder();  
        InputStreamReader streamReader = new InputStreamReader(stream);  
        BufferedReader reader = new BufferedReader(streamReader);  
        String line = null;  
          
        while ((line = reader.readLine()) != null) {  
      
            txt.append(line);  
                txt.append("\n");  
            if (line.contains(" ")) {  
                txt.append(line.replaceAll(" ", ";"));  
                txt.append("\n");  
            }  
        }
        * /
          
        System.out.println(txt);  
          */
    }
    
    public void lerPDF(String arquivoPDF,String arquivoTxt) {
        
        //System.out.println(arquivoPDF);
            FileWriter x = null;
        try {
            CtrlPDF pdf = new CtrlPDF();
            pdf.setEnderecoRecurso(arquivoPDF);
            String retorno = pdf.getConteudo();
            x = new FileWriter(arquivoTxt, false);
            x.write(retorno);
            x.close();
            
        } catch (IOException ex) {
            Logger.getLogger(CtrlPDF.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                x.close();
            } catch (IOException ex) {
                Logger.getLogger(CtrlPDF.class.getName()).log(Level.SEVERE, null, ex);
            }
        }  
          
    }  
  
    private String enderecoRecurso;  
  
    public void setEnderecoRecurso(String enderecoRecurso) {  
        this.enderecoRecurso = enderecoRecurso; // endereço dos ficheiros  
    }  
  
    public String getConteudo() {  
  
        File f = new File(this.enderecoRecurso);  
        FileInputStream is = null;  
        try {  
            is = new FileInputStream(f);  
        } catch (IOException e) {  
            System.out.println("ERRO: " + e.getMessage());  
            return null;  
        }  
  
        PDDocument pdfDocument = null;  
        try {  
            PDFParser parser = new PDFParser(is);  
            parser.parse();  
            pdfDocument = parser.getPDDocument();  
            PDFTextStripper stripper = new PDFTextStripper();  
            return stripper.getText(pdfDocument);  
            
        } catch (IOException e) {  
            return "ERRO: Não é possível abrir a stream" + e;  
        } catch (Throwable e) {  
            // Fazemos um catch, uma vez que precisamos de fechar o recurso  
            return "ERRO: Um erro ocorreu enquanto tentava obter o conteúdo do PDF"  
                    + e;  
        } finally {  
            if (pdfDocument != null) {  
                try {  
                    pdfDocument.close();  
                } catch (IOException e) {  
                    return "ERRO: Não foi possível fechar o PDF." + e;  
                }  
            }  
        }  
    }  
}
Criado 1 de maio de 2013
Respostas 0
Participantes 1