提取 Word 文档内容在工作和学习中扮演着重要角色。抽取一页内容有助于快速浏览和总结要点,而提取一个节的内容则有助于深入研究特定主题或部分内容。全文提取则使您能够全面了解文档内容,促进深入分析和全面理解。本文将介绍如何使用 Spire.Doc for Java 在 Java 从 Word 文档中提取单页内容、单个节的内容以及整篇文档的内容。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从此链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.10.6</version>
    </dependency>
</dependencies>
Java 从 Word 文档中读取一页的内容
使用 FixedLayoutDocument 类和 FixedLayoutPage 类可以方便地提取指定页面的内容。为了更便于查看提取的内容,下面的示例代码将提取的内容保存到一个新的 Word 文档中。详细步骤如下:
- 创建一个 Document 对象。
- 使用 Document.loadFromFile() 方法加载示例 Word 文档。
- 创建一个 FixedLayoutDocument 对象。
- 获取文档中的一个页面的 FixedLayoutPage 对象。
- 通过 FixedLayoutPage.getSection() 方法获取页面所在的节。
- 获取页面第一个段落在节中的索引位置。
- 获取页面最后一个段落在节中的索引位置。
- 创建另一个 Document 对象。
- 使用 Document.addSection() 添加一个新的节。
- 使用 Section.cloneSectionPropertiesTo(newSection); 方法将原始节的属性克隆到新节中。
- 复制原文档中页面的内容到新文档中。
- 使用 Document.saveToFile() 方法保存结果文档。
- Java
import com.spire.doc.*;
import com.spire.doc.pages.*;
import com.spire.doc.documents.*;
public class ReadOnePage {
    public static void main(String[] args) {
        // 创建一个新的文档对象
        Document document = new Document();
        // 从指定文件加载文档内容
        document.loadFromFile("示例.docx");
        // 创建一个固定布局文档对象
        FixedLayoutDocument layoutDoc = new FixedLayoutDocument(document);
        // 获取第一个页面
        FixedLayoutPage page = layoutDoc.getPages().get(0);
        // 获取页面所在的节
        Section section = page.getSection();
        // 获取页面的第一个段落
        Paragraph paragraphStart = page.getColumns().get(0).getLines().getFirst().getParagraph();
        int startIndex = 0;
        if (paragraphStart != null)
        {
            // 获取段落在节中的索引
            startIndex = section.getBody().getChildObjects().indexOf(paragraphStart);
        }
        // 获取页面的最后一个段落
        Paragraph paragraphEnd = page.getColumns().get(0).getLines().getLast().getParagraph();
        int endIndex = 0;
        if (paragraphEnd != null)
        {
            // 获取段落在节中的索引
            endIndex = section.getBody().getChildObjects().indexOf(paragraphEnd);
        }
        // 创建一个新的文档对象
        Document newdoc = new Document();
        // 添加一个新的节
        Section newSection = newdoc.addSection();
        // 克隆原节的属性到新节
        section.cloneSectionPropertiesTo(newSection);
        // 复制原文档中页面内容到新文档
        for (int i = startIndex; i <=endIndex; i++)
        {
            newSection.getBody().getChildObjects().add(section.getBody().getChildObjects().get(i).deepClone());
        }
        // 将新文档保存为指定文件
        newdoc.saveToFile("读取一页的内容.docx", FileFormat.Docx);
        // 关闭并释放新文档
        newdoc.close();
        newdoc.dispose();
        // 关闭并释放原文档
        document.close();
        document.dispose();
    }
}
Java 从 Word 文档中读取一个节的内容
使用 Document.Sections[index] 可以访问特定的节对象,其中包含页眉、页脚和正文内容。以下示例展示了一种简单的方法,将一个节的所有内容复制到另一个文档中。详细步骤如下:
- 创建一个 Document 对象。
- 使用 Document.loadFromFile() 方法加载示例 Word 文档。
- 使用 Document.getSections().get(1) 获取文档的第二个节。
- 创建另一个新的 Document 对象。
- 使用 Document.cloneDefaultStyleTo(newdoc) 方法克隆原始文档的默认样式到新文档。
- 使用 Document.getSections().add(section.deepClone()) 方法将原始文档的第二个节的内容克隆到新文档中。
- 使用 Document.saveToFile() 方法保存结果文档。
- Java
import com.spire.doc.*;
public class ReadOneSection {
    public static void main(String[] args) {
        // 创建一个新的文档对象
        Document document = new Document();
        // 从文件加载Word文档
        document.loadFromFile("示例.docx");
        // 获取文档的第二个节
        Section section = document.getSections().get(1);
        // 创建一个新的文档对象
        Document newdoc = new Document();
        // 将默认样式克隆到新文档
        document.cloneDefaultStyleTo(newdoc);
        // 将第二个节克隆到新文档中
        newdoc.getSections().add(section.deepClone());
        // 将新文档保存为文件
        newdoc.saveToFile("读取一个节的内容.docx", FileFormat.Docx);
        // 关闭并释放新文档对象
        newdoc.close();
        newdoc.dispose();
        // 关闭并释放原始文档对象
        document.close();
        document.dispose();
    }
}
Java 从 Word 文档中读取整个文档的内容
这个示例演示了如何通过遍历原始文档的每个节来读取整个文档的内容,并将每个节克隆到一个新文档中。这种方法可以帮助您快速复制整个文档的结构和内容,使得在新文档中保留原始文档的格式和布局。这样的操作对于保留文档结构的完整性和一致性非常有用。详细步骤如下:
- 创建一个 Document 对象。
- 使用 Document.loadFromFile() 方法加载示例 Word 文档。
- 创建另一个新的 Document 对象。
- 使用 Document.cloneDefaultStyleTo(newdoc) 方法克隆原始文档的默认样式到新文档。
- 使用一个 for 循环遍历原始文档中的每个节并将其克隆到新文档中。
- 使用 Document.saveToFile() 方法保存结果文档。
- Java
import com.spire.doc.*;
public class ReadOneDocument {
    public static void main(String[] args) {
        // 创建一个新的文档对象
        Document document = new Document();
        // 从文件加载Word文档
        document.loadFromFile("示例.docx");
        // 创建一个新的文档对象
        Document newdoc = new Document();
        // 将默认样式克隆到新文档
        document.cloneDefaultStyleTo(newdoc);
        // 遍历原始文档中的每个节并将其克隆到新文档中
        for (Section sourceSection : (Iterable)document.getSections()) {
            newdoc.getSections().add(sourceSection.deepClone());
        }
        // 将新文档保存为文件
        newdoc.saveToFile("读取整个文档的内容.docx", FileFormat.Docx);
        // 关闭并释放新文档对象
        newdoc.close();
        newdoc.dispose();
        // 关闭并释放原始文档对象
        document.close();
        document.dispose();
    }
} 
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
 



 
					



