Java PDFBox - 读取和修改带有特殊字符的pdf(变音符号) [英] Java PDFBox - Reading and modifying a pdf with special characters (diacritics)

查看:400
本文介绍了Java PDFBox - 读取和修改带有特殊字符的pdf(变音符号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此方法修改pdf(第一个代码块 - 使用PDFStreamParser并迭代PDFOperator,然后在需要时更新COSString):



http://grepcode.com/file/repo1.maven.org/maven2/org.apache.pdfbox/pdfbox/1.5.0/ org / apache / pdfbox / util / PDFTextStripper.java



我正在挖掘更多...

解决方案

最后,它确定了在pdf文件中提取字体的过程非常复杂。我无法明确地使用这些字体,因此我在PDFStreamEngine的代码和扩展OperatorProcessor的类中搜索并找到了如何将PDFont对象创建到地图中(我几乎复制了提取变音符号所需的代码块) 。所以,之后我在通过parser.getTokens()进行交互时使用检测到的字体来调用string中每个字符的encode(...)方法。


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

http://www.coderanch.com/t/556009/open-source/PdfBox-Replace-String-double-pdf

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:

  1. when I write the updated pdf file, the characters are show correctly (even though i could't detected and replace them)
  2. if i try to strip the text using PDFTextStripper 's getText(...), the text is extracted perfectly.
  3. 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:

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 );

The new problem is that when i instantiate a PDFont class with the same 2 lines of code, i get a "null" font class, and thus i cannot use .encode(...) method. Source code for those classes are here: http://grepcode.com/file/repo1.maven.org/maven2/org.apache.pdfbox/pdfbox/1.5.0/org/apache/pdfbox/util/PDFStreamEngine.java and http://grepcode.com/file/repo1.maven.org/maven2/org.apache.pdfbox/pdfbox/1.5.0/org/apache/pdfbox/util/PDFTextStripper.java

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".

这篇关于Java PDFBox - 读取和修改带有特殊字符的pdf(变音符号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆