Apache的PdfBox提供了处理Pdf文件的丰富的接口,可以提取Pdf中的文本信息。但是,也有PdfBox无法处理的情况,究竟是什么情况呢?
最近在利用Apache公司的PdfBox做Pdf文件处理时,发现有几个Pdf文件的文本信息提取不到。但是手动打开这些Pdf文件时,看上去也是正常的。
到后台日志看,发现有以下警告信息:
警告: No Unicode mapping for 99 (99) in font null
三月 30, 2019 2:10:47 下午 org.apache.pdfbox.pdmodel.font.PDSimpleFont toUnicode
从字面上看,似乎是与字体有关,是不是该文件使用了特殊的字体转换过来的呢?
直接把Pdf文件拖放到浏览器中,能够正常查看,但是选择文本时无法选择。
有没有可能这是从图片转过来的Pdf呢,那读取文本肯定不能读取了。
但是看转换过的文本,还是有文本信息的,只不过是乱码而已。
到PdfBox官网继续查,发现官网上有这样的描述:
https://pdfbox.apache.org/2.0/faq.html#text-extraction
How come I am not getting any text from the PDF document?
Text extraction from a pdf document is a complicated task and there are many factors
involved that effect the possibility and accuracy of text extraction. It would be helpful
to the PDFBox team if you could try a couple things.
-
Open the PDF in Acrobat and try to extract text from there. If Acrobat can extract text then PDFBox
should be able to as well and it is a bug if it cannot. If Acrobat cannot extract text then PDFBox ‘probably’ cannot either.
-
It might really be an image instead of text. Some PDF documents are just images that have been scanned in.
You can tell by using the selection tool in Acrobat, if you can’t select any text then it is probably an image.
原来,还真有很多情况是无法读取文本的。
按照说明,把Pdf文件在Acrobat中打开,里面也能选择文本,但是拷贝出来是乱码,并且通过查找功能无法查找到最简单的字符。
看来,官网也给出来这些不能处理的情况,那么,只能从OCR的方式也考虑了。