You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为iText-PDF生成的PDF正确配置行间距?

问题:iText-PDF 生成PDF时行间距不符合预期

我正在开发一款生成PDF的应用,目前使用iText-PDF库进行概念验证(POC)。配置行间距时遇到问题:手动生成的PDF中红色行与黑色行间距紧凑符合预期,但通过代码生成的PDF里两行间距过大。

当前生成代码如下:

@Test
void testDocumentWithCustomFont() throws FileNotFoundException {
    inDocument(CUSTOM_FONT_DESTINATION, document -> {
        try {
            FontProgram fontProgram = FontProgramFactory.createFont(CUSTOM_FONT);
            PdfFont customFont = PdfFontFactory.createFont(fontProgram, PdfEncodings.IDENTITY_H,
                    PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);

            Text title = new Text("THIS IS A TITLE").setFont(customFont);
            title.setFontSize(18);
            document.add(new Paragraph(title).setTextAlignment(TextAlignment.CENTER).setMarginBottom(12));

            Text chordLine = new Text("THIS IS A RED LINE").setFont(customFont);
            chordLine.setFontSize(12);
            chordLine.setFontColor(ColorConstants.RED);
            document.add(new Paragraph(chordLine).setMarginBottom(0));

            Text lyricLine = new Text("THIS IS A BLACK LINE THAT SHOULD BE NEAR TO THE RED").setFont(customFont);
            lyricLine.setFontSize(12);
            document.add(new Paragraph(lyricLine));
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    });
}

private void inDocument(String destination, Consumer<Document> documentConsumer) throws FileNotFoundException {
    try (Document document = new Document(new PdfDocument(new PdfWriter(destination)))){
        documentConsumer.accept(document);
    }
}
解决方案

问题出在两个独立Paragraph之间的默认间距上,仅设置setMarginBottom(0)不足以消除段落间的额外间距,需要结合调整spacing属性:

  1. 对红色行的Paragraph,同时设置setMarginBottom(0)和setSpacingAfter(0),取消该段落后的所有额外间距
  2. 对黑色行的Paragraph,设置setSpacingBefore(0),取消该段落前的额外间距

修改后的完整代码:

@Test
void testDocumentWithCustomFont() throws FileNotFoundException {
    inDocument(CUSTOM_FONT_DESTINATION, document -> {
        try {
            FontProgram fontProgram = FontProgramFactory.createFont(CUSTOM_FONT);
            PdfFont customFont = PdfFontFactory.createFont(fontProgram, PdfEncodings.IDENTITY_H,
                    PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);

            Text title = new Text("THIS IS A TITLE").setFont(customFont);
            title.setFontSize(18);
            document.add(new Paragraph(title).setTextAlignment(TextAlignment.CENTER).setMarginBottom(12));

            Text chordLine = new Text("THIS IS A RED LINE").setFont(customFont);
            chordLine.setFontSize(12);
            chordLine.setFontColor(ColorConstants.RED);
            // 调整红色行段落的底部间距和后间距
            document.add(new Paragraph(chordLine).setMarginBottom(0).setSpacingAfter(0));

            Text lyricLine = new Text("THIS IS A BLACK LINE THAT SHOULD BE NEAR TO THE RED").setFont(customFont);
            lyricLine.setFontSize(12);
            // 调整黑色行段落的前间距
            document.add(new Paragraph(lyricLine).setSpacingBefore(0));
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }
    });
}

private void inDocument(String destination, Consumer<Document> documentConsumer) throws FileNotFoundException {
    try (Document document = new Document(new PdfDocument(new PdfWriter(destination)))){
        documentConsumer.accept(document);
    }
}

说明:iText中margin和spacing是独立的属性,spacing用于控制段落与其他元素之间的额外间距,仅清除margin无法完全消除段落间的默认空隙,两者结合才能实现两行紧凑排列的效果。

内容的提问来源于stack exchange,提问作者Alvaro Pedraza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 09:43:09