翻譯|使用教程|編輯:胡濤|2022-07-27 10:10:32.143|閱讀 174 次
概述:本文主要介紹如何在C#獲取Word文檔中內(nèi)容控件的別名、標(biāo)簽和ID,歡迎查閱!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
內(nèi)容控件為您提供了一種設(shè)計(jì)文檔的方法。當(dāng)您向文檔添加內(nèi)容控件時(shí),該控件由邊框、標(biāo)題和可以向用戶(hù)提供說(shuō)明的臨時(shí)文本標(biāo)識(shí)。根據(jù)微軟的說(shuō)法,內(nèi)容控件主要受益于兩個(gè)功能:
因此,開(kāi)發(fā)者在運(yùn)行時(shí)處理內(nèi)容控件時(shí),需要獲取內(nèi)容控件的屬性。本文說(shuō)明如何通過(guò) Spire.Doc 獲取所有控件及其屬性,包括別名、id 和標(biāo)記。
首先,檢查包含六個(gè)按行和表格分布的內(nèi)容控件的測(cè)試文件。默認(rèn)情況下,如果我們不點(diǎn)擊受保護(hù)的部分,控件的邊框和標(biāo)題不會(huì)出現(xiàn)。
測(cè)試文件:
主要步驟:
第 1 步:創(chuàng)建一個(gè)新的 Word 文檔并加載測(cè)試文件。
第 2 步:創(chuàng)建兩個(gè)列表來(lái)存儲(chǔ)標(biāo)簽,這些標(biāo)簽分別以行和表的形式分布。在這里,每個(gè)內(nèi)容控件都將由標(biāo)簽標(biāo)識(shí)。
第 3 步:使用foreach語(yǔ)句獲取Word文檔中的所有標(biāo)簽。
完整代碼:
static void Main(string[] args) { using (Document document = new Document(@"..\..\TestData\test.docx")) { StructureTags structureTags = GetAllTags(document); List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines; string alias = tagInlines[0].SDTProperties.Alias; decimal id = tagInlines[0].SDTProperties.Id; string tag = tagInlines[0].SDTProperties.Tag; List<StructureDocumentTag> tags = structureTags.tags; alias = tags[0].SDTProperties.Alias; id = tags[0].SDTProperties.Id; tag = tags[0].SDTProperties.Tag; } } 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); } } } else if (obj.DocumentObjectType == DocumentObjectType.Table) { foreach (TableRow row in (obj as Table).Rows) { foreach (TableCell cell in row.Cells) { foreach (DocumentObject cellChild in cell.ChildObjects) { if (cellChild.DocumentObjectType == DocumentObjectType.StructureDocumentTag) { structureTags.tags.Add(cellChild as StructureDocumentTag); } else if (cellChild.DocumentObjectType == DocumentObjectType.Paragraph) { foreach (DocumentObject pobj in (cellChild 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 tagInlines { get { if (m_tagInlines == null) m_tagInlines = new List(); return m_tagInlines; } set { m_tagInlines = value; } } List<StructureDocumentTag> m_tags; public List tags { get { if (m_tags == null) m_tags = new List(); return m_tags; } set { m_tags = value; } } }
效果截圖:
行中的內(nèi)容控件
表格中的內(nèi)容控件
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢(xún) ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn