【问题标题】:itextpdf insert space beetwen 7 and dot after extract textitextpdf 在提取文本后在 7 和点之间插入空格
【发布时间】:2016-05-25 14:06:32
【问题描述】:

我的问题描述了这张图片 http://185.49.12.119/~pogdan/7spacedot/7spacedot.jpg 输入文件 http://185.49.12.119/~pogdan/7spacedot/monitor_2016_99.pdf

输出文件 http://185.49.12.119/~pogdan/7spacedot/monitor_2016_99.txt

所有用jar和java设置的文件 http://185.49.12.119/~pogdan/7spacedot/

为什么itextpdf插入空格?如何删除它?更换 7 。 -> 7. 没有为我解决。

【问题讨论】:

    标签: java itextpdf pdftotext


    【解决方案1】:

    为什么itextpdf要插入空格?

    每当两个连续的文本块之间存在大于一定数量的间隙时,iText 就会插入空格,或者如果两个连续的文本块重叠。这样做是为了表明这些块没有以正常方式相互跟随。

    在您的文档中,7 后面的点通常会尽可能向左移动,以便字符边界框重叠:

    怎么去掉?

    如果你不想这样,你必须相应地调整你使用的文本提取策略。

    在当前的 5.5.9 中,代码如下所示:

    if (result.charAt(result.length()-1) != ' ' && renderInfo.getText().length() > 0 && renderInfo.getText().charAt(0) != ' '){ // we only insert a blank space if the trailing character of the previous string wasn't a space, and the leading character of the current string isn't a space
        float spacing = lastEnd.subtract(start).length();
        if (spacing > renderInfo.getSingleSpaceWidth()/2f){
            appendTextChunk(" ");
            //System.out.println("Inserting implied space before '" + renderInfo.getText() + "'");
        }
    }
    

    您的古代 iText 版本的来源在这里可能看起来仍然相似。这就是您必须更改逻辑以不为后退插入空格或至少只为较大的空格插入的地方。


    正如 OP 在评论中解释的那样,使用

    float spaceWidth = renderInfo.getSingleSpaceWidth() * 3f/2f;
    float diffI1 = start.subtract(lastEnd).get(Vector.I1);
    if (spacing > spaceWidth && diffI1 > 0)
    {
        result.append(" ");
    }
    

    在他的情况下效果很好。但是,这并不意味着人们通常应该以这种方式更改策略代码,因为它假定书写面向正 x 轴的方向。此外,乘以renderInfo.getSingleSpaceWidth() 的常数的最佳值也取决于手头的文档类型,参见。例如this case.

    【讨论】:

    • 感谢您的解释。我正在尝试这样做 float diffI1= lastEnd.subtract(start).get(Vector.I1); if (diffI1>0) { appendTextChunk();} 但它没有。
    • 会发生什么?
    • 结果和之前一样 185.49.12.119/~pogdan/7spacedot/7spacedot1.jpg // 127670. LELY EAST -> LEL Y EAST :( float spacing = lastEnd.subtract(start).length(); float spaceWidth= renderInfo. getSingleSpaceWidth()/2f; float diffI1= lastEnd.subtract(start).get(Vector.I1); if (spacing > spaceWidth && diffI1>0){ result.append(" "); }
    • 错误,应该是 diff1 spaceWidth && diffI1>0){ result.append(" "); } 工作得很好。感谢您的回答!
    • @pogdan 工作得很好 - 很棒。在这种情况下,请考虑将答案标记为已接受;你可以通过点击它左上角的勾号来完成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2012-10-08
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多