一、下载用于拆分 Word DOCX 的 Java API
Aspose.Words for Java是一个功能强大且功能丰富的文档操作 API,可让您创建和处理 MS Word 文档。除了基本和高级的 Word 自动化功能外,API 还允许您将一个 Word 文档拆分为多个文档。您可以下载API 或使用以下配置将其安装在基于 Maven 的应用程序中。
<repository><id>AsposeJavaAPI</id><name>Aspose Java API</name><url>https://repository.aspose.com/repo/</url></repository>
<dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>21.1</version><classifier>jdk17</classifier></dependency>
二、Word 文档拆分器 –
在开始拆分文档之前,您需要将以下帮助程序类添加到您的项目中,以实现基于 Aspose.Words for Java 的 Java 文档拆分器。添加类后,您可以使用以下部分中提供的代码示例继续拆分文档。
- 分页器
三、使用 Java 拆分 Word DOCX
首先,让我们看一下如何按页拆分 MS Word 文档。在这种情况下,源文档的每一页都将转换为单独的 Word 文档。以下是拆分 Word 文档页面的步骤。
- 使用Document类加载 Word 文档。
- 创建PageSplitter的对象并使用Document对象对其进行初始化。
- 循环浏览文档中的页面。
- 使用PageSplitter.getDocumentOfPage(Int pageIndex)方法将每个页面检索到一个Document对象中。
- 使用Document.save(String)方法保存文档。
以下代码示例显示了如何使用 Java 拆分 Word 文档。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java// Open a Word documentDocument doc = new Document("Word.docx");// Split nodes in the document into separate pagesDocumentPageSplitter splitter = new DocumentPageSplitter(doc);// Save each page as a separate documentfor (int page = 1; page <= doc.getPageCount(); page++){Document pageDoc = splitter.getDocumentOfPage(page);pageDoc.save("SplitDocumentByPage_" + page + ".docx");}
四、在 Java 中使用页面范围拆分 Word DOCX
您还可以定义要从源 Word 文档中拆分的页面范围。以下是执行此操作的步骤。
- 使用Document类加载 Word 文档。
- 创建PageSplitter的对象并使用Document对象对其进行初始化。
- 使用PageSplitter.getDocumentOfPageRange(Int, Int)方法将页面集合检索到Document对象中。
- 使用Document.save(String)方法保存文档。
以下代码示例显示了如何使用 Java 按页面范围拆分 Word 文档。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java// Open a Word documentDocument doc = new Document("Word.docx");// Split nodes in the document into separate pagesDocumentPageSplitter splitter = new DocumentPageSplitter(doc);// Get part of the documentDocument pageDoc = splitter.getDocumentOfPageRange(3,6);pageDoc.save("SplitDocumentByPageRange.docx");
五、使用 Java 按部分拆分 Word 文档
Aspose.Words for Java 还允许您通过分节符拆分 Word 文档。以下是执行此操作的步骤。
- 使用Document类加载 Word 文档。
- 使用Document.getSections()方法遍历文档的每个部分。
- 使用Document.getSections().get(index).deepClone()方法将节克隆到节对象中。
- 创建一个新文档并使用Document.getSections().add(Section)方法将克隆的部分添加到文档中。
- 使用Document.save(String)方法保存文档。
以下代码示例显示了如何使用 Java 按部分拆分 Word 文档。
// Load a Word DOCX documentDocument doc = new Document("word.docx");for (int i = 0; i < doc.getSections().getCount(); i++) {// Split a document into smaller parts, in this instance split by sectionSection section = doc.getSections().get(i).deepClone();// Create a new documentDocument newDoc = new Document();newDoc.getSections().clear();// Add sectionSection newSection = (Section) newDoc.importNode(section, true);newDoc.getSections().add(newSection);// Save each section as a separate documentnewDoc.save("splitted_" + i + ".docx");}
以上便是使用 Java 拆分 MS Word 文档详细步骤,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。
欢迎下载|体验更多Aspose产品
点此获取更多Aspose产品信息 或 加入Aspose技术交流群()
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!