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

如何使用Apache POI为整段文本设置3个制表位的缩进?

问题

我尝试使用Apache POI为整段文本设置3个制表位的缩进,编写了如下代码:

import org.apache.poi.xwpf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class ApachePOIExample {
    public static void main(String[] args) {
        // 创建新Word文档
        XWPFDocument document = new XWPFDocument();

        // 创建段落
        XWPFParagraph paragraph = document.createParagraph();

        // 创建文本段并添加内容
        XWPFRun run = paragraph.createRun();
        run.addTab();
        run.addTab();
        run.addTab();
        run.setText("A paragraph is a series of sentences that are organized and coherent, and are all related to a single topic. Almost every piece of writing you do that is longer than a few sentences should be organized into paragraphs. This is because paragraphs show a reader where the subdivisions of an essay begin and end, and thus help the reader see the organization of the essay and grasp its main points.");

        // 保存文档
        try (FileOutputStream fileOutputStream = new FileOutputStream("example.docx")) {
            document.write(fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

运行后发现run.addTab()仅缩进了段落首行,效果为仅首行有三个制表位的空白,其余行靠左对齐。我需要实现整段所有行都缩进三个制表位宽度的效果,能否通过Apache POI实现?

解决方案

当前方法仅在文本开头添加制表符,只会影响首行。要实现整段缩进,需直接设置段落的左侧缩进,让整个段落的所有行向右偏移指定宽度。

实现步骤

  1. 确认Word默认制表位宽度:通常为500 Twip(1 Twip = 1/20 点,约等于0.0176厘米),3个制表位对应的总宽度为3 * 500 = 1500 Twip。
  2. 通过XWPFParagraph的setIndentationLeft()方法设置段落左侧缩进值。

修改后的完整代码

import org.apache.poi.xwpf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;

public class ApachePOIExample {
    public static void main(String[] args) {
        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();

        // 设置整段左侧缩进为3个默认制表位宽度
        int defaultTabWidth = 500; // Word默认制表位宽度(单位:Twip)
        paragraph.setIndentationLeft(defaultTabWidth * 3);

        XWPFRun run = paragraph.createRun();
        run.setText("A paragraph is a series of sentences that are organized and coherent, and are all related to a single topic. Almost every piece of writing you do that is longer than a few sentences should be organized into paragraphs. This is because paragraphs show a reader where the subdivisions of an essay begin and end, and thus help the reader see the organization of the essay and grasp its main points.");

        try (FileOutputStream fileOutputStream = new FileOutputStream("example.docx")) {
            document.write(fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

补充说明

  • 若需自定义制表位宽度(比如设置为1厘米),可将defaultTabWidth换算为对应Twip值(1厘米≈360 Twip),再计算总缩进值。
  • 如果需要更复杂的制表位规则(如不同位置的制表位对齐方式),可通过CTTabStop类手动添加段落的制表位配置,但整段缩进场景下,直接设置左侧缩进是最优方案。

内容的提问来源于stack exchange,提问作者Bharath Naidu Yenumula

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 10:02:10