Word控件Spire.Doc 【表单域】教程(一): 在 C# 中的 Word 文档中创建表单域

以下是开发人员如何使用 Spire.Doc 创建 FormField 的详细信息:

  • 下载 Spire.Doc for .NET(或 Spire.Office for .NET)并将其安装在您的系统上。
  • 通过以下路径在下载的 Bin 文件夹中添加 Spire.Doc.dll 作为参考:“..Spire.DocBinNET4.0 Spire.Doc.dll”。

第一步创建word文档。

//Create a word documentDocument document = new Document();

第 2 步将新部分添加到文档中,我们将提供有关表单部分的详细信息。表单中主要包含三个字段:文本、下拉列表和复选框。

//Add new section to documentSection section = document.AddSection();//Add Form to sectionprivate void AddForm(Section section)//add text input fieldTextFormField field= fieldParagraph.AppendField(fieldId, FieldType.FieldFormTextInput) as TextFormField;//add dropdown fieldDropDownFormField list= fieldParagraph.AppendField(fieldId, FieldType.FieldFormDropDown) as DropDownFormField;//add checkbox fieldfieldParagraph.AppendField(fieldId, FieldType.FieldFormCheckBox);

第 3 步将文档保存为 Microsoft Word 格式的文件。

//Save doc filedocument.SaveToFile("Sample.doc",FileFormat.Doc);

有效截图:

创建表单域

创建 FormField 的完整源代码

namespace CreateFormField{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){Document document = new Document();Section section = document.AddSection();//page setupSetPage(section);//insert header and footer.InsertHeaderAndFooter(section);//add titleAddTitle(section);//add formAddForm(section);//protect document, only form fields could be edited.document.Protect(ProtectionType.AllowOnlyFormFields, "e-iceblue");//Save doc file.document.SaveToFile("Sample.doc",FileFormat.Doc);//Launching the MS Word file.WordDocViewer("Sample.doc");}private void SetPage(Section section){//the unit of all measures below is point, 1point = 0.3528 mmsection.PageSetup.PageSize = PageSize.A4;section.PageSetup.Margins.Top = 72f;section.PageSetup.Margins.Bottom = 72f;section.PageSetup.Margins.Left = 89.85f;section.PageSetup.Margins.Right = 89.85f;}private void InsertHeaderAndFooter(Section section){//insert picture and text to headerParagraph headerParagraph = section.HeadersFooters.Header.AddParagraph();DocPicture headerPicture= headerParagraph.AppendPicture(Image.FromFile(@"............DataHeader.png"));//header textTextRange text = headerParagraph.AppendText("Demo of Spire.Doc");text.CharacterFormat.FontName = "Arial";text.CharacterFormat.FontSize = 10;text.CharacterFormat.Italic = true;headerParagraph.Format.HorizontalAlignment= Spire.Doc.Documents.HorizontalAlignment.Right;//borderheaderParagraph.Format.Borders.Bottom.BorderType= Spire.Doc.Documents.BorderStyle.Single;headerParagraph.Format.Borders.Bottom.Space = 0.05F;//header picture layout - text wrappingheaderPicture.TextWrappingStyle = TextWrappingStyle.Behind;//header picture layout - positionheaderPicture.HorizontalOrigin = HorizontalOrigin.Page;headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;headerPicture.VerticalOrigin = VerticalOrigin.Page;headerPicture.VerticalAlignment = ShapeVerticalAlignment.Top;//insert picture to footerParagraph footerParagraph = section.HeadersFooters.Footer.AddParagraph();DocPicture footerPicture= footerParagraph.AppendPicture(Image.FromFile(@"............DataFooter.png"));//footer picture layoutfooterPicture.TextWrappingStyle = TextWrappingStyle.Behind;footerPicture.HorizontalOrigin = HorizontalOrigin.Page;footerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;footerPicture.VerticalOrigin = VerticalOrigin.Page;footerPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom;//insert page numberfooterParagraph.AppendField("page number", FieldType.FieldPage);footerParagraph.AppendText(" of ");footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);footerParagraph.Format.HorizontalAlignment= Spire.Doc.Documents.HorizontalAlignment.Right;//borderfooterParagraph.Format.Borders.Top.BorderType= Spire.Doc.Documents.BorderStyle.Single;footerParagraph.Format.Borders.Top.Space = 0.05F;}private void AddTitle(Section section){Paragraph title = section.AddParagraph();TextRange titleText = title.AppendText("Create Your Account");titleText.CharacterFormat.FontSize = 18;titleText.CharacterFormat.FontName = "Arial";titleText.CharacterFormat.TextColor = Color.FromArgb(0x00, 0x71, 0xb6);title.Format.HorizontalAlignment= Spire.Doc.Documents.HorizontalAlignment.Center;title.Format.AfterSpacing = 8;}private void AddForm(Section section){ParagraphStyle descriptionStyle = new ParagraphStyle(section.Document);descriptionStyle.Name = "description";descriptionStyle.CharacterFormat.FontSize = 12;descriptionStyle.CharacterFormat.FontName = "Arial";descriptionStyle.CharacterFormat.TextColor = Color.FromArgb(0x00, 0x45, 0x8e);section.Document.Styles.Add(descriptionStyle);Paragraph p1 = section.AddParagraph();String text1= "So that we can verify your identity and find your information, "+ "please provide us with the following information. "+ "This information will be used to create your online account. "+ "Your information is not public, shared in anyway, or displayed on this site";p1.AppendText(text1);p1.ApplyStyle(descriptionStyle.Name);Paragraph p2 = section.AddParagraph();String text2= "You must provide a real email address to which we will send your password.";p2.AppendText(text2);p2.ApplyStyle(descriptionStyle.Name);p2.Format.AfterSpacing = 8;//field group label styleParagraphStyle formFieldGroupLabelStyle = new ParagraphStyle(section.Document);formFieldGroupLabelStyle.Name = "formFieldGroupLabel";formFieldGroupLabelStyle.ApplyBaseStyle("description");formFieldGroupLabelStyle.CharacterFormat.Bold = true;formFieldGroupLabelStyle.CharacterFormat.TextColor = Color.White;section.Document.Styles.Add(formFieldGroupLabelStyle);//field label styleParagraphStyle formFieldLabelStyle = new ParagraphStyle(section.Document);formFieldLabelStyle.Name = "formFieldLabel";formFieldLabelStyle.ApplyBaseStyle("description");formFieldLabelStyle.ParagraphFormat.HorizontalAlignment= Spire.Doc.Documents.HorizontalAlignment.Right;section.Document.Styles.Add(formFieldLabelStyle);//add tableTable table = section.AddTable();//2 columns of per rowtable.DefaultColumnsNumber = 2;//default height of row is 20pointtable.DefaultRowHeight = 20;//load form config datausing (Stream stream = File.OpenRead(@"............DataForm.xml")){XPathDocument xpathDoc = new XPathDocument(stream);XPathNodeIterator sectionNodes = xpathDoc.CreateNavigator().Select("/form/section");foreach (XPathNavigator node in sectionNodes){//create a row for field group label, does not copy formatTableRow row = table.AddRow(false);row.Cells[0].CellFormat.BackColor = Color.FromArgb(0x00, 0x71, 0xb6);row.Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;//label of field groupParagraph cellParagraph = row.Cells[0].AddParagraph();cellParagraph.AppendText(node.GetAttribute("name", ""));cellParagraph.ApplyStyle(formFieldGroupLabelStyle.Name);XPathNodeIterator fieldNodes = node.Select("field");foreach (XPathNavigator fieldNode in fieldNodes){//create a row for field, does not copy formatTableRow fieldRow = table.AddRow(false);//field labelfieldRow.Cells[0].CellFormat.VerticalAlignment = VerticalAlignment.Middle;Paragraph labelParagraph = fieldRow.Cells[0].AddParagraph();labelParagraph.AppendText(fieldNode.GetAttribute("label", ""));labelParagraph.ApplyStyle(formFieldLabelStyle.Name);fieldRow.Cells[1].CellFormat.VerticalAlignment = VerticalAlignment.Middle;Paragraph fieldParagraph = fieldRow.Cells[1].AddParagraph();String fieldId = fieldNode.GetAttribute("id", "");switch (fieldNode.GetAttribute("type", "")){case "text"://add text input fieldTextFormField field= fieldParagraph.AppendField(fieldId, FieldType.FieldFormTextInput) as TextFormField;//set default textfield.DefaultText = "";field.Text = "";break;case "list"://add dropdown fieldDropDownFormField list= fieldParagraph.AppendField(fieldId, FieldType.FieldFormDropDown) as DropDownFormField;//add items into dropdown.XPathNodeIterator itemNodes = fieldNode.Select("item");foreach (XPathNavigator itemNode in itemNodes){list.DropDownItems.Add(itemNode.SelectSingleNode("text()").Value);}break;case "checkbox"://add checkbox fieldfieldParagraph.AppendField(fieldId, FieldType.FieldFormCheckBox);break;}}//merge field group row. 2 columns to 1 columntable.ApplyHorizontalMerge(row.GetRowIndex(), 0, 1);}}}private void WordDocViewer(string fileName){try{System.Diagnostics.Process.Start(fileName);}catch { }}}}

欢迎下载|体验更多E-iceblue产品

如需获取更多产品相关信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年3月20日
下一篇 2022年3月20日

相关推荐

发表回复

登录后才能评论