如何从不可搜索的pdf中检测出可搜索的pdf?

时间:2015-07-08 17:24:00

标签: java pdf ocr tesseract apache-tika

我有一堆pdf文件,有些是可搜索的常规pdf文件,有些是不可搜索的某些文档的扫描版本。我想提取每个pdf的内容。要提取常规pdf的内容,我使用https://regex101.com/r/fM9dP1/3并从我正在使用Apache Tika的不可搜索的内容中提取内容。但是我需要区分哪个pdf是nornal pdf,哪个不是。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

这将为您提供帮助,

public static boolean isSearchablePdf(String filePath) throws Exception {

    String parsedText;
    PDFTextStripper pdfStripper = null;
    PDDocument document = null;
    COSDocument cosDoc = null;
    File file = new File(filePath);
    boolean isSearchable = true;

    PDFParser parser = new PDFParser(new RandomAccessFile(file, "r"));
    parser.parse();
    cosDoc = parser.getDocument();
    pdfStripper = new PDFTextStripper();
    document = new PDDocument(cosDoc);
    int noOfPages = document.getNumberOfPages();

    for (int page = 1; page <= noOfPages; page++) {

        pdfStripper.setStartPage(page);
        pdfStripper.setEndPage(page);

        parsedText = pdfStripper.getText(document);
        isSearchable = isSearchable & isSearchablePDFContent(parsedText, page);

        if (!isSearchable) {
            break;
        }
        if (page >= 5) {
            break;
        }

    }
    if (isSearchable && noOfPages > 10) {
        int min = 5;
        int max = noOfPages;
        for (int i = 0; i < 4; i++) {
            int randomNo = min + (int) (Math.random() * ((max - min) + 1));
            pdfStripper.setStartPage(randomNo);
            pdfStripper.setEndPage(randomNo);
            parsedText = pdfStripper.getText(document);
            isSearchable = isSearchable & isSearchablePDFContent(parsedText, randomNo);
            if (!isSearchable)
                break;
        }
    }
    if (isSearchable && noOfPages >= 10) {
        for (int page = noOfPages - 5; page < noOfPages; page++) {
            pdfStripper.setStartPage(page);
            pdfStripper.setEndPage(page);
            parsedText = pdfStripper.getText(document);
            isSearchable = isSearchable & isSearchablePDFContent(parsedText, page);
            if (!isSearchable)
                break;

        }
    }

    if (document != null){
        document.close();
    }

    return isSearchable;
}

public static boolean isSearchablePDFContent(String contentOfPdf, int pageNo) throws IOException {
    int count = 0;
    boolean isSearchable = false;
    if (!contentOfPdf.isEmpty()) {
        StringTokenizer st = new StringTokenizer(contentOfPdf);
        while (st.hasMoreTokens()) {
            st.nextToken();
            if (count >= 3) {
                isSearchable = true;
                break;
            }
            count++;
        }

    } else {
        isSearchable = false;
    }

    return isSearchable;
} 

答案 1 :(得分:0)

你能不能使用Tika提取文字和图像,一旦你知道文字很少,你只需将图像提供给tesseract?根据这个答案Extract Images from PDF with Apache Tika图像提取应该是可能的,至少在理论上是这样。

如果Tika不起作用,您应该能够使用PDFbox采用相同的方法。有关常规文本提取部分,请参阅How to read PDF files using Java?;有关图像提取的提示,请参阅extract images from pdf using pdfbox

相关问题