前面我们介绍了如何使用 Spire.Doc for .NET 给 Word 文档添加多种类型的内容控件,如下拉列表内容控件,纯文本内容控件,格式文本内容控件,日期选取器内容控件等。该文将介绍如何使用 Spire.Doc for .NET 获取 Word 文档中的内容控件属性,如标题,标记,值等。
首先,请查看含有内容控件的 Word 文档及内容控件属性示例图:
获取 word 文档内容控件及其属性
- C#
- VB.NET
using System;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Collections.Generic;
namespace WordContentControl
{
class Program
{
static void Main(string[] args)
{
//加载含有内容控件的示例文档
Document document = new Document();
document.LoadFromFile("Sample.docx");
//调用StructureTags类获取内容控件列表
StructureTags structureTags = GetAllTags(document);
List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;
//获取内容控件属性并输出到控制台
for(int i = 0; i< tagInlines.Count; i++)
{
string alias = tagInlines[i].SDTProperties.Alias;
string tag = tagInlines[i].SDTProperties.Tag;
string value = tagInlines[i].SDTContent.Text;
Console.WriteLine(alias);
Console.WriteLine(tag);
Console.WriteLine(value);
Console.WriteLine("***************************");
}
Console.ReadKey();
}
static StructureTags GetAllTags(Document document)
{
//遍历文档并获取所有的内容控件
StructureTags structureTags = new StructureTags();
foreach (Section section in document.Sections)
{
foreach (DocumentObject obj in section.Body.ChildObjects)
{
if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
{
foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
{
if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
{
structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
}
}
}
}
}
return structureTags;
}
public class StructureTags
{
List<StructureDocumentTagInline> m_tagInlines;
public List<StructureDocumentTagInline> tagInlines
{
get
{
if (m_tagInlines == null)
m_tagInlines = new List<StructureDocumentTagInline>();
return m_tagInlines;
}
set
{
m_tagInlines = value;
}
}
}
}
}
Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Collections.Generic
Namespace WordContentControl
Class Program
Private Shared Sub Main(ByVal args() As String)
'加载含有内容控件的示例文档
Dim document As Document = New Document
document.LoadFromFile("Sample.docx")
'调用StructureTags类获取内容控件列表
Dim structureTags As StructureTags = GetAllTags(document)
Dim tagInlines As List(Of StructureDocumentTagInline) = structureTags.tagInlines
'获取内容控件属性并输出到控制台
Dim i As Integer = 0
Do While (i < tagInlines.Count)
Dim alias As String = tagInlines(i).SDTProperties.Alias
Dim tag As String = tagInlines(i).SDTProperties.Tag
Dim value As String = tagInlines(i).SDTContent.Text
Console.WriteLine(alias)
Console.WriteLine(tag)
Console.WriteLine(value)
Console.WriteLine("***************************")
i = (i + 1)
Loop
Console.ReadKey
End Sub
Private Shared Function GetAllTags(ByVal document As Document) As StructureTags
'遍历文档并获取所有的内容控件
Dim structureTags As StructureTags = New StructureTags
For Each section As Section In document.Sections
For Each obj As DocumentObject In section.Body.ChildObjects
If (obj.DocumentObjectType = DocumentObjectType.Paragraph) Then
For Each pobj As DocumentObject In CType(obj,Paragraph).ChildObjects
If (pobj.DocumentObjectType = DocumentObjectType.StructureDocumentTagInline) Then
structureTags.tagInlines.Add(CType(pobj,StructureDocumentTagInline))
End If
Next
End If
Next
Next
Return structureTags
End Function
Public Class StructureTags
Private m_tagInlines As List(Of StructureDocumentTagInline)
Public Property tagInlines As List(Of StructureDocumentTagInline)
Get
If (Me.m_tagInlines Is Nothing) Then
Me.m_tagInlines = New List(Of StructureDocumentTagInline)
End If
Return Me.m_tagInlines
End Get
Set
Me.m_tagInlines = value
End Set
End Property
End Class
End Class
End Namespace
效果图: