确保 Spire.Doc for .NET 已正确安装,然后在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考,路径如下:“..Spire.DocBinNET4.0 Spire.Doc。 dll”。以下是开发人员如何使用 Spire.Doc 填写表单字段的详细信息:
第一步:打开需要填写数据的表格。
[C#]
//Create word documentDocument document = new Document(@"......DataUserForm.doc");
第 2 步:加载将填写表格的数据。
[C#]
//Fill data from XML fileusing (Stream stream = File.OpenRead(@"......DataUser.xml")){XPathDocument xpathDoc = new XPathDocument(stream);XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");
第 3 步:使用加载的数据填写表格。
[C#]
//fill dataforeach (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 filedocument.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 formDocument document = new Document(@"............DataUserForm.doc");//load datausing (Stream stream = File.OpenRead(@"............DataUser.xml")){XPathDocument xpathDoc = new XPathDocument(stream);XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");//fill dataforeach (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产品
如需获取更多产品相关信息请咨询在线客服
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!