本文介绍使用spire.doc for .net读取word文本框的方法。可读取文本框中的文本、图片和表格等,读取表格请参考这篇文章。文本内容位为读取文本和图片。
用于测试的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("sample.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("sample.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("sample.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("sample.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
图片读取结果: