如何使用 Spire.Doc 在 Java 中以编程方式拆分 Word 文档

发布: (2025年12月11日 GMT+8 09:38)
4 min read
原文: Dev.to

Source: Dev.to

介绍 Spire.Doc for Java 及安装

Spire.Doc for Java 是一款专业的 Java 库,旨在无需安装 Microsoft Office 即可创建、写入、编辑、转换和打印 Word 文档。它支持 DOC、DOCX、RTF 和 XML 格式。其完整的 API 让开发者能够以高保真度执行诸如拆分、合并或提取内容等复杂的文档操作。

要在项目中集成 Spire.Doc for Java,请在 pom.xml(Maven)中添加以下依赖:

<dependency>
    <groupId>com.e-iceblue</groupId>
    <artifactId>spire.doc</artifactId>
    <version>13.11.2</version>
    <repository>
        <id>e-iceblue</id>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</dependency>

添加依赖后,同步项目即可下载所需的库。

按分页符拆分 Word 文档

按分页符拆分 Word 文档非常适合每个逻辑单元(例如章节或报告段落)都从新页面开始的情况。当视觉上的页面分隔与内容分隔一致时,此方法效果最佳。

按分页符拆分的步骤

  1. 创建 Document 实例,并使用 Document.loadFromFile() 加载源文件。
  2. 创建一个新的 Word 文档并向其添加一个节。
  3. 遍历原始文档每个节中的所有正文子对象,识别段落和表格。
  4. 如果对象是表格,则直接将其添加到新文档的节中。
  5. 如果对象是段落,则将段落添加到新节中,然后检查其子对象是否包含分页符。
  6. 当发现分页符时,从段落中移除该分页符,保存当前的新文档,并为下一块内容启动一个新文档。
  7. 重复上述过程,直至所有内容处理完毕。

Java 示例

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class SplitDocByPageBreak {
    public static void main(String[] args) throws Exception {
        // Load the original document
        Document original = new Document();
        original.loadFromFile("E:\\Files\\SplitByPageBreak.docx");

        // Prepare the first output document
        Document newWord = new Document();
        Section section = newWord.addSection();
        int index = 0;

        // Traverse all sections of the original document
        for (int s = 0; s = 0) {
                                    section.getParagraphs().get(0).getChildObjects().removeAt(breakIdx);
                                    breakIdx--;
                                }
                            }
                        } else if (obj instanceof Table) {
                            // Add tables directly to the new document
                            section.getBody().getChildObjects().add(obj.deepClone());
                        }
                    }
                }

                // Save the final part
                newWord.saveToFile("output/result" + index + ".docx", FileFormat.Docx);
            }
        }
    }
}

按分节符拆分 Word 文档

按分节符拆分提供了更细粒度的控制,尤其适用于具有不同页眉/页脚、页面方向或其他布局差异的文档。分节符表示一个逻辑划分,可以拥有独立的格式属性。

按分节符拆分的步骤

  1. 创建 Document 实例并加载源文件。
  2. 创建一个新的 Word 文档。
  3. 遍历原始文档中的所有节。
  4. 对每个节使用 Section.deepClone() 进行克隆。
  5. 使用 Document.getSections().add() 将克隆后的节添加到新文档中。
  6. 使用 Document.saveToFile() 保存生成的文档。

Java 示例

import com.spire.doc.*;

public class SplitDocBySectionBreak {
    public static void main(String[] args) throws Exception {
        // Load the original document
        Document original = new Document();
        original.loadFromFile("E:\\Files\\SplitBySectionBreak.docx");

        // Prepare the output document
        Document newDoc = new Document();

        // Iterate through each section and clone it into the new document
        for (int i = 0; i < original.getSections().getCount(); i++) {
            Section srcSection = original.getSections().get(i);
            Section clonedSection = (Section) srcSection.deepClone();
            newDoc.getSections().add(clonedSection);
        }

        // Save the split document
        newDoc.saveToFile("output/sectionSplit.docx", FileFormat.Docx);
    }
}
Back to Blog

相关文章

阅读更多 »