1

I have encountered the same problem as described here, i.e to output € sign in my generated pdf document. The euro sign should be appended after the data retrieved from database, in this case the solution provided is not very useful, I tried with this lines, so that I can use the variable euroToPDF anywhere I want:

Encoding e = EncodingManager.INSTANCE.getEncoding(COSName.WIN_ANSI_ENCODING);

String euroToPDF = String.valueOf(Character.toChars(e.getCode(e.getCharacter(128))));

but the application throws IOException, saying No character code for character name '€', anyone knows how to solve this?

Community
  • 1
  • 1
Blake
  • 7,367
  • 19
  • 54
  • 80
  • possible duplicate of [PdfBox encode symbol currency euro](http://stackoverflow.com/questions/22260344/pdfbox-encode-symbol-currency-euro) – mkl Apr 21 '15 at 10:56
  • @mkl if you read my post carefully, I have referenced that post, and said it didn't solve my case. – Blake Apr 21 '15 at 11:34
  • 1
    If you had read that post carefully and looked into the related sources, you have seen that the canonical way to adress the issue is to program a replacement for `PDPageContentStream.drawString(String)`, not to try to feed it differently. (Admittedly, I called it a duplicate after seen Alex' answer which essentially duplicates that post.) – mkl Apr 21 '15 at 12:29
  • @mkl while, in my case the accepted answer will heavily complicate my code, I am not going after that approach, instead I am after the other solution somebody claimed to be working fine in the post, and try to solve my issue with only two line of codes. but it didn't seem to work, thus I am trying to know why it didn't by posting here. – Blake Apr 21 '15 at 12:51
  • Gentlemen. Sorry for posting a partial duplicate, but my intention was to stress a particular aspect of the problem that I consider very important. Which version of PDFBox are we talking about? In ver 2 the whole question looks pointless to me. While in the 1.* versions there are multiple known bugs with mapping codepoints that were _properly_ fixed only in ver 2. For the narrow problem presented only a hack below looks reliable to me, and I had my own quest to Unicode in PDFBox. – Alex Nevidomsky Apr 21 '15 at 19:45
  • @Blake *I am after the other solution somebody claimed to be working fine in the post, and try to solve my issue with only two line of codes. but it didn't seem to work,* - I doubt that simpler solution ever worked in the context of an 1.8.* PDFBox version and a string to draw using `PDPageContentStream.drawString(String)`. Even if that line using the encoding to map the symbol back and forth did not throw an exception, it would result in a character with a value > 127 and, therefore, cause `drawString(String)` to use some UTF-16 encoding which is not the encoding required there. – mkl Apr 22 '15 at 08:14
  • @AlexNevidomsky Indeed, in the 1.8.* versions encoding is utterly broken, not merely for the € symbol. But you are talking about version 2 as if there already was such a version. There is none yet, you can merely take a SNAPSHOT of the current state of the 2.0.0 development code. Thus, whenever people ask PDFBox questions without stating a version, I currently assume they use the latest release, i.e. currently 1.8.9. – mkl Apr 22 '15 at 08:28

1 Answers1

1

If you are using v2 of PDFBox, you should use the € symbol in the string you output, without trying to convert the encoding.

In v1.8 you can use a trick:

contentStream.beginText();
contentStream.setTextMatrix(100, 0, 0, 100, 50, 100);
contentStream.setFont(PDType1Font.HELVETICA, 8);
byte[] commands = "(x) Tj ".getBytes();
commands[1] = (byte) 128;
contentStream.appendRawCommands(commands);
contentStream.endText();
contentStream.close();
Alex Nevidomsky
  • 668
  • 7
  • 14