0

Perhaps this is a repetetive query, but I am not sure how to go about this?

I referred all threads available, but not able to proceed further. I am using PDFBox to drawString . It works fine with all special characters too exce[pt superscript characters. What I want to write to PDF is "90000039-PREDISOL ® C YELLOW 13 SNDOT™M" but whaT actually gets written is "þÿ 9 0 0 0 0 0 3 9 - P R E D I S O L ® C Y E L L O W 1 3 S N D O T!" M "

Can some1 please help me in detail as to how do I identify which encoding this is taking and how do I proceed with this?

Also, is there any other option so that we can avoid loading of ttf through file explicitly.

Thanks & Regards, Archana

  • 1
    PDFBox text encoding methods in versions before the current 2.0.0-SNAPSHOT development version are quite deficient. Your problem essentially is the same as [the problem here with the € symbol](http://stackoverflow.com/a/22274334/1729265). In your case, though, there is an obvious work-around: Write the superscript `TM` using normal letters in a smaller font with a bit of text rise. – mkl Jun 03 '15 at 13:58

1 Answers1

0

As already explained in this answer, PDFBox's String encoding is far from perfect yet (version 1.8.x). Unfortunately it uses the same routines when encoding strings in generic PDF objects as when encoding strings in content streams which is fundamentally wrong.

Along the lines of that answer, one would create the text drawing command manually for the special character:

    contents.drawString("90000039-PREDISOL ® C YELLOW 13 SNDOT");
    byte[] commands = "(x) Tj ".getBytes();
    commands[1] = (byte) 0231;
    contents.appendRawCommands(commands);
    contents.drawString("M");

(DrawSpecialCharacters method testDrawTmSignCustomDraw)

testDrawTmSignCustomDraw output

As the special character in question essentially is a set of superscripted normal characters, one can alternatively draw it using letters in a smaller font drawn with a bit of text rise:

    contents.drawString("90000039-PREDISOL ® C YELLOW 13 SNDOT");
    contents.appendRawCommands("\n6 Ts\n".getBytes());
    contents.setFont(font, 10);
    contents.drawString("TM");
    contents.appendRawCommands("\n0 Ts\n".getBytes());
    contents.setFont(font, 18);
    contents.drawString("M");

(DrawSpecialCharacters method testDrawTmSignLetters)

testDrawTmSignLetters output

Community
  • 1
  • 1
mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thanks a lot . This is really good. Just 1 more question. How to handle this case in case the data is generated dynamically. Here we have considered a static Strting and this solution is perfect in this case. What if we do not know befor hand the String to be generated – archana chaubey Jun 04 '15 at 09:38
  • Essentially one needs a replacement for the `drawString` method which letter by letter checks whether special treatment is necessary or not (the issue does not merely affect `€` and `TM`!) and creates proper string drawing instructions. – mkl Jun 04 '15 at 11:28