我正在使用 Apache 的 PDFBox 库来编写 PdfDocumentBuilder 类。currentFont.hasGlyph(character)在尝试将字符写入文件之前,我使用它来检查字符是否具有字形。问题是,当字符是 unicode 控制字符时'\u001f',hasGlyph()返回 true,导致encode()写入时抛出异常(请参阅下面的 PdfDocumentBuilder 代码和堆栈跟踪以供参考)。
我做了一些研究,看起来我使用的字体(Courier Prime)不支持这些 unicode 控制字符。
那么为什么hasGlyph()在不支持 unicode 控制字符时返回 true 呢?当然,我可以replaceAll在进入该writeTextWithSymbol()方法之前使用简单的命令从行中删除控制字符,但是如果该hasGlyph()方法没有按我预期的那样工作,我就会遇到更大的问题。
PDF文档生成器:
private final PDType0Font baseFont;
private PDType0Font currentFont;
public PdfDocumentBuilder () {
baseFont = PDType0Font.load(doc, this.getClass().getResourceAsStream("/CourierPrime.ttf"));
currentFont = baseFont;
}
private void writeTextWithSymbol (String text) throws IOException {
StringBuilder nonSymbolBuffer = new StringBuilder();
for (char character : text.toCharArray()) {
if (currentFont.hasGlyph(character)) {
nonSymbolBuffer.append(character); …Run Code Online (Sandbox Code Playgroud)