如何使用pdfbox从pdf中提取粗体文本? [英] How to extract bold text from pdf using pdfbox?

查看:965
本文介绍了如何使用pdfbox从pdf中提取粗体文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache pdfbox来提取文本。我可以从pdf中提取文本,但我不知道如何知道这个词是否是粗体??? (代码建议会很好!!!)
以下是从pdf中提取纯文本的代码,该代码工作正常。

I am using a Apache pdfbox for extracting text. I can extract the text from pdf but I dont know how to know that whether the word is bold or not??? (code suggestion would be good!!!) Here is the code for extracting plain text from pdf that is working fine.

PDDocument document = PDDocument
    .load("/home/lipu/workspace/MRCPTester/test.pdf");
document.getClass();
if (document.isEncrypted()) {
    try {
        document.decrypt("");
    } catch (InvalidPasswordException e) {
        System.err.println("Error: Document is encrypted with a password.");
        System.exit(1);
    }
}

// PDFTextStripperByArea stripper = new PDFTextStripperByArea();
// stripper.setSortByPosition(true);
PDFTextStripper stripper = new PDFTextStripper();
stripper.setStartPage(1);
stripper.setEndPage(2);
stripper.setSortByPosition(true);
String st = stripper.getText(document);


推荐答案

PDFTextStripper <的结果/ code>是纯文本。因此,提取后,为时已晚。但是你可以覆盖它的某些方法,只允许通过根据你的意愿格式化的文本。

The result of PDFTextStripper is plain text. After extracting it, therefore, it is too late. But you can override certain methods of it and only let through text which is formatted according to your wishes.

如果是 PDFTextStripper 你必须覆盖

protected void processTextPosition( TextPosition text )

在覆盖中,检查相关文本是否符合您的要求( TextPosition 包含很多信息有问题的文本,不仅是文本本身),如果是,将 TextPosition文本转发到 super 实施。

In your override you check whether the text in question fulfills your requirements (TextPosition contains much information on the text in question, not only the text itself), and if it does, forward the TextPosition text to the super implementation.

但主要问题是识别哪个文字是粗体

The main problem is, though, to recognize which text is bold.

粗体的标准可能是字体名称中的单词 bold ,例如 Courier-BoldOblique - 使用 text.getFont()字体的后记名称访问文本字体使用字体的 getBaseFont()方法

Criteria for boldness may be the word bold in the font name, e.g. Courier-BoldOblique - you access the font of the text using text.getFont() and the postscript name of the font using the font's getBaseFont() method

String postscriptName = text.getFont().getBaseFont();

标准也可以来自字体描述符 - 使用<$获取字体的字体描述符c $ c> getFontDescriptor 方法,字体描述符具有可选的字体权重值

Criteria may also be from the font descriptor - you get the font descriptor of a font using the getFontDescriptor method, and a font descriptor has an optional font weight value

float fontWeight = text.getFont().getFontDescriptor().getFontWeight();

该值定义为


(可选; PDF 1.5;应用于标记PDF文档中的Type 3字体)完全限定字体名称或字体说明符的权重(厚度)​​组件。可能的值应为100,200,300,400,500,600,700,800或900,其中每个数字表示至少与其前身一样暗的重量。值400表示正常体重; 700表示粗体。

这些值的具体解释因字体而异。

The specific interpretation of these values varies from font to font.

一种字体的示例300可能看起来与另一种字体中的500最相似。

EXAMPLE 300 in one font may appear most similar to 500 in another.

(表122,第9.8.1节,ISO 32000-1)

可能需要检查粗体 -ism的其他提示,例如大线宽

There may be additional hints towards bold-ism to check, e.g. a big line width

double lineWidth = getGraphicsState().getLineWidth();

当渲染模式也绘制轮廓时:

when the rendering mode draws an outline, too:

int renderingMode = getGraphicsState().getTextState().getRenderingMode();

您可能需要尝试使用手头的文件,这些标准就足够了。

You may have to try with your the documents you have at hand which criteria suffice.

这篇关于如何使用pdfbox从pdf中提取粗体文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆