0

I have the following text, which I receive from my database

"----- Some Text ------ Bônus -------- Some Text ------- "

I am storing it in a String variable and printing the variable in the console and writing it into the PDF , but the issue is I am not getting the text in the correct format in console as well as in PDF , rather am getting Bônus as 'Bônus' , I referred this example http://itext.2136553.n4.nabble.com/Problem-with-spanish-character-td2163635.html

and changed the encoding of my compiler in gradle file to ,

compileJava.options.encoding = 'UTF-8'

But still the issue didnt get resolved

The example text I put out is just a sample one , there are many other words which gets changed or either a empty box is displayed.

Do I need to look into it from IText perspective ? or Compiler ?

Changing the Unicode of compiler didn't help though

Am Novice
  • 325
  • 2
  • 9
  • 21
  • Editor and compiler must be using the same encoding. The above could have resulted from a .java in UTF-8, and compiling with a Windows encoding. Try `\u00F4` instead of `ô` to check that issue. – Joop Eggen Mar 22 '18 at 09:52
  • using `\u00F4` am getting the correct value in console , but how do I get it working in real time ? – Am Novice Mar 22 '18 at 10:01
  • 2
    Then the configured compiler encoding did not take. I have no experience with gradle. Should you find a solution, answer your own question - for others with the same problem. **It could also be a database problem.** In mysql you have to specify UTF-8 in the database and in the transport, – Joop Eggen Mar 22 '18 at 10:05
  • *"using \u00F4 am getting the correct value in console"* - using \u00F4, what do you get in PDF? – mkl Mar 22 '18 at 11:01

2 Answers2

1

Check if you have a unicode font. It could cause problem in your case. There is a code for setting font

BaseFont basefont = BaseFont.createFont("font_name.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

And then use it just like that:

Paragraph p = new Paragraph("text in unicode", new Font(basefont, 22));
dgebert
  • 1,235
  • 1
  • 13
  • 30
  • I am using the following font style `Style style = new Style() .setFont(PdfFontFactory.createFont(StandardFonts.COURIER_OBLIQUE);) .setFontSize(4.5f) .setFontColor(ColorConstants.BLACK);` – Am Novice Mar 22 '18 at 09:49
  • Did you tried to use something else? For example: FontConstants.TIMES_ROMAN? What is the result? – dgebert Mar 22 '18 at 10:00
0

After some more searching , I was finally able to get it in the UTF 8 form

byte[] ptext = originalString.getBytes(ISO_8859_1);
String value = new String(ptext, UTF_8);

Please find the link I referred to,

Encode String to UTF-8

Am Novice
  • 325
  • 2
  • 9
  • 21
  • This implies that the text as you retrieve it from the database is broken. – mkl Mar 22 '18 at 11:24
  • Well, strictly speaking it would be preferably if this could be corrected by changing database driver or database settings. If the data in the database already is broken, though, your code likely is the best thing one can still do short of a complete remake of all database access in all applications concerned. – mkl Mar 22 '18 at 12:37