Using Java PDFBox library to write Russian PDF
- by Brad
I am using a Java library called PDFBox trying to write text to a PDF. It works perfect for English text, but when i tried to write Russian text inside the PDF the letters appeared so strange. It seems the problem is in the font used, but i am not so sure about that, so i hope if anyone could guide me through this. Here is the important code lines :
PDTrueTypeFont font = PDTrueTypeFont.loadTTF( pdfFile, new File( "fonts/VREMACCI.TTF" ) ); // Windows Russian font imported to write the Russian text.
font.setEncoding( new WinAnsiEncoding() ); // Define the Encoding used in writing.
// Some code here to open the PDF & define a new page.
contentStream.drawString( "??????? ????????????" ); // Write the Russian text.
The WinAnsiEncoding source code is : Click here
--------------------- Edit on 18 November 2009
After some investigation, i am now sure it is an Encoding problem, this could be solved by defining my own Encoding using the helpful PDFBox class called DictionaryEncoding.
I am not sure how to use it, but here is what i have tried until now :
COSDictionary cosDic = new COSDictionary();
cosDic.setString( COSName.getPDFName("Ercyrillic"), "0420 " ); // Russian letter.
font.setEncoding( new DictionaryEncoding( cosDic ) );
This does not work, as it seems i am filling the dictionary in a wrong way, when i write a PDF page using this it appears blank.
The DictionaryEncoding source code is : Click here
Thanks . . .