I am facing a problem when invoking the setValue method of a PDField and trying to set a value which contains special characters.
field.setValue("TEST-BY (TEST)")
In detail, if my value contains characters as U+00A0 i am getting the following exception:
Caused by: java.lang.IllegalArgumentException: U+00A0 is not available in this font's encoding: WinAnsiEncoding
A complete stracktrace can be found here: Stacktrace
I currently have set PDType1Font.TIMES_ROMAN as font. In order to solve this problem i tried with other available fonts as well. The same problem persisted.
I found the following suggestion in this answer https://stackoverflow.com/a/22274334/7434590 but since we use the setValue and not any of the methods showText/drawText that can manipulate bytes, i could not use this approach since setValue accepts only string as a parameter.
Note: I cannot replace the characters with others to solve this issue, i must be able to set any kind of supported by the font character in the setValue method.
You'll have to embed a font and not use WinAnsiEncoding:
PDFont formFont = PDType0Font.load(doc, new FileInputStream("c:/windows/fonts/somefont.ttf"), false); // check that the font has what you need; ARIALUNI.TTF is good but huge
PDResources res = acroForm.getDefaultResources(); // could be null, if so, then create it with the setter
String fontName = res.add(formFont).getName();
String defaultAppearanceString = "/" + fontName + " 0 Tf 0 g"; // adjust to replace existing font name
textField.setDefaultAppearance(defaultAppearanceString);
Note that this code must be ran before calling setValue().
More about this in the CreateSimpleFormWithEmbeddedFont.java example from the source code download.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With