在通过占位符渲染模版时,文本内容会自动换行,如何解决这个问题

模版定义:

渲染结果:


占位符字符串:

代码:

源代码:

public class CustomTextReplacingHandle implements IReplacingCallback {
    private final String content;

    public CustomTextReplacingHandle(String content) {
        this.content = content;
    }

    public int replacing(ReplacingArgs args) throws Exception {
        DocumentBuilder builder = new DocumentBuilder((Document)args.getMatchNode().getDocument());
        builder.moveTo(args.getMatchNode());
        double firstLineIndent = (double)20.0F;
        String[] lines = this.content.split("\n");

        for(String line : lines) {
            if (line.contains("\t")) {
                builder.getParagraphFormat().setFirstLineIndent(firstLineIndent);
                line = line.replace("\t", "").trim();
            } else {
                builder.getParagraphFormat().setFirstLineIndent((double)0.0F);
            }

            builder.write(line);
            builder.writeln();
        }

        args.getMatchNode().remove();
        return 1;
    }
}

@JOOL 您在插入文本行时使用了 builder.writeln();。 例如,如果该行是数组中的最后一行,则需要将其排除在外:

if (!line.equals(lines[lines.length - 1].replace("\t", "").trim()))
	builder.writeln();