首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从不可搜索的pdf中检测出可搜索的pdf?

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

Stack Overflow用户
提问于 2015-07-09 01:24:00
回答 2查看 1.4K关注 0票数 2

我有一堆pdf文件,一些是可搜索的常规pdf文件,一些是一些不可搜索的文件的扫描版本。我想提取每个pdf的内容。要提取常规tesseract-ocr的内容,我使用pdf;要从不可搜索的pdfs中提取内容,我使用pdf。然而,我需要区分哪个pdf是正常的pdf,哪个不是。有没有办法做到这一点?

EN

回答 2

Stack Overflow用户

发布于 2019-11-12 19:45:21

这会对你有帮助

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2015-07-10 05:47:49

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31299514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档