Word控件Spire.Doc 【文本】教程(5) ;从 Word 文档中的文本框中提取文本

文本框的目的是允许用户输入程序要使用的文本信息。也可以从文本框中提取现有的文本信息。以下指南重点介绍如何通过Spire.Doc for .NET从 C# 中 Word 文档的文本框中提取文本。

Spire.Doc for.NET 最新下载(qun:767755948)icon-default.png?t=M85Bhttps://www.evget.com/product/3368/download

首先,查看word文档中的文本框信息。

其次,下载Spire.Doc 并安装在您的系统上。Spire.Doc 安装干净、专业,并包含在 MSI 安装程序中。

然后通过以下路径在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考:“..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll”。

现在介绍如何从文本框中提取文本的步骤。

第 1 步:从文件中加载一个 word 文档。

Document document = new Document();
document.LoadFromFile(@"..\..\Test.docx");

第 2 步:检查文档中是否存在文本框。

//Verify whether the document contains a textbox or not
if (document.TextBoxes.Count > 0)

第 3 步:初始化 StreamWriter 类以保存接下来要提取的文本

using (StreamWriter sw = File.CreateText("result.txt"))

第 4 步:从文本框中提取文本。

//Traverse the document
foreach (Section section in document.Sections)
{
foreach (Paragraph p in section.Paragraphs)
{
foreach (DocumentObject obj in p.ChildObjects)

//Extract text from paragraph in TextBox
if (objt.DocumentObjectType == DocumentObjectType.Paragraph)
{
sw.Write((objt as Paragraph).Text)
}
//Extract text from Table in TextBox
if (objt.DocumentObjectType == DocumentObjectType.Table)
{
Table table = objt as Table;
ExtractTextFromTables(table, sw);
}
//Extract text from Table
static void ExtractTextFromTables(Table table, StreamWriter sw)
{
for (int i = 0; i < table.Rows.Count; i++)
{
TableRow row = table.Rows[i];
for (int j = 0; j < row.Cells.Count; j++)
{
TableCell cell = row.Cells[j];
foreach (Paragraph paragraph in cell.Paragraphs)
{
sw.Write(paragraph.Text);
}
}
}
}

完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Fields;
using System.IO;
using Spire.Doc.Documents;
namespace ExtractTextFromTextBoxes
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile(@"..\..\Test.docx");

//Verify whether the document contains a textbox or not
if (document.TextBoxes.Count > 0)
{
using (StreamWriter sw = File.CreateText("result.txt"))
{
foreach (Section section in document.Sections)
{
foreach (Paragraph p in section.Paragraphs)
{
foreach (DocumentObject obj in p.ChildObjects)
{
if (obj.DocumentObjectType == DocumentObjectType.TextBox)
{
TextBox textbox = obj as TextBox;
foreach (DocumentObject objt in textbox.ChildObjects)
{
if (objt.DocumentObjectType == DocumentObjectType.Paragraph)
{
sw.Write((objt as Paragraph).Text);
}

if (objt.DocumentObjectType == DocumentObjectType.Table)
{
Table table = objt as Table;
ExtractTextFromTables(table, sw);
}
}
}
}
}
}
}
}
}
static void ExtractTextFromTables(Table table, StreamWriter sw)
{
for (int i = 0; i < table.Rows.Count; i++)
{
TableRow row = table.Rows[i];
for (int j = 0; j < row.Cells.Count; j++)
{
TableCell cell = row.Cells[j];
foreach (Paragraph paragraph in cell.Paragraphs)
{
sw.Write(paragraph.Text);
}
}
}
}
}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值