• Word控件Spire.Doc 【表单域】教程(二):在 C# 中填写 Word 文档中的表单字段


    我们已经演示了如何创建表单域。本文主要向您展示开发人员如何使用独立的.NET Word 组件 Spire.Doc仅通过 4 个简单的步骤在 C# 中填写 Word 文档中的表单字段。

    Spire.Doc for.NET 最新下载 

    欢迎下载|体验更多E-iceblue产品 技术交流Q群(767755948)

    确保 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 { }
    }
    
    }
    }
  • 相关阅读:
    【华为机试真题 Python实现】火星文计算【2022 Q1 Q2 | 100分】
    在 Azure ML 上用 .NET 跑机器学习
    影响关系分类汇总
    @Component,@ComponentScan,@MapperScan注解
    区块链实验室(24) - FISCO网络重构
    20_ue4进阶末日生存游戏开发[AI基础框架搭建]
    Etsy玩家必看之7个运营技巧
    SAP-ABAP-SELECT语法SQL语法详解
    该微信用户未开启“公众号安全助手”的消息接收功能,请先开启后再绑定
    简单搭建redis哨兵集群
  • 原文地址:https://blog.csdn.net/m0_67129275/article/details/126007897