C# 读取Word文本框中的文本、图片和表格(附VB.NET代码)
【概述】
Word中可插入文本框,在文本框中可添加文本、图片、表格等内容。本篇文章通过C#程序代码介绍如何来读取文本框中的文本、图片和表格等内容。附VB.NET代码,有需要可作参考。
【程序环境】
程序中所需必要的程序集文件Spire.Doc.dll,及其他相关dll文件(见下文)。
用于测试的Word源文档如图:
【程序代码】
1.读取文本框中的文本
所需程序集:
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.IO;
using System.Text;
namespace ExtractText
{
class Program
{
static void Main(string[] args)
{
//加载Word源文档
Document doc = new Document();
doc.LoadFromFile("test.docx");
//获取文本框
TextBox textbox = doc.TextBoxes[0];
//创建StringBuilder类的对象
StringBuilder sb = new StringBuilder();
//遍历文本框中的对象,获取文本
foreach (object obj in textbox.Body.ChildObjects)
{
if (obj is Paragraph)
{
String text = ((Paragraph)obj).Text;
sb.AppendLine(text);
}
}
//保存写入的txt文档到指定路径
File.WriteAllText("ExtractedText.txt", sb.ToString());
System.Diagnostics.Process.Start("ExtractedText.txt");
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.IO
Imports System.Text
Namespace ExtractText
Class Program
Private Shared Sub Main(args As String())
'加载Word源文档
Dim doc As New Document()
doc.LoadFromFile("test.docx")
'获取文本框
Dim textbox As TextBox = doc.TextBoxes(0)
'创建StringBuilder类的对象
Dim sb As New StringBuilder()
'遍历文本框中的对象,获取文本
For Each obj As Object In textbox.Body.ChildObjects
If TypeOf obj Is Paragraph Then
Dim text As [String] = DirectCast(obj, Paragraph).Text
sb.AppendLine(text)
End If
Next
'保存写入的txt文档到指定路径
File.WriteAllText("ExtractedText.txt", sb.ToString())
System.Diagnostics.Process.Start("ExtractedText.txt")
End Sub
End Class
End Namespace
文本读取结果:
2.读取文本框中的图片
所需程序集:
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
namespace ExtractImg
{
class Program
{
static void Main(string[] args)
{
//加载Word源文档
Document doc = new Document();
doc.LoadFromFile("test.docx");
//获取文本框
TextBox textbox = doc.TextBoxes[0];
int index = 0 ;
//遍历文本框中所有段落
for (int i = 0 ; i < textbox.Body.Paragraphs.Count;i++)
{
Paragraph paragraph = textbox.Body.Paragraphs[i];
//遍历段落中的所有子对象
for (int j = 0; j < paragraph.ChildObjects.Count; j++)
{
object obj = paragraph.ChildObjects[j];
//判定对象是否为图片
if (obj is DocPicture)
{
//获取图片
DocPicture picture = (DocPicture) obj;
String imageName = String.Format("Image-{0}.png", index);
picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);
index++;
}
}
}
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace ExtractImg
Class Program
Private Shared Sub Main(args As String())
'加载Word源文档
Dim doc As New Document()
doc.LoadFromFile("test.docx")
'获取文本框
Dim textbox As TextBox = doc.TextBoxes(0)
Dim index As Integer = 0
'遍历文本框中所有段落
For i As Integer = 0 To textbox.Body.Paragraphs.Count - 1
Dim paragraph As Paragraph = textbox.Body.Paragraphs(i)
'遍历段落中的所有子对象
For j As Integer = 0 To paragraph.ChildObjects.Count - 1
Dim obj As Object = paragraph.ChildObjects(j)
'判定对象是否为图片
If TypeOf obj Is DocPicture Then
'获取图片
Dim picture As DocPicture = DirectCast(obj, DocPicture)
Dim imageName As [String] = [String].Format("Image-{0}.png", index)
picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)
index += 1
End If
Next
Next
End Sub
End Class
End Namespace
图片读取结果:
3.读取文本框中的表格
所需程序集:
C#
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;
using System.Text;
namespace ExtractTable
{
class Program
{
static void Main(string[] args)
{
//加载Word文档
Document doc = new Document();
doc.LoadFromFile("test.docx");
//获取文本框
TextBox textbox = doc.TextBoxes[0];
//获取文本框中表格
Table table = textbox.Body.Tables[0] as Table;
StringBuilder sb = new StringBuilder();
//遍历表格中的段落并提取文本
foreach (TableRow row in table.Rows)
{
foreach (TableCell cell in row.Cells)
{
foreach (Paragraph paragraph in cell.Paragraphs)
{
sb.AppendLine(paragraph.Text);
}
}
}
File.WriteAllText("ExtractedTable.txt", sb.ToString());
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.IO
Imports System.Text
Namespace ExtractTable
Class Program
Private Shared Sub Main(args As String())
'加载Word文档
Dim doc As New Document()
doc.LoadFromFile("test.docx")
'获取文本框
Dim textbox As TextBox = doc.TextBoxes(0)
'获取文本框中表格
Dim table As Table = TryCast(textbox.Body.Tables(0), Table)
Dim sb As New StringBuilder()
'遍历表格中的段落并提取文本
For Each row As TableRow In table.Rows
For Each cell As TableCell In row.Cells
For Each paragraph As Paragraph In cell.Paragraphs
sb.AppendLine(paragraph.Text)
Next
Next
Next
File.WriteAllText("ExtractedTable.txt", sb.ToString())
End Sub
End Class
End Namespace
表格数据读取结果:
【总结】
以上是本文关于通过C#程序读取Word中的文本框的方法。所附VB.NET代码供参考。