【问题标题】:iText7.1.3 adding SVG into PdfDocumentiText7.1.3 将 SVG 添加到 PdfDocument
【发布时间】:2019-03-09 20:14:10
【问题描述】:

使用 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 中,在 Trim trailing whitespace

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

对于trimmedText = A (length = 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);
    }
}

【问题讨论】:

    标签: java svg itext7


    【解决方案1】:

    你指出的trimTrailingWhitespace确实有一个bug

    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;
    }
    

    [...] 周围的框架也变得不必要了。并且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);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-08
      • 2019-06-13
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 2019-03-23
      • 2018-03-16
      相关资源
      最近更新 更多