Java如何获取word文档分节符?

想通过Java读取一个word文档并按照文档中的分节符分割个多个word文档,如何获取这个分节符{MNcode3%%Z/codeEC13KS1Y)PZL$L.png

阅读 2.7k
1 个回答

Spire.Doc for Java支持按节来拆分Word文档,其中包含了如何遍历获取文档中的节。代码示例如下:

import com.spire.doc.Document;

public class SplitWordBySection {

    public static void main(String[] args) {

        //创建Document对象
        Document document = new Document();
        
        //加载要拆分的文档
        document.loadFromFile("C:UsersAdministratorDesktopsections.docx");
        
        //声明新的Document对象
        Document newWord;
        
        //遍历源文档中的节
        for (int i = 0; i < document.getSections().getCount(); i++)
        {
            //初始化新的Document对象
            newWord = new Document();
            
            //将源文档中的指定节复制到新文档
            newWord.getSections().add(document.getSections().get(i).deepClone());
            
            //保存新文档到指定文件夹 
            newWord.saveToFile(String.format("C:UsersAdministratorDesktopOutputresult-%d.docx", i));
        }
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题