如何使用apache pdf box将“不间断空格"打印到pdf? [英] How to print `Non-breaking space` to a pdf using apache pdf box?
问题描述
final String NBSP = new String("\u00a0");contentStream.showText("Konichua!" + NBSP);
这会引发以下异常:
<块引用>java.lang.IllegalArgumentException: U+00A0 ('nbspace') 不是可用此字体 Courier 编码:WinAnsiEncoding
我已经尝试了所有可用的 3 种字体;TimesNewRoman, Courier &Helvica 与所有 3 导致相同的异常.
但是当您查看 apcahe pdfBox 源代码中的 WIN_ANSI_ENCODING_TABLE 时,
{040, "space"},{0243, "英镑"},..//添加一些在 pdf 规范的附录 D 中定义的附加映射{0240, "空格"},{0255, "连字符"}
我们可以看到定义了下面的不间断空格.
<前>DEC OCT HEX BIN 符号说明<前>160 240 A0 10100000 不间断空间
在 pdf 规范中 文档一>也引用了以下内容:
<块引用>空格字符也应在 MacRomanEncoding 中编码为 312,在 WinAnsiEncoding 中编码为 240.此重复代码应表示不间断空格;它在排版上应与 (U+003A) SPACE 相同.
<小时>
用例:
通过添加NBSP填充来增加标题txt列的宽度,以便在标题列上调用string.trim()
时不会删除它.
它在排版上应与 (U+003A) SPACE 相同".
所以它没有 nbsp/nbspace.通过调用 PDType0Font.load(document, new File("..."))
获取你的字体.
顺便说一句,对字符串调用 new String()
不是一个好主意.
final String NBSP = new String("\u00a0");
contentStream.showText("Konichua!" + NBSP);
this throws the following exception:
java.lang.IllegalArgumentException: U+00A0 ('nbspace') is not available in this font Courier encoding: WinAnsiEncoding
I have tried it with all the 3 fonts available; TimesNewRoman, Courier & Helvitica with all 3 resulting in the same exception.
But when you look at the WIN_ANSI_ENCODING_TABLE present in the source code of apcahe pdfBox,
{040, "space"},
{0243, "sterling"},
.
.
// adding some additional mappings as defined in Appendix D of the pdf spec
{0240, "space"},
{0255, "hyphen"}
we can see that the below Non-breaking space is defined.
DEC OCT HEX BIN Symbol Description
160 240 A0 10100000 Non-breaking space
In the pdf specification document the following is quoted too:
The SPACE character shall also be encoded as 312 in MacRomanEncoding and as 240 in WinAnsiEncoding . This duplicate code shall signify a nonbreaking space; it shall be typographically the same as (U+003A) SPACE.
Use case:
To increase the width of header txt columns by adding padding with NBSP, so that it is not removed when string.trim()
is called on header columns.
"it shall be typographically the same as (U+003A) SPACE".
So it doesn't have the nbsp / nbspace. Get your font by calling PDType0Font.load(document, new File("..."))
.
Btw, calling new String()
on a string is not a good idea.
这篇关于如何使用apache pdf box将“不间断空格"打印到pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!