C# 创建可填充Word表单

背景介绍

有时候,我们需要制作一个Word模板文档,然后发给用户填写,但我们希望用户只能在指定位置填写内容,其他内容不允许编辑和修改。这时候我们就可以通过表单控件来轻松实现这一功能。
本文将介绍如何使用C#在Word文档中创建可填充的Word表单。

使用工具

• Visual Studio
Spire.Doc for .NET组件

在添加以下代码前,需要下载Spire.Doc组件,并从安装路径下的bin文件夹中引用Spire.Doc.dll到程序中。

代码

在Word中,表单控件主要分为两种:

• 旧式窗体域
• 内容控件 (Word 2010及以后版本)

下面看看如何使用Spire.Doc添加旧式窗体域和内容控件到Word模板文档。

添加旧式窗体域

Word 2007及以前的版本中是旧式窗体域。旧式窗体域分为:文本型窗体域、复选框型窗体域和下拉型窗体域。

下面的代码创建了一个Word文档,然后添加了一个表格,并给表格添加文本型、复选框型和下拉型窗体域,最后保护Word文档。

{
//创建Document实例
Document doc = new Document();
//添加一个section
Section section = doc.AddSection();

//标题
Paragraph title = section.AddParagraph();
TextRange titleText = title.AppendText("职位申请表");
titleText.CharacterFormat.FontName = "宋体";
titleText.CharacterFormat.FontSize = 16f;
title.Format.HorizontalAlignment = HorizontalAlignment.Center;

//添加一个7行2列的表格
Table table = section.AddTable(true);
table.ResetCells(7, 2);

//合并首行的单元格
table.ApplyHorizontalMerge(0, 0, 1);

//设置表头
TableRow headerRow = table.Rows[0];
headerRow.IsHeader = true;
headerRow.RowFormat.BackColor = Color.FromArgb(0x00, 0x71, 0xb6);
headerRow.Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
Paragraph headerParagraph = headerRow.Cells[0].AddParagraph();
TextRange headerText = headerParagraph.AppendText("第一部分、个人信息");
headerText.CharacterFormat.Bold = true;

//添加段落到单元格[1,0]
Paragraph paragraph = table.Rows[1].Cells[0].AddParagraph();
TextRange textRange = paragraph.AppendText("姓名");

//添加文本型窗体到单元格[1,1]
paragraph = table.Rows[1].Cells[1].AddParagraph();
AddTextFormField(paragraph, "Name");

//添加段落到单元格[2,0]
paragraph = table.Rows[2].Cells[0].AddParagraph();
textRange = paragraph.AppendText("年龄");

//添加文本型窗体到单元格[2,1]
paragraph = table.Rows[2].Cells[1].AddParagraph();
AddTextFormField(paragraph, "Age");

//添加段落到单元格[3,0]
paragraph = table.Rows[3].Cells[0].AddParagraph();
textRange = paragraph.AppendText("婚否");

//添加复选框型窗体到单元格[3,1]
paragraph = table.Rows[3].Cells[1].AddParagraph();
AddCheckBoxFormField(paragraph, "Married");

//添加段落到单元格[4,0]
paragraph = table.Rows[4].Cells[0].AddParagraph();
textRange = paragraph.AppendText("专业");

//添加下拉型窗体到单元格[4,1]
paragraph = table.Rows[4].Cells[1].AddParagraph();
AddDropDownFormField(paragraph, "Major");

//添加段落到单元格[5,0]
paragraph = table.Rows[5].Cells[0].AddParagraph();
textRange = paragraph.AppendText("申请职位");

//添加文本型窗体到单元格[5,1]
paragraph = table.Rows[5].Cells[1].AddParagraph();
AddTextFormField(paragraph, "Position");

//添加段落到单元格[6,0]
paragraph = table.Rows[6].Cells[0].AddParagraph();
textRange = paragraph.AppendText("申请理由");

//添加文本型窗体到单元格[6,1]
paragraph = table.Rows[6].Cells[1].AddParagraph();
AddTextFormField(paragraph, "Reason");

//创建段落样式
ParagraphStyle style = new ParagraphStyle(doc);
style.Name = "style";
style.CharacterFormat.FontName = "宋体";
style.CharacterFormat.FontSize = 11f;
doc.Styles.Add(style);

for (int i = 0; i < table.Rows.Count; i++)
{
    //设置表格行高
    table.Rows[i].Height = 20f;
    for (int j = 0; j < table.Rows[i].Cells.Count; j++)
    {
        //设置单元格文本垂直对齐方式
        table[i, j].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
        //设置单元格的宽度,即列宽
        table[i, j].Width = 200f;
        foreach (Paragraph para in table[i, j].Paragraphs)
        {
            //应用段落样式
            para.ApplyStyle(style.Name);
        }
    }
}

//设置表格居中排列
table.TableFormat.HorizontalAlignment = RowAlignment.Center;

//保护文档,并设置模式为仅允许编辑表单域
doc.Protect(ProtectionType.AllowOnlyFormFields, "123");

//保存
doc.SaveToFile("AddFormFields.docx", FileFormat.Docx2013);
}

//添加文本型窗体、复选框型窗体和下拉型窗体的方法如下:
//添加文本型窗体
static void AddTextFormField(Paragraph paragraph, string fieldName)
{
    TextFormField textForm = paragraph.AppendField(fieldName, FieldType.FieldFormTextInput) as TextFormField;           
    textForm.DefaultText = "";
    textForm.Text = "";
}

//添加复选框型窗体
static void AddCheckBoxFormField(Paragraph paragraph, string fieldName)
{
    CheckBoxFormField checkBoxForm = paragraph.AppendField(fieldName,    FieldType.FieldFormCheckBox) as CheckBoxFormField;
    checkBoxForm.SizeType = CheckBoxSizeType.Exactly;
    checkBoxForm.CheckBoxSize = 8;
}

//添加下拉型窗体
static void AddDropDownFormField(Paragraph paragraph, string fieldName) 
{
    DropDownFormField dropDownForm = paragraph.AppendField(fieldName, FieldType.FieldFormDropDown) as DropDownFormField ;
    dropDownForm.DropDownItems.Add("选择一个专业");
    dropDownForm.DropDownItems.Add("计算机科学与技术");
    dropDownForm.DropDownItems.Add("软件工程");
    dropDownForm.DropDownItems.Add("信息管理");
    dropDownForm.DropDownItems.Add("电子商务");
}
}
}

用户打开下面的生成文档,只能编辑表格中的窗体,不能修改其他内容:
C# 创建可填充Word表单

添加内容控件

Word 2010及以后的版本中添加了内容控件。下面就介绍如何使用Spire.Doc添加内容控件到Word文档。

Spire.Doc支持多种内容控件类型,可在枚举SdtType中查看,如下图所示:
C# 创建可填充Word表单

//创建Document实例 
Document document = new Document();
//添加一个section
Section section = document.AddSection();

//添加段落
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("姓名: ");

//添加纯文本内容控件 
StructureDocumentTagInline sdt = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.Text;
sdt.SDTProperties.Alias = "纯文本";
//设置展示文本
SdtText text = new SdtText(false);
text.IsMultiline = true;
sdt.SDTProperties.ControlProperties = text;
TextRange rt = new TextRange(document);
rt.Text = "姓名";
sdt.SDTContent.ChildObjects.Add(rt);

paragraph.AppendBreak(BreakType.LineBreak);

//添加段落
paragraph = section.AddParagraph();
paragraph.AppendText("性别: ");

//添加下拉列表内容控件
sdt = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.DropDownList;
sdt.SDTProperties.Alias = "下拉列表";
//添加下拉选项
SdtDropDownList sddl = new SdtDropDownList();
sddl.ListItems.Add(new SdtListItem("男", "1"));
sddl.ListItems.Add(new SdtListItem("女", "2"));
sdt.SDTProperties.ControlProperties = sddl;
//设置控件展示的初始选项
rt = new TextRange(document);
rt.Text = sddl.ListItems[1].DisplayText;
sdt.SDTContent.ChildObjects.Add(rt);

paragraph.AppendBreak(BreakType.LineBreak);

//添加段落 
paragraph = section.AddParagraph();
paragraph.AppendText("出生日期: ");

//添加日期选取器内容控件 
sdt = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.DatePicker;
sdt.SDTProperties.Alias = "日期选取器";
//设置日历格式
SdtDate date = new SdtDate();
date.CalendarType = CalendarType.Default;
date.DateFormat = "yyyy.MM.dd";
date.FullDate = DateTime.Now;
sdt.SDTProperties.ControlProperties = date;
//设置展示日期
rt = new TextRange(document);
rt.Text = "1991.02.08";
sdt.SDTContent.ChildObjects.Add(rt);

paragraph.AppendBreak(BreakType.LineBreak);

//添加段落
paragraph = section.AddParagraph();
paragraph.AppendText("国籍: ");

//添加组合框内容控件  
sdt = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sdt);
sdt.SDTProperties.SDTType = SdtType.ComboBox;
sdt.SDTProperties.Alias = "组合框";
//添加选项
SdtComboBox cb = new SdtComboBox();
cb.ListItems.Add(new SdtListItem("中国", "1"));
cb.ListItems.Add(new SdtListItem("英国", "2"));
cb.ListItems.Add(new SdtListItem("意大利", "3"));
sdt.SDTProperties.ControlProperties = cb;
//设置展示选项
rt = new TextRange(document);
rt.Text = cb.ListItems[0].DisplayText;
sdt.SDTContent.ChildObjects.Add(rt);

paragraph.AppendBreak(BreakType.LineBreak);

//创建段落样式
ParagraphStyle style = new ParagraphStyle(document);
style.Name = "style";
style.CharacterFormat.FontName = "宋?体¬?";
style.CharacterFormat.FontSize = 11f;
document.Styles.Add(style);

//应用段落样式
foreach(Paragraph para in section.Paragraphs)
{
para.ApplyStyle(style.Name);
}

//保护文档,仅允许修改表单
document.Protect(ProtectionType.AllowOnlyFormFields, "123");

//保存          
document.SaveToFile("ContentControls.docx", FileFormat.Docx2013);

生成文档:
C# 创建可填充Word表单

猜你喜欢

转载自blog.51cto.com/13688031/2310656