1

I am using PDFBox API 2.0.1 version for reading PDF files. I want to extract the text properties and written the following code to get the bold property of text

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

From the above code, I am getting 0.0 for every character even-though the character is in bold. Please suggest solution. Thanks in advance.

sagar
  • 115
  • 1
  • 1
  • 10
  • The font weight is an optional entry, in your case it most likely is not there. You might want to look at [this old answer](http://stackoverflow.com/a/19777953/1729265). – mkl Dec 30 '16 at 12:38
  • current version is 2.0.4 (won't change anything re your question) – Tilman Hausherr Dec 30 '16 at 13:01

1 Answers1

2

usingFontDescriptor.getFontWeight()There is no guarantee of determining that whether the text is of bold or not.

I prefer you to use text.getFont().getBaseFont().Contains("bold") for extracting Bold text .

Gaurav Varshney
  • 502
  • 5
  • 15
  • 'getBaseFont()' method is not available in pdfbox- 2.0.1 version – sagar Jan 02 '17 at 04:34
  • 2
    use `text.getFont().getName().toLower().Contains("bold")`. – Tilman Hausherr Jan 02 '17 at 15:39
  • I have used the method of testing the font name with some success, but I am looking at PDF where the font name is being returned as "Microsoft San Serif" regardless of whether the font is bold or not. And getFontWeight returns 0 on both bold and normal strings. – Fred Andrews Sep 26 '17 at 23:28