【问题标题】:How to get whether Character is bold or not in pdf using PDFBox如何使用 PDFBox 获取 pdf 中的字符是否为粗体
【发布时间】:2016-12-30 20:49:23
【问题描述】:

我正在使用 PDFBox API 2.0.1 版本来阅读 PDF 文件。我想提取文本属性并编写以下代码来获取文本的粗体属性

@Override
protected void processTextPositon(TextPosition text)
{
    System.out.println(text.getFont().getFontDescriptor().getFontWeight());
}

从上面的代码中,我得到每个字符的 0.0,即使该字符是粗体的。 请提出解决方案。提前致谢。

【问题讨论】:

  • 字体粗细是一个可选条目,在您的情况下,它很可能不存在。你可能想看看this old answer
  • 当前版本是 2.0.4(不会改变你的问题)

标签: java pdfbox


【解决方案1】:

使用FontDescriptor.getFontWeight()无法保证确定文本是否为粗体。

我更喜欢你使用text.getFont().getBaseFont().Contains("bold") 提取粗体文本。

【讨论】:

  • 'getBaseFont()' 方法在 pdfbox- 2.0.1 版本中不可用
  • 使用text.getFont().getName().toLower().Contains("bold")
  • 我使用了测试字体名称的方法并取得了一些成功,但我正在查看 PDF,无论字体是否为粗体,字体名称都返回为“Microsoft San Serif” . getFontWeight 在粗体和普通字符串上都返回 0。
猜你喜欢
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 2013-05-17
相关资源
最近更新 更多