我正在使用pdfbox从pdf文件中提取文本 . 我阅读pdf文件如下
PDFParser parser = null;
String text = "";
PDFTextStripper stripper = null;
PDDocument pdoc = null;
COSDocument cdoc = null;
File file = new File("path");
try {
parser = new PDFParser(new FileInputStream(file));
} catch (IOException e) {
e.printStackTrace();
}
try {
parser.parse();
cdoc = parser.getDocument();
stripper = new PDFTextStripper();
pdoc = new PDDocument(cdoc);
stripper.setStartPage(1);
stripper.setEndPage(2);
text = stripper.getText(pdoc);
System.out.println(text);
} catch (IOException e) {
e.printStackTrace();
}
但我想要做的是逐行阅读文档并从每一行中提取文本属性,如粗体,斜体 . 我怎样才能通过pdfbox库实现这一目标
1 回答
粗体和斜体等属性不是PDF中的第一类属性 .
Bold 或使用PDF格式的斜体书写
不同的字体(这是更好的方式);在这种情况下,可以尝试确定字体是粗体还是斜体
查看字体名称:它可能包含子字符串“bold”,“italic”,“oblique”......
查看字体的一些可选属性,例如字体重量...
检查嵌入的字体文件 .
这些方法都不是万无一失的;要么
使用与非粗体非斜体文本相同的字体,但使用特殊技术使它们显示为粗体或斜体(又名穷人的粗体),例如
不仅可以填充字形轮廓,还可以沿着它绘制更粗的线条,以获得大胆的印象,
画了两次雕文,第二次稍微移位,也是为了大胆的印象,
使用文本或转换矩阵倾斜字母以显示斜体 .
通过相应地重写
PDFTextStripper
方法,您可以在PDF文本提取期间获得相当好的样式猜测率 .