aspose.word删除分页符

Aspose.word用法都类似,此处使用aspose for java进行操作
项目需要将word去掉所有的分页符,再进行一级大纲为划分的分页

目标文件状态:

img

思考逻辑:遍历整个paragraphs节点下run节点,并取得分页符号节点后移除该节点

   public Document deletePageBreaker(String fileName) throws Exception{
        //获取文件
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName + ".docx");
        Document doc = new Document(inputStream);
        for (Section section : doc.getSections()) {
            Body body = section.getBody();
            for (Paragraph paragraph : body.getParagraphs()) {
                for (Run run : paragraph.getRuns()) {
                    if("\f".equals(run.getText())){
                        run.remove();
                    }
                }
            }
        }
        return doc;
    }

但是此方法移除节点后会导致在原有的分页符位置中有换行符的残留,因为以文件节点的思路来说,run移除自身,但是原本的父级节点paragraph依旧存在(无内容)会以单个换行符进行占位

img

       InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName + ".docx");
        Document doc = new Document(inputStream);
        for (Section section : doc.getSections()) {
            Body body = section.getBody();
            for (Paragraph paragraph : body.getParagraphs()) {
                for (int i = 0; i < paragraph.getRuns().getCount(); i++) {
                    Run run = paragraph.getRuns().get(i);
                    if("\f".equals(run.getText())&&paragraph.getRuns().getCount()==1){
                        paragraph.remove();
                    }
                }
            }
        }
        doc.save(HOME + "tee.docx");

所以实际思路应该为移除该父级别paragraph节点
在这里插入图片描述
转换后
在这里插入图片描述
符合预期

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值