![Creative The name of the picture]()
java: translating string to greek characters
I'm trying to writte some Greek text into a pdf form via iText. I'm retrieving the string from the DB, where is in Greek, but when I try to print it into the pdf form, it shows nothing (the pdf is also Greek-written). I've tried almost everything I've found, same in here, same in iText forum.
As you can see, the string is in greek:

but it is not written into the pdf:

EDIT:
I've lost count how the efforts trying to make this happen. Converting the string to an array of bytes and then coding it back was one of the closest aproches i could make. Sadly, there is no much code to be shown. As told befor, i've got an string qich has the dfata shown and when traying to printi it into dthe pdf form with a sentence like form.getField("municipality").setValue(municipio);
the output is, again, the shown.
form.getField("municipality").setValue(municipio);
EDIT2:
Using itext 7.0.4, now we can't create the font, as nor baseforn nor font classes are beeing recogniced as itext classes.
import com.itextpdf.*;
...
final String f = "resources/fonts/FreeSans.ttf";
Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
...
form.getField("municipality").setValue(municipio);
EDIT3:
After inserting the following maven dependency, now I can use de basefonts classes:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
float fontSize = (float) 10.0;
PdfFont font = null;
final String f = (String) VarGlobales.getHTTPApplication().get("ruta.fuentes") + "/FreeSans.ttf";
try {
font = PdfFontFactory.createFont(f, true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PdfReader pdfReader = null;
try {
pdfReader = new PdfReader(new FileInputStream(
(String) VarGlobales.getHTTPApplication().get("ruta.tenderForms") + "/" + tenderFormFile));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter pdfWriter = new PdfWriter(baos);
PdfDocument pdfDoc = new PdfDocument(pdfReader, pdfWriter);
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
form.getField("municipality").setValue(municipio, font, fontSize);
But the string is, once more, not beeing painted. Am I using it correctly?
FYI, the fields created in the pdf form were created via Adobe Acrobat 9.
2 Answers
2
Enconding of non-ASCII symbols is font-dependent. You should look for a font that supports Greek symbols. If you use one of the main fonts in IText, you won't have any trouble; if not, you should embed the font as described in the IText manual.
Ps: you should put your code, efforts and version used.
Well, When all else fails you can convert your Greek String into unicode sequence and send it as such. Then you will know for sure that you are sending String of Greek characters and if the problem persists it is with the ability to correctly display it rather then the content. I wrote an Open Source java library called MgntUtils that has the utilty that converts any String to unicode sequence and vise-versa. All you will have to do is:
String codes = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence("Hello world");
And it will return String "u0048u0065u006cu006cu006fu0020u0057u006fu0072u006cu0064"
"u0048u0065u006cu006cu006fu0020u0057u006fu0072u006cu0064"
The same would work for any String in any language including special characters. Here is the link to the article Open Source Java library with stack trace filtering, Silent String parsing Unicode converter and Version comparison that explains about the library and where to get it (available both on Maven central and github. In the article search for paragraph: "String Unicode converter"
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
idownvotedbecau.se/nocode
– fantaghirocco
yesterday