填写 Word 文档中的表单字段
Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
我们已经演示了如何创建表单域。本文主要向您展示开发人员如何使用独立的.NET Word 组件 Spire.Doc仅通过 4 个简单的步骤在 C# 中填写 Word 文档中的表单字段。
确保 Spire.Doc for .NET 已正确安装,然后在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考,路径如下:“..\Spire.Doc\Bin\NET4.0\ Spire.Doc。 dll”。以下是开发人员如何使用 Spire.Doc 填写表单字段的详细信息:
第一步:打开需要填写数据的表格。
[C#]
//Create word document Document document = new Document(@"..\..\..\Data\UserForm.doc");
第 2 步:加载将填写表格的数据。
[C#]
//Fill data from XML file
using (Stream stream = File.OpenRead(@"..\..\..\Data\User.xml"))
{
XPathDocument xpathDoc = new XPathDocument(stream);
XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");
第 3 步:使用加载的数据填写表格。
[C#]
//fill data
foreach (FormField field in document.Sections[0].Body.FormFields)
{
String path = String.Format("{0}/text()", field.Name);
XPathNavigator propertyNode = user.SelectSingleNode(path);
if (propertyNode != null)
{
switch (field.Type)
{
case FieldType.FieldFormTextInput:
field.Text = propertyNode.Value;
break;
case FieldType.FieldFormDropDown:
DropDownFormField combox = field as DropDownFormField;
for(int i = 0; i < combox.DropDownItems.Count; i++)
{
if (combox.DropDownItems[i].Text == propertyNode.Value)
{
combox.DropDownSelectedIndex = i;
break;
}
if (field.Name == "country" && combox.DropDownItems[i].Text == "Others")
{
combox.DropDownSelectedIndex = i;
}
}
break;
case FieldType.FieldFormCheckBox:
if (Convert.ToBoolean(propertyNode.Value))
{
CheckBoxFormField checkBox = field as CheckBoxFormField;
checkBox.Checked = true;
}
break;
}
}
}
}
第 4 步:将文档保存为 XML 或 Microsoft Word 格式的文件。
[C#]
//Save doc file
document.SaveToFile("Sample.doc",FileFormat.Doc);
有效截图:
填写表单域的完整源代码:
[C#]
namespace FillFormField
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//open form
Document document = new Document(@"..\..\..\..\..\..\Data\UserForm.doc");
//load data
using (Stream stream = File.OpenRead(@"..\..\..\..\..\..\Data\User.xml"))
{
XPathDocument xpathDoc = new XPathDocument(stream);
XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");
//fill data
foreach (FormField field in document.Sections[0].Body.FormFields)
{
String path = String.Format("{0}/text()", field.Name);
XPathNavigator propertyNode = user.SelectSingleNode(path);
if (propertyNode != null)
{
switch (field.Type)
{
case FieldType.FieldFormTextInput:
field.Text = propertyNode.Value;
break;
case FieldType.FieldFormDropDown:
DropDownFormField combox = field as DropDownFormField;
for(int i = 0; i < combox.DropDownItems.Count; i++)
{
if (combox.DropDownItems[i].Text == propertyNode.Value)
{
combox.DropDownSelectedIndex = i;
break;
}
if (field.Name == "country" && combox.DropDownItems[i].Text == "Others")
{
combox.DropDownSelectedIndex = i;
}
}
break;
case FieldType.FieldFormCheckBox:
if (Convert.ToBoolean(propertyNode.Value))
{
CheckBoxFormField checkBox = field as CheckBoxFormField;
checkBox.Checked = true;
}
break;
}
}
}
}
//Save doc file.
document.SaveToFile("Sample.doc",FileFormat.Doc);
//Launching the MS Word file.
WordDocViewer("Sample.doc");
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
}
}
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询 ;技术交流Q群(767755948)


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线