我有一堆pdf文件,一些是可搜索的常规pdf文件,一些是一些不可搜索的文件的扫描版本。我想提取每个pdf的内容。要提取常规tesseract-ocr的内容,我使用pdf;要从不可搜索的pdfs中提取内容,我使用pdf。然而,我需要区分哪个pdf是正常的pdf,哪个不是。有没有办法做到这一点?
发布于 2019-11-12 19:45:21
这会对你有帮助
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;
}
发布于 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。
https://stackoverflow.com/questions/31299514
复制相似问题