You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Yavuz Nuzumlalı <ma...@gmail.com> on 2011/10/17 10:23:58 UTC

Correct way to understand whether text is bold or italic?

Hi,

When try to get whether the font of a TextPosition object is bold or italic
using this type of a code;

PDType1CFont fnt = null;
PDType0Font fnt2 = null;
Font font = null;
int styleFont = Font.PLAIN;
try {
    if (tp.getFont() instanceof PDType1CFont) {
       fnt = ((PDType1CFont) tp.getFont());
       font = fnt.getawtFont();
    }
    else if (tp.getFont() instanceof PDType0Font) {
       fnt2 = ((PDType0Font) tp.getFont());
       font = fnt2.getawtFont();
    }

    if(font.isBold())
       tpChar = "<b>" + tpChar + "</b>";
    if(font.isItalic())
       tpChar = "<i>" + tpChar + "</i>";
} catch (Exception e) {
           //System.out.println("ERROR!!");
}

(here tp is a TextPosition object)
Using this code, isBold() and isItalic() methods always returns false. And
sometimes tp.getFont() doesn't match with both PDType1CFont and PDType0Font
classes, so font comes as null. What's the correct way to get font's bold or
italic property?

Regards,