2

I have a text to draw on pdf, something like 500 €/hour. I am using PdfBox-Android library.

I was trying to write above string as follows,

pageContentStream.drawString("500 " + Html.fromHtml(getString(R.string.euro)).toString() + "/hour");

where euro is defined in strings.xml as

<string name="euro">(&#8364;)</string>
<string name="pound">(&#163;)</string>

With above code PdfBox-Androidis writing some gibberish characters.

I found one solution here to write using pdfbox, which is working perfectly.

My question is how to write the text next to sign in one go..?

Do I need to write first, then move text next to it and then write remaining text? I don't feel that would be a correct solution.

Community
  • 1
  • 1
Rupesh
  • 3,415
  • 2
  • 26
  • 30
  • 1
    As mentioned in the answer you refer to, *PDFBox's String encoding is far from perfect yet (version 1.8.x)*. This actually is a nice way to say **it is broken**.. If you use PDFBox 1.8.* (or older), characters with Unicode codes beyond 255 can only be displayed using customized methods, not PDFBox' `PDPageContentStream.drawString`. – mkl May 04 '15 at 15:12

2 Answers2

1

You need to pass the HTML code of the currency symbol

Html.fromHtml((String) currency_symbol).toString()

Html.fromHtml((String) "&#8364;").toString() //for euro

Html.fromHtml((String) "&#163;").toString()  //for pound
WISHY
  • 11,067
  • 25
  • 105
  • 197
  • @Rupesh: you shouldn't just *'Accept'* this answer, but also *'Upvote'* it! – Kurt Pfeifle May 04 '15 at 12:07
  • pardon me.. I am reverting it.. I tried above solution with `£` and that worked.. but its not working for `€` and other characters... – Rupesh May 04 '15 at 12:36
  • What code are you using?? Refer this http://www.w3schools.com/charsets/ref_utf_currency.asp cause I tried its working fine – WISHY May 04 '15 at 12:41
  • @WISHY same as yours.. but the thing is I am drawing text on `pdf`. `PageContentStream`is still drawing garbage characters with above method.. – Rupesh May 04 '15 at 13:33
  • @Rupesh have look at this if it solves your problem http://stackoverflow.com/questions/22260344/pdfbox-encode-symbol-currency-euro – WISHY May 05 '15 at 05:06
1

by referring to this I manage to implement what I needed. Please refer following code snippet.

contentStream.beginText();

/*x will act as placeholder here*/
byte[] commands = "(500 x/year**) Tj ".getBytes();

/* commands[index_of_x] = (byte)128, where 128 is decimal value of octal
 * 200. (char code for '€' in WinAnsiEncoding).
 * you may want to refer annex D.2, Latin Character Set and Encodings of 
 * PDF specification ISO 32000-1
 */
commands[5] = (byte) 128;

contentStream.appendRawCommands(commands);
contentStream.endText();
contentStream.close();

PDF specification ISO 32000-1

Community
  • 1
  • 1
Rupesh
  • 3,415
  • 2
  • 26
  • 30