文本框在Word文档中是一个经常使用的输入类控件,可以用于用户输入文本你信息,那么对于存在的文本框中的信息一样可以提取出来,
Spire.Doc for .NET可以快速帮助开发人员实现该功能,下面的文档中包含了多个文本框,事例代码可以对任意文本框中的文本进行提取,具体如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
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);
}
}
}
}
}
}