iText7.1.3 添加 SVG 到 PdfDocument

iText7.1.3 adding SVG into PdfDocument

使用 iText 7.1.3 并尝试将 SVG 文件添加到 PdfDocument 给出了一个输出,其中 texts with长度 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 中,在 Trim 尾随空格

的行中
trimmedText = SvgTextUtil.trimTrailingWhitespace("A");

对于trimmedText = A(长度=1)returns空字符串

/**
* 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 is whitespace and not a newline, increase current 后跟 current--; 已经表明的那样,此方法是 trimLeadingWhitespace 的副本(相同注释后的行确实 增加了 current)修改为在String参数的另一端工作。不幸的是,修改是不正确的:如果字符串在位置 0 处有一个非空白字符,之后只有空白字符,它会被错误地认为是空的。

修复方法是替换

while (current > 0)

来自

while (current >= 0)

if(current == 0)

来自

if(current < 0)

通过该修复

if (end > 0) {
    [...]
}else{
    return toTrim;
}

frame around [...] 也变得不必要了。 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);
}