Hey Pdfbox-Users,
I'm using Pdfbox for a custom pdf library. I'm using the TextStripper
class to extract textual contents of the pdf's pages. This works for
alsmost 99% of my pdfs. Here's the old implementation:
<code>
public String getNameAsString( COSName key )
{
String retval = null;
COSName name = (COSName)getDictionaryObject( key );
if( name != null )
{
retval = name.getName();
}
return retval;
}
</code>
But for some I got ClassCastExceptions in
COSDictionary#getNameAsString(COSName key) (used by TextStripper ..)
because something returned by getDictionaryObject(COSName key) is not
a COSName. After adding an "instanceof"-test I sometimes got
NullPointerExceptions. So here is a new implementation with some
simple checks which works for 100% of my pdfs ;-)
public String getNameAsString( COSName key )
{
String retval = null;
COSBase cb = getDictionaryObject( key );
if (cb != null) {
if (cb instanceof COSName) {
COSName name = (COSName)cb;
if( name != null )
{
retval = name.getName();
}
} else {
retval = cb.toString();
}
}
return retval;
}
As far as I can tell there are no semantics touched by these modifications.
Thanks for your attention!