使用iText 7.1.3并尝试将 SVG 文件添加到 PdfDocument 会产生不呈现长度为 1 的文本的输出。我发现可能问题出在哪里。 请iText团队成员检查一下。
try {
SvgConverter.drawOnCanvas(svgUrl.openStream(), pdfCanvas_, imageLlx, imageLly);
} catch (IOException e) {
e.printStackTrace();
}
调试器调用:
processText:255, DefaultSvgProcessor (com.itextpdf.svg.processors.impl)
visit:212, DefaultSvgProcessor (com.itextpdf.svg.processors.impl)
visit:204, DefaultSvgProcessor (com.itextpdf.svg.processors.impl)
visit:204, DefaultSvgProcessor (com.itextpdf.svg.processors.impl)
executeDepthFirstTraversal:153, DefaultSvgProcessor (com.itextpdf.svg.processors.impl)
process:106, DefaultSvgProcessor (com.itextpdf.svg.processors.impl)
process:768, SvgConverter (com.itextpdf.svg.converter)
convertToXObject:555, SvgConverter (com.itextpdf.svg.converter)
convertToXObject:590, SvgConverter (com.itextpdf.svg.converter)
drawOnCanvas:380, SvgConverter (com.itextpdf.svg.converter)
在函数processText中,在修剪尾随空格行中
trimmedText = SvgTextUtil.trimTrailingWhitespace("A");
对于trimmedText = A(长度= 1)返回空字符串
/**
* Process the text contained in the text-node
*
* @param textNode node containing text to process
*/
private void processText(ITextNode textNode) {
ISvgNodeRenderer parentRenderer = this.processorState.top();
if (parentRenderer instanceof TextSvgNodeRenderer) {
// when svg is parsed by jsoup it leaves all whitespace in text element as is. Meaning that
// tab/space indented xml files will retain their tabs and spaces.
// The following regex replaces all whitespace with a single space.
//TODO(RND-906) evaluate regex and trim methods
String trimmedText = textNode.wholeText().replaceAll("\\s+", " ");
//Trim leading whitespace
trimmedText = SvgTextUtil.trimLeadingWhitespace(trimmedText);
//Trim trailing whitespace
trimmedText = SvgTextUtil.trimTrailingWhitespace(trimmedText);
parentRenderer.setAttribute(SvgConstants.Attributes.TEXT_CONTENT, trimmedText);
}
}
最佳答案
您指出的trimTrailingWhitespace
确实存在错误
public static String trimTrailingWhitespace(String toTrim) {
if(toTrim == null){
return "";
}
int end = toTrim.length();
if (end > 0) {
int current = end - 1;
while (current > 0) {
char currentChar = toTrim.charAt(current);
if (Character.isWhitespace(currentChar) && !(currentChar == '\n' || currentChar == '\r')) {
//if the character is whitespace and not a newline, increase current
current--;
} else {
break;
}
}
if(current == 0){
return "";
}else {
return toTrim.substring(0, current + 1);
}
}else{
return toTrim;
}
}
正如注释 //if the character iswhitespace and not a newline,increase current
后跟 current--;
已经表明,此方法是 trimLeadingWhitespace
(其中相同注释后的行确实增加当前
)修改为在String
的另一端工作范围。但不幸的是,修改是不正确的:如果字符串在位置 0 处有非空白字符且此后只有空白,则它会被错误地视为空。
修复方法是替换
while (current > 0)
由
while (current >= 0)
和
if(current == 0)
由
if(current < 0)
这样就解决了
if (end > 0) {
[...]
}else{
return toTrim;
}
此外,围绕[...]
的框架变得不必要。 while
循环可以更紧凑地表述为 for
循环,例如像这样:
public static String trimTrailingWhitespace(String toTrim) {
if (toTrim == null) {
return "";
}
int current = toTrim.length() - 1;
for ( ; current >= 0; current--) {
char currentChar = toTrim.charAt(current);
if (!(Character.isWhitespace(currentChar) && !(currentChar == '\n' || currentChar == '\r'))) {
break;
}
}
return current < 0 ? "" : toTrim.substring(0, current + 1);
}
关于java - iText7.1.3 将 SVG 添加到 PdfDocument 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52646675/