问题描述
我看到"java.lang.IllegalArgumentException:字体中的U + 05D0没有字形"(例如),在调用PDFPageContentStream的showText(String)方法时抛出异常.
I'm seeing "java.lang.IllegalArgumentException: No glyph for U+05D0 in font" (as an example) exception being thrown when calling the showText(String) method of PDFPageContentStream.
捕获异常不是很有用,因为不会写出好的字符.也不检查输入字符串中的每个字符,这将成为性能杀手(每个PDF可能是数千页,数百万个字符).我真正需要的是一种防止任何丢失的字形出现异常并自动将其替换为其他字形或动态显示的,显示unicode值的字形的方法.
Catching the exception isn't very helpful because good characters won't get written. Neither is checking each character in the input string, which would be a performance killer (each PDF could be thousands of pages, millions of characters). What I really need is a way to prevent the exception for ANY missing glyph and have it automatically replaced with some other glyph, or a dynamically created glyph that shows the unicode value.
我不想停止生成PDF,因为字体不支持特定的字形,我只想使用一些替换字符并继续使用.
I don't want to stop producing the PDF because a font doesn't support a particular glyph, I just want some replacement character to be used instead and keep going.
如何实现?
推荐答案
这就是我所做的
private final char[] replacements = IntStream.range(0, 1<<16)
.map(c -> canRender(font, c) ? c : "?")
.collect(StringBuilder::new, StringBuilder::appendCodePoint,
StringBuilder::append)
.toString().toCharArray();
// This is extremely ugly!!!
private boolean canRender(PDType0Font font, int codepoint) {
try {
font.getStringWidth(new String(Character.toChars(codepoint)));
return true;
} catch (final Exception e) {
return false;
}
}
String sanitize(String input) {
return input.codePoints()
.map(c -> c<replacements.length ? replacements[c] : '?')
.collect(StringBuilder::new, StringBuilder::appendCodePoint,
StringBuilder::append)
.toString();
我不认为,值得进行优化,因为在生成PDF的过程中,还需要做更多的工作,包括hasGlyph
测试.
I don't think, it's worth optimizing as during the PDF generation, more work has to be done, including the hasGlyph
tests.
这篇关于如何忽略PDFBox 2.0.7使用的字体中缺少的字形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!