2

I created a simple PDF document using Acrobat that consists of a single test field. The font of the text field is set to David Bold. Then, I tried to fill this text field with hebrew characters using PDFBox and I get the following error:

java.lang.IllegalArgumentException: U+05D0 is not available in this font's encoding: WinAnsiEncoding

The PDFBox FAQ proposes the following solution:

Check whether the character is available in WinAnsiEncoding by looking at the PDF Specification Appendix D. If not, but if it is available in this font (in windows, have a look with charmap.exe), then load the font with PDType0Font.load(), see also in the EmbeddedFonts.java example in the source code download.

I checked the Appendix D and the characters I'm using are not part of it, as expected. However I don't really understand what the FAQ means by loading the font. I also checked the example but I don't use the same methods to fill my document. My code is as follows:

PDDocument pdfDocument = PDDocument.load(this.getClass().getClassLoader().getResourceAsStream("test.pdf"));
PDType0Font font = PDType0Font.load(pdfDocument, this.getClass().getClassLoader().getResourceAsStream("David-Bold_13087.ttf"));

PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
if (acroForm != null) {
    PDTextField field = (PDTextField) acroForm.getField("test");
    field.setValue("א");
}
manash
  • 6,985
  • 12
  • 65
  • 125
  • Can you share the file? What PDFBox version are you using (hopefully 2.0.5) – Tilman Hausherr Apr 05 '17 at 08:56
  • Yes - v2.0.5. You can download the file here http://docdro.id/S2bAfpV – manash Apr 05 '17 at 09:02
  • Sadly, this is a duplicate of https://stackoverflow.com/questions/42903217/write-cyrillic-chars-into-pdf-form-fields-with-pdfbox/ and https://issues.apache.org/jira/browse/PDFBOX-3138 – Tilman Hausherr Apr 05 '17 at 09:24
  • @TilmanHausherr is there a solution to that issue? – manash Apr 05 '17 at 09:29
  • No, except what the other guy did, which works only if you display the file with Adobe Reader. – Tilman Hausherr Apr 05 '17 at 09:29
  • You can use the workaround provided in https://stackoverflow.com/a/43002076 if the 'David Bold' is embedded as TrueType – Maruan Sahyoun Apr 05 '17 at 13:47
  • @TilmanHausherr is the problem related to the embedded font? will the issue be solved if I use another font? – manash Apr 06 '17 at 07:19
  • @MaruanSahyoun Thanks for the workaround, it worked except that text appears in the reverse direction. Any idea? I wonder if using another "standard" font would solve all these issues? – manash Apr 06 '17 at 07:21
  • @MickaelMarrache sorry, I don't know more, although I'd wish I could be able to help more. – Tilman Hausherr Apr 06 '17 at 08:05
  • @MickaelMarrache The form field uses the Arial font and not David, Bold. I can't replicate the 'reverse direction' you are describing. Setting the value to a string of 'This is a test' results in a display of this value. – Maruan Sahyoun Apr 06 '17 at 11:59
  • @MaruanSahyoun Please try with the following PDF http://docdro.id/aJqKtuw I used the workaround you mentioned and I get reversed text. – manash Apr 20 '17 at 14:55
  • @MaruanSahyoun I forgot to mention that the issue appears when I use right to left text (in this case, hebrew) – manash Apr 20 '17 at 14:59

0 Answers0