您基本上有两种选择来获取文本:
- 按原样直接从每个页面提取文本。
- 沿列空间将每一页一分为二,分别提取每一半的文本
对于第一个选项,我建议您先尝试pdftotext,但使用参数-layout。 (还有其他工具,例如 TET,来自 PDFlib 人员的文本提取工具包,如果 pdftotext 不够好,您可以尝试。)
对于使用 Ghostscript 和其他工具遵循第二个选项的道路,您可能需要查看我对以下问题的回答:
pdftotext -layout
您可以使用命令行工具pdftotext 进行尝试。您必须决定它是否“足够好”以达到您的目的。
以下命令仅从第 8 页(双列布局的第一页)中提取文本并将其打印到<stdout>:
$ pdftotext -f 8 -l 8 -layout \
Dictionary+of+Medical+Terms+4th+Ed.-+\(Malestrom\).pdf - \
| head -n 30
结果:
Medicine.fm Page 1 Thursday, November 20, 2003 4:26 PM
A
A /e/ noun a human blood type of the ABO abdominal distension /bdɒmn(ə)l ds
A abdominal distension
system, containing the A antigen (NOTE: Some- tenʃ(ə)n/ noun a condition in which the abdo-
one with type A can donate to people of the men is stretched because of gas or fluid
same group or of the AB group, and can receive abdominal pain /b dɒmn(ə)l pen/ noun
abdominal pain
blood from people with type A or type O.) pain in the abdomen caused by indigestion or
AA
AA abbr Alcoholics Anonymous more serious disorders
A & E /e ənd i
/, A & E department /e ənd abdominal viscera /bdɒmn(ə)l vsərə/
A & E abdominal viscera
i
d pɑ
tmənt/ noun same as accident and
plural noun the organs which are contained in
emergency department the abdomen, e.g. the stomach, liver and intes-
A & E medicine /e ənd i
med(ə)sn/
A & E medicine
tines
abdominal wall /b dɒmn(ə)l wɔ
l/ noun
abdominal wall
noun the medical procedures used in A & E de-
partments muscular tissue which surrounds the abdomen
abdomino- /bdɒmnəυ/ prefix referring to
abdomino-
注意-layout的使用!没有它,提取的文本将如下所示:
Medicine.fm 第 1 页 2003 年 11 月 20 日星期四下午 4:26
一种
A /e/ 名词 ABO 的人类血型
系统,包含 A 抗原(注意:SomeA
A 型的人可以捐赠给
同组或AB组,可接收
A型或O型人的血液。)
AA abbr 酗酒者匿名
A & E /e ənd i
/, A & E 部门 /e ənd
一世
d pɑ
tmənt/ 名词与事故和
急诊科
A & E 医学 /e ənd i
med(ə)sn/
名词 A & E deAA 中使用的医疗程序
A&E
急诊医学
隔间
AB /e bi
/名词人类血型的
ABO 系统,包含 A 和 B 抗原
乙
我注意到该文件在第 8 页使用但未嵌入字体 Courier、Helvetica、Helvetica-Bold、Times-Roman 和 Times-Italic。
这不会对文本提取造成问题,因为所有这些字体都使用/WinAnsiEncoding。
但是,还有其他字体作为子集嵌入。这些字体确实使用/Custom 编码,但它们不提供/ToUnicode 表。可靠的文本提取(将字形名称反向翻译为字符名称)需要此表。
我说的可以看这张表:
$ pdffonts -f 8 -l 8 Dictionary+of+Medical+Terms+4th+Ed.-+\(Malestrom\).pdf
name type encoding emb sub uni object ID
------------------------------ ----------- ------------- --- --- --- ---------
Helvetica-Bold Type 1 WinAnsi no no no 1505 0
Courier Type 1 WinAnsi no no no 1507 0
Helvetica Type 1 WinAnsi no no no 1497 0
MOEKLA+Times-PhoneticIPA Type 1C Custom yes yes yes 1509 0
Times-Roman Type 1 WinAnsi no no no 1506 0
Times-Italic Type 1 WinAnsi no no no 1499 0
IGFBAL+EuropeanPi-Three Type 1C Custom yes yes no 1502 0
碰巧我最近为一个新的 GitHub 项目手工编码了 5 个不同的 PDF 文件,并带有注释的源代码。这 5 个文件展示了正确的/ToUnicode 表对于作为子集嵌入的每种字体的重要性。可以在这里找到它们,以及解释更多细节的自述文件