解决pdfbox中文乱码问题

buybyetoo 2010-12-09 06:07:43
网上搜的中文解决办法都不管用,花了几天时间,终于自己解决了。
##org.apache.pdfbox.pdmodel.font.PDFont.java

public String encode( byte[] c, int offset, int length ) throws IOException
{
String retval = null;
if( cmap != null )
{
if (length == 1 && cmap.hasOneByteMappings())
{
retval = cmap.lookup( c, offset, length );
}
else if (length == 2 && cmap.hasTwoByteMappings())
{
retval = cmap.lookup( c, offset, length );
}

}

// there is no cmap but probably an encoding with a suitable mapping
if( retval == null )
{
Encoding encoding = getEncoding();
if( encoding != null )
{
retval = encoding.getCharacter( getCodeFromArray( c, offset, length ) );
}

// when the font has a DescendantFonts ,try to decode with it
if(length == 2 && retval == null && this instanceof PDType0Font)
{
COSArray descendantFontArray =(COSArray)font.getDictionaryObject( COSName.DESCENDANT_FONTS );

if( descendantFontArray != null && descendantFontArray.size() != 0)
{
COSDictionary descendantFontDictionary = (COSDictionary)descendantFontArray.getObject( 0 );
PDFont descendentFont = PDFontFactory.createFont( descendantFontDictionary );
CMap cmp = descendentFont.cmap ;
String cmpName = cmp.getRegistry()+"-"+cmp.getOrdering()+"-UCS2";
CMap cmp2 = cmapObjects.get(cmpName);

if(cmp2 == null && threadLocale.get() == null)
{
parseCmap( resourceRootCMAP, getClass().getResourceAsStream(cmpName), COSName.getPDFName(cmpName) );
threadLocale.set(true);
cmp2 = cmapObjects.get(cmpName);
if(cmp2 == null)
{
log.warn("Can't parse the CMap for " + cmpName);
}
}

if(cmp2 != null)
{
retval = cmp2.lookup(c, offset, length);
}

}
}

if( retval == null && (cmap == null || length == 2))
{
retval = getStringFromArray( c, offset, length );
}
}
return retval;
}
...全文
3166 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuangr 2011-01-02
  • 打赏
  • 举报
回复
杂不发呢?
buybyetoo 2010-12-10
  • 打赏
  • 举报
回复
还有点问题。解决后继续发
24K純帥 2010-12-10
  • 打赏
  • 举报
回复
解决了就好~
Jlins 2010-12-10
  • 打赏
  • 举报
回复
哦 谢谢共享..
buybyetoo 2010-12-10
  • 打赏
  • 举报
回复
看另外一篇...

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧