java+pdf特殊字符,Java PDFBox - 读取和修改带有特殊字符的pdf(变音符号)

i'm trying to modify a pdf using this method (first code block - using PDFStreamParser and iterating through PDFOperator, then updating COSString when needed):

I'm having an issue with some UTF-8 characters (diacritics): when I print the text that i want to update it show like "Societ? ?ii Na?ionale" (where '?' is a code like 0002 or 0004).

The funny things are:

when I write the updated pdf file, the characters are show correctly (even though i could't detected and replace them)

if i try to strip the text using PDFTextStripper 's getText(...), the text is extracted perfectly.

i tried 2 pdfbox versions: 1.5.0 (that behaves as described above) and 1.8.1 (where the final, written, pdf file does not display special characters correctly and "null" strings appear in the document)

What can I do (configure) for the classes used for updating the pdf (or at least try...) so that all of the UTF-8 characters are displayed correctly ?

EDIT:

Screenshot:

BN3jo.png

EDIT 2:

I searched through the pdfbox source code in PDFTextStripper and its superclass, and I found out how the text was extracted:

At the beginning of processStream method we have

graphicsState = new PDGraphicsState(aPage.findCropBox());

when stripping the text in processEncodedText, an instance of PDFont class is used like this:

final PDFont font = graphicsState.getTextState().getFont();

and the text is extracted from a byte[] with :

String c = font.encode( string, i, codeLength );

I'm digging now for more ...

解决方案

Finally, it seams that the process of extracting the fonts in the pdf file is quite complex. I couldn't use the fonts explicitly so I searched inside the code of PDFStreamEngine and the classes that extends OperatorProcessor and found how the PDFont objects were created into a map (I pretty much copied the chunks of code that i needed for extracting the diacritics). So, after that I used the detected fonts when interating through parser.getTokens() to call encode(...) method on each character in the "string".

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值