IRONSOFTWAREHOME
提取文本
using IronWord;
using System.Linq;
using System;

// Load docx
WordDocument doc = new WordDocument("multi-paragraph.docx");

// Returns text from the entire file
Console.WriteLine(doc.ExtractText());

// Returns text from the first paragraph only
Console.WriteLine(doc.Paragraphs[0].ExtractText());

// Returns text from the last paragraph only
Console.WriteLine(doc.Paragraphs.Last().ExtractText());
Imports IronWord
Imports System.Linq
Imports System

' Load docx
Dim doc As New WordDocument("multi-paragraph.docx")

' Returns text from the entire file
Console.WriteLine(doc.ExtractText())

' Returns text from the first paragraph only
Console.WriteLine(doc.Paragraphs(0).ExtractText())

' Returns text from the last paragraph only
Console.WriteLine(doc.Paragraphs.Last().ExtractText())
NuGet Download
PM > Install-Package IronWord
提取文本

提取文本

在从文档中提取大量文本时,过程可能低效且耗时,特别是在处理表格和大量段落时。然而,IronWord的ExtractText方法是一个节省时间的解决方案。 它使开发人员能够轻松地提取文档中所有特定数量的文本,消除了额外循环的需要,并简化了对Text属性的访问。 这种方法可以确保开发人员高效工作并节省宝贵时间。

在这个示例中,我们将展示几种使用ExtractText方法的方式,并提高从文档中检索文本时的效率。

从 Docx 文件中提取文本的实用方法

  • using IronWord;
  • WordDocument doc = new WordDocument("multi-paragraph.docx");
  • Console.WriteLine(doc.ExtractText());
  • Console.WriteLine(doc.Paragraphs[0].ExtractText());
  • Console.WriteLine(doc.Paragraphs.Last().ExtractText());

提取文本

using IronWord 库,从 Word 文档中提取文本是一个简单的过程。 我们首先导入库并初始化WordDocument类。 此步骤允许我们加载带有段落的现有文档。然后我们调用ExtractText方法,并将文档的全部文本打印到控制台。

提取特定文本

上面的示例提取了整个文档的文本,但使用 IronWord 库,您可以完全控制提取过程。 如果您只需要特定部分或段落,可以在Paragraphs数组。 作为一个通用列表,该数组可以根据您的要求进行操作,可以通过如上所示调用doc.Paragraphs[0]索引,或使用C#集合的内置数组方法。

在访问Paragraphs的索引时,我们只返回并提取文档第一段的文本,并将其打印到控制台。 随后,我们也可以在Last,只返回并提取文档最后一段的文本。

探索 IronWord API 的高级文本提取功能
在 GitHub 上查看

准备开始了吗?

Nuget Downloads 56,401版本:2026.9刚刚发布

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户