java: translating string to greek characters

Multi tool use
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.
stackoverflow.com/questions/3858423/…
– fantaghirocco
yesterday
@fantaghirocco butI'm not using any custom font and theoretically it is not mandatory if using the default for iText, or am I wrong?
– Alfredo Piris
yesterday
I'm not an iText expert but seems that using a font that has Greek characters in it - either custom or not - could solve your issue
– fantaghirocco
yesterday
I'll give it a try tomorrow morning, ty!
– Alfredo Piris
yesterday
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.
I'm pretty much sure that we are using one of the main fonts...
– Alfredo Piris
yesterday
But are you sure this font support the Greek symbols?
– Rafael Palomino
yesterday
we a re using the default one. Maeby it is not supported? could you point me to the direction of changing the font?
– Alfredo Piris
yesterday
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"
@MichaelGantamn Thanks a lot for the effort taken on creating such a great library! After using it, I can tell youy that the main problem is that itext's font can't print the desired characters.
– Alfredo Piris
5 hours ago
@Alfredo Piris - You made my day! Thanks for the feedback. Could I ask you for a small favor - could you go to the article that I provided in my answer and write a comment with your feedback? I am trying to advertise my library as I think it is worth it
– Michael Gantman
4 hours ago
@MichaelGantamn Done! Have you got any clue on how to change the font of a form field in itext? TY!
– Alfredo Piris
4 hours ago
@Alfredo Piris - Thanks for the comment. As to your question I worked with iText last time about 12 - 13 years ago. I was very impressed with the library then but today I certainly would not be able to help you much on practical questions
– Michael Gantman
3 hours ago
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