Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1621479
  • 博文数量: 95
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 941
  • 用 户 组: 普通用户
  • 注册时间: 2017-10-27 10:00
  • 认证徽章:
文章分类

全部博文(95)

文章存档

2019年(41)

2018年(45)

2017年(9)

我的朋友

分类: C#/.net

2019-08-22 11:49:33

在Word中,借助内容控件,可设计具有特定功能的文档或模板。以下表格中简单罗列了几种常用的内容控件。

名称

下拉列表内容控件

下拉列表包含了一个预先定义好的列表。和组合框不同的是下拉列表不允许用户编辑项

纯文本内容控件

纯文本内容控件只能包含文本,不能包含其他项,例如表格、图片或其他内容控件

富文本内容控件

与纯文本内容控件不同,富文本内容控件可以包含除文本以外的其他项,例如表格、图片或其他内容控件

日期选取器内容控件

日期选取器内容控件包含一个日历控件,用于帮助用户输入日期

组合框内容控件

组合框控件包含一个可以直接编辑的列表。它结合了文本框和下拉列表的属性,用户可以在框中键入值或者从下拉列表中选择值

图片内容控件

图片内容控件用于显示图片。用户可以在制作模板时指定图片,也可以通过单击此控件来选择需要插入的图片















本篇文章中介绍通过C# 来添加以上几种内容控件的添加方法。同时,针对Word文档中已有的控件,也可以读取控件名及控件中的内容。


使用工具:Spire.Doc for .NET

Dll文件获取及导入:

方法1通过官网下载dll文件包。下载后,解压安装。完成安装后,注意在vs程序中添加引用Spire.Doc.dll程序集文件。如下图:


方法2:可通过Nuget网站下载。


C# 代码示例

【示例1】添加内容控件到Word

  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using Spire.Doc.Fields;
  4. using System;
  5. using System.Drawing;

  6. namespace Addsdt
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             //创建word文档
  13.             Document document = new Document();
  14.             Section section = document.AddSection();
  15.             Paragraph paragraph = section.AddParagraph();

  16.             //实例化StructureDocumentTagInline类的对象
  17.             StructureDocumentTagInline sdt = new StructureDocumentTagInline(document);


  18.             //添加下拉列表内容控件
  19.             paragraph.ChildObjects.Add(sdt);
  20.             sdt.SDTProperties.SDTType = SdtType.DropDownList;
  21.             //为控件设置标题和标签
  22.             sdt.SDTProperties.Alias = "下拉列表";
  23.             sdt.SDTProperties.Tag = "下拉列表";
  24.             //添加下拉选项
  25.             SdtDropDownList sddl = new SdtDropDownList();
  26.             sddl.ListItems.Add(new SdtListItem("男", "1"));
  27.             sddl.ListItems.Add(new SdtListItem("女", "2"));
  28.             sdt.SDTProperties.ControlProperties = sddl;
  29.             //设置控件显示的初始选项
  30.             TextRange rt = new TextRange(document);
  31.             rt.Text = sddl.ListItems[0].DisplayText;
  32.             Spire.Doc.Formatting.CharacterFormat format = new Spire.Doc.Formatting.CharacterFormat(document);
  33.             format.FontName = "宋体";
  34.             rt.ApplyCharacterFormat(format);
  35.             sdt.SDTContent.ChildObjects.Add(rt);


  36.             //添加纯文本内容控件
  37.             paragraph = section.AddParagraph();
  38.             sdt = new StructureDocumentTagInline(document);
  39.             paragraph.ChildObjects.Add(sdt);
  40.             sdt.SDTProperties.SDTType = SdtType.Text;
  41.             //为控件设置标题和标签
  42.             sdt.SDTProperties.Alias = "纯文本";
  43.             sdt.SDTProperties.Tag = "纯文本";
  44.             //设置显示文本
  45.             SdtText text = new SdtText(false);
  46.             text.IsMultiline = true;
  47.             sdt.SDTProperties.ControlProperties = text;
  48.             rt = new TextRange(document);
  49.             rt.Text = "此处只能输入纯文本";
  50.             rt.ApplyCharacterFormat(format);
  51.             sdt.SDTContent.ChildObjects.Add(rt);


  52.             //添加富文本内容控件
  53.             paragraph = section.AddParagraph();
  54.             sdt = new StructureDocumentTagInline(document);
  55.             paragraph.ChildObjects.Add(sdt);
  56.             sdt.SDTProperties.SDTType = SdtType.RichText;
  57.             //为控件设置标题和标签
  58.             sdt.SDTProperties.Alias = "富文本";
  59.             sdt.SDTProperties.Tag = "富文本";
  60.             //设置显示文本
  61.             SdtText richText = new SdtText(true);
  62.             richText.IsMultiline = true;
  63.             sdt.SDTProperties.ControlProperties = richText;
  64.             rt = new TextRange(document);
  65.             rt.Text = "可输入格式化的文本、表格、图片等";
  66.             rt.ApplyCharacterFormat(format);
  67.             sdt.SDTContent.ChildObjects.Add(rt);


  68.             //添加日期选取器内容控件
  69.             paragraph = section.AddParagraph();
  70.             sdt = new StructureDocumentTagInline(document);
  71.             paragraph.ChildObjects.Add(sdt);
  72.             sdt.SDTProperties.SDTType = SdtType.DatePicker;
  73.             //为控件设置标题和标签
  74.             sdt.SDTProperties.Alias = "日期选取器";
  75.             sdt.SDTProperties.Tag = "日期选取器";
  76.             //设置日历模式
  77.             SdtDate date = new SdtDate();
  78.             date.CalendarType = CalendarType.Default;
  79.             date.DateFormat = "yyyy.MM.dd";
  80.             date.FullDate = DateTime.Now;
  81.             sdt.SDTProperties.ControlProperties = date;
  82.             //设置显示日期
  83.             rt = new TextRange(document);
  84.             rt.Text = "2019.08.22";
  85.             rt.ApplyCharacterFormat(format);
  86.             sdt.SDTContent.ChildObjects.Add(rt);


  87.             //添加组合框内容控件
  88.             paragraph = section.AddParagraph();
  89.             sdt = new StructureDocumentTagInline(document);
  90.             paragraph.ChildObjects.Add(sdt);
  91.             sdt.SDTProperties.SDTType = SdtType.ComboBox;
  92.             //为控件设置标题和标签
  93.             sdt.SDTProperties.Alias = "组合框";
  94.             sdt.SDTProperties.Tag = "组合框";
  95.             //添加选项
  96.             SdtComboBox cb = new SdtComboBox();
  97.             cb.ListItems.Add(new SdtListItem("中国", "1"));
  98.             cb.ListItems.Add(new SdtListItem("日本", "2"));
  99.             cb.ListItems.Add(new SdtListItem("意大利", "3"));
  100.             sdt.SDTProperties.ControlProperties = cb;
  101.             //添加显示文本
  102.             rt = new TextRange(document);
  103.             rt.Text = cb.ListItems[0].DisplayText;
  104.             rt.ApplyCharacterFormat(format);
  105.             sdt.SDTContent.ChildObjects.Add(rt);


  106.             //添加复选框内容控件
  107.             paragraph = section.AddParagraph();
  108.             sdt = new StructureDocumentTagInline(document);
  109.             paragraph.ChildObjects.Add(sdt);
  110.             sdt.SDTProperties.SDTType = SdtType.CheckBox;
  111.             //为控件设置标题和标签
  112.             sdt.SDTProperties.Alias = "复选框";
  113.             sdt.SDTProperties.Tag = "复选框";
  114.             SdtCheckBox scb = new SdtCheckBox();
  115.             sdt.SDTProperties.ControlProperties = scb;
  116.             rt = new TextRange(document);
  117.             rt.ApplyCharacterFormat(format);
  118.             sdt.ChildObjects.Add(rt);
  119.             scb.Checked = false;

  120.             //添加图片内容控件
  121.             paragraph = section.AddParagraph();
  122.             sdt = new StructureDocumentTagInline(document);
  123.             paragraph.ChildObjects.Add(sdt);
  124.             sdt.SDTProperties.ControlProperties = new SdtPicture();
  125.             //为控件设置标题和标签
  126.             sdt.SDTProperties.Alias = "图片";
  127.             sdt.SDTProperties.Tag = "图片";
  128.             //添加图片
  129.             DocPicture pic = new DocPicture(document);
  130.             pic.WidthScale = 30f;
  131.             pic.HeightScale = 30f;
  132.             pic.LoadImage(Image.FromFile("nz.png"));
  133.             sdt.SDTContent.ChildObjects.Add(pic);

  134.             //保存文档
  135.             document.SaveToFile("Controls.docx", FileFormat.Docx);
  136.             System.Diagnostics.Process.Start("Controls.docx");
  137.         }
  138.     }
  139. }

控件添加效果:


点击相应内容,可查看控件添加情况。


【示例2】获取Word中的内容控件

  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;

  6. namespace GetSDT
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             //加载含有内容控件的文档
  13.             Document document = new Document();
  14.             document.LoadFromFile("test.docx");

  15.             //调用StructureTags类获取内容控件列表
  16.             StructureTags structureTags = GetAllTags(document);
  17.             List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;

  18.             //获取内容控件属性并输出到控制台
  19.             for (int i = 0; i < tagInlines.Count; i++)
  20.             {
  21.                 string alias = tagInlines[i].SDTProperties.Alias;
  22.                 string tag = tagInlines[i].SDTProperties.Tag;
  23.                 string value = tagInlines[i].SDTContent.Text;
  24.                 Console.WriteLine(alias);
  25.                 Console.WriteLine(tag);
  26.                 Console.WriteLine(value);
  27.                 Console.WriteLine("_____________________");
  28.            
  29.             }
  30.             Console.ReadLine();
  31.         }
  32.         static StructureTags GetAllTags(Document document)
  33.         {
  34.             //遍历文档并获取所有的内容控件
  35.             StructureTags structureTags = new StructureTags();
  36.             foreach (Section section in document.Sections)
  37.             {
  38.                 foreach (DocumentObject obj in section.Body.ChildObjects)
  39.                 {
  40.                     if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
  41.                     {
  42.                         foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
  43.                         {
  44.                             if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
  45.                             {
  46.                                 structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
  47.                             }
  48.                         }
  49.                     }
  50.                 }
  51.             }
  52.             return structureTags;
  53.         }
  54.         public class StructureTags
  55.         {
  56.             List<StructureDocumentTagInline> m_tagInlines;
  57.             public List<StructureDocumentTagInline> tagInlines
  58.             {
  59.                 get
  60.                 {
  61.                     if (m_tagInlines == null)
  62.                         m_tagInlines = new List<StructureDocumentTagInline>();
  63.                     return m_tagInlines;
  64.                 }
  65.                 set
  66.                 {
  67.                     m_tagInlines = value;
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }

读取效果:




(本文完)

阅读(102520) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册