PDFBox U+00A0 在此字体的编码中不可用 [英] PDFBox U+00A0 is not available in this font's encoding

查看:222
本文介绍了PDFBox U+00A0 在此字体的编码中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用 PDField 的 setValue 方法并尝试设置包含特殊字符的值时遇到问题.

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)")

详细来说,如果我的值包含 U+00A0 字符,则会出现以下异常:

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可用此字体的编码:WinAnsiEncoding

Caused by: java.lang.IllegalArgumentException: U+00A0 is not available in this font's encoding: WinAnsiEncoding

可以在此处找到完整的 stracktrace:Stacktrace

A complete stracktrace can be found here: Stacktrace

我目前已将 PDType1Font.TIMES_ROMAN 设置为字体.为了解决这个问题,我也尝试了其他可用的字体.同样的问题仍然存在.

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.

我在这个答案https://stackoverflow.com/a/22274334/7434590中发现了以下建议,但由于我们使用 setValue 而不是任何可以操作字节的 showText/drawText 方法,我无法使用这种方法,因为 setValue 只接受字符串作为参数.

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.

注意:我不能用其他字符替换来解决这个问题,我必须能够在 setValue 方法中设置字体字符支持的任何类型.

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.

推荐答案

您必须嵌入字体而不使用 WinAnsiEncoding:

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);

请注意,此代码必须在调用 setValue() 之前运行.

Note that this code must be ran before calling setValue().

CreateSimpleFormWithEmbeddedFont.java 源代码下载示例.

More about this in the CreateSimpleFormWithEmbeddedFont.java example from the source code download.

这篇关于PDFBox U+00A0 在此字体的编码中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆