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