1

I have a PDF file and want to extract text from it. I am using PDFTextStream. I got this code from its documentation, but it gives error.

import com.snowtide.PDF;
import com.snowtide.pdf.Document;
import com.snowtide.pdf.OutputTarget;

public class ExtractText {
  public static void main (String[] args) throws java.io.IOException {
   // String pdfFilePath = args[0];

    Document pdf = PDF.open("data/bill.pdf");
    StringBuilder text = new StringBuilder(1024);
    pdf.pipe(new OutputTarget(text));
    pdf.close();
    System.out.println(text);
  }
}

This is the error:

Exception in thread "main" java.lang.NullPointerException
    at com.snowtide.pdf.e.b(SourceFile:2740)
    at com.snowtide.pdf.e.a(SourceFile:2691)
    at com.snowtide.pdf.e.c(SourceFile:2642)
    at com.snowtide.pdf.e.a(SourceFile:2059)
    at com.snowtide.pdf.e.c(SourceFile:2108)
    at com.snowtide.pdf.e.a(SourceFile:1950)
    at com.snowtide.pdf.e.a(SourceFile:1755)
    at com.snowtide.pdf.e.a(SourceFile:1820)
    at com.snowtide.pdf.e.pipe(SourceFile:468)
    at ExtractText.main(ExtractText.java:12)

I am using PDFTextStream version 3.3.1. Line 12 is this:

pdf.pipe(new OutputTarget(text));
Dax Amin
  • 497
  • 2
  • 5
  • 13
  • `pdf` is null. Most likely the path passed to `PDF.open()` is incorrect. – dambros Apr 15 '16 at 19:07
  • Please post some other lines of the error, at least the next 5 lines – Dazak Apr 15 '16 at 19:13
  • @Dazak This is all, no other lines of error. – Dax Amin Apr 15 '16 at 19:14
  • Sorry but I have tried to reproduce the error and it was not possible... I create a java project, put the same version to PDFTextStream on it and copy your code and it works good... even when the pdf file has no text on it... – Dazak Apr 15 '16 at 20:05
  • Well, I have to put the whole path of the pdf.. otherwise it can not find it (in that case java give me a java.io.FileNotFoundException) – Dazak Apr 15 '16 at 20:19
  • Yeah i tried it with other pdf file it worked. – Dax Amin Apr 15 '16 at 20:21
  • I posted an answer in order to give a solution... but feel free to delete the question if you consider it is not relevant anymore – Dazak Apr 15 '16 at 20:27

1 Answers1

0

The Pdf file should be corrupted or something... there is no way to reproduce the error. Try with another pdf file

Dazak
  • 1,011
  • 2
  • 9
  • 17