前面我们介绍了如何使用spire.doc 等。该文将介绍如何使用spire.doc for .net 获取word文档中的内容控件属性,如标题,标记,值等。
首先,请查看含有内容控件的word文档及内容控件属性示例图:
获取word文档内容控件及其属性
c#
using system.collections.generic;
using spire.doc.documents;
using spire.doc;
using system;
namespace wordcontentcontrol
{
class program
{
private static void main(string[] args)
{
//加载源文档
document document = new document();
document.loadfromfile("sample.docx");
//获取内容控件
structuretags structuretags = structuretags.getalltags(document);
list 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("***************************");
i = (i 1);
}
console.readkey();
}
private 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 ((paragraph)(obj)).childobjects)
{
if ((pobj.documentobjecttype == documentobjecttype.structuredocumenttaginline))
{
structuretags.taginlines.add(((structuredocumenttaginline)(pobj)));
}
}
}
}
}
return structuretags;
}
public class structuretags
{
private list m_taginlines;
public list taginlines
{
get
{
if ((this.m_taginlines == null))
{
this.m_taginlines = new list();
}
return this.m_taginlines;
}
set
{
this.m_taginlines = value;
}
}
}
}
}
vb.net
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")
dim structuretags as structuretags = getalltags(document)
dim taginlines as list = 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
效果图: