问题
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.
回答1:
"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.
来源:https://stackoverflow.com/questions/47944235/how-to-print-non-breaking-space-to-a-pdf-using-apache-pdf-box