Extract text from a pdf file using Apache Tika in java
Asked Answered
A

1

5
try {
      File file = new File("Example.pdf");
      String content = new Tika().parseToString(file);
      System.out.println("The Content: " + content);
    } catch (Exception e) {
       e.printStackTrace();
    }

I have imported java.io.File and import org.apache.tika.Tika; but while running this code I am getting error like this :

Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;Ljava/lang/Throwable;)V at org.apache.commons.logging.impl.SLF4JLocationAwareLog.warn(SLF4JLocationAwareLog.java:162) at org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.loadDiskCache(FileSystemFontProvider.java:461) at org.apache.pdfbox.pdmodel.font.FileSystemFontProvider.(FileSystemFontProvider.java:217) at org.apache.pdfbox.pdmodel.font.FontMapperImpl$DefaultFontProvider.(FontMapperImpl.java:130) at org.apache.pdfbox.pdmodel.font.FontMapperImpl.getProvider(FontMapperImpl.java:149) at org.apache.pdfbox.pdmodel.font.FontMapperImpl.findFont(FontMapperImpl.java:413) at org.apache.pdfbox.pdmodel.font.FontMapperImpl.findFontBoxFont(FontMapperImpl.java:376) at org.apache.pdfbox.pdmodel.font.FontMapperImpl.getFontBoxFont(FontMapperImpl.java:350) at org.apache.pdfbox.pdmodel.font.PDType1Font.(PDType1Font.java:146) at org.apache.pdfbox.pdmodel.font.PDType1Font.(PDType1Font.java:79) at org.apache.pdfbox.pdmodel.font.PDFontFactory.createFont(PDFontFactory.java:62) at org.apache.pdfbox.pdmodel.PDResources.getFont(PDResources.java:143) at org.apache.pdfbox.contentstream.operator.text.SetFontAndSize.process(SetFontAndSize.java:60) at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:838) at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:495) at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:469) at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:150) at org.apache.pdfbox.text.LegacyPDFStreamEngine.processPage(LegacyPDFStreamEngine.java:139) at org.apache.pdfbox.text.PDFTextStripper.processPage(PDFTextStripper.java:391) at org.apache.tika.parser.pdf.PDF2XHTML.processPage(PDF2XHTML.java:147) at org.apache.pdfbox.text.PDFTextStripper.processPages(PDFTextStripper.java:319) at org.apache.pdfbox.text.PDFTextStripper.writeText(PDFTextStripper.java:266) at org.apache.tika.parser.pdf.PDF2XHTML.process(PDF2XHTML.java:117) at org.apache.tika.parser.pdf.PDFParser.parse(PDFParser.java:167) at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:280) at org.apache.tika.parser.CompositeParser.parse(CompositeParser.java:280) at org.apache.tika.parser.AutoDetectParser.parse(AutoDetectParser.java:135) at org.apache.tika.Tika.parseToString(Tika.java:527) at org.apache.tika.Tika.parseToString(Tika.java:642) at java_programs.PdfParse.main(PdfParse.java:22)

Aperiodic answered 31/7, 2017 at 11:4 Comment(1)
How did you add Apache Tika to your project? Only it looks like you've incorrectly got an old slf4j jar instead of the one from TikaDaisy
O
6

The following seems to work for me. I get the string I want but I also get some warnings printed in the console.

[on Windows] I compile and run it like this:

javac -cp .;tika-app-1.16.jar Test.java

java -cp .;tika-app-1.16.jar Test

What tika jar are you using? I have added another method (tikaPdfTest()) to show a different way of getting the text from a PDF that might help you.

import java.io.File;
import org.apache.tika.Tika;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.sax.BodyContentHandler;
import org.apache.tika.Tika;

import org.xml.sax.SAXException;

public class Test {
    public static void main(final String[] args) {
        //Your way
        try {
            File file = new File("Example.pdf");
            String content = new Tika().parseToString(file);
            System.out.println("The Content: " + content);
        } catch (final Exception e) {
            e.printStackTrace();
        }

        //Another way
        try {
            System.out.println("The contents:\t[" + Test.tikaPdfTest("Example.pdf") + "]");
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }

    public static String tikaPdfTest(final String fileName) throws IOException, SAXException, TikaException {
        try(final FileInputStream inputstream = new FileInputStream(new File(fileName))) {
            final BodyContentHandler handler = new BodyContentHandler();
            new PDFParser().parse(inputstream, handler, new Metadata(), new ParseContext());
            return handler.toString().trim();
        }
    }
}
Olnee answered 31/7, 2017 at 11:55 Comment(4)
Thanks for the answer. i am using tika-app-1.16.jarAperiodic
Can u tell me which jars u had used so that i can remove the other jars coz its still showing me the same error after running your code.Aperiodic
Thanks it worked fine. i just create a new project and a new java file in it.Aperiodic
I only used that 1 jar file. I am glad you got it working.Olnee

© 2022 - 2024 — McMap. All rights reserved.