原創(chuàng)|使用教程|編輯:王香|2017-12-27 14:11:11.000|閱讀 700 次
概述:Spire.Doc 是一個(gè)MS Word 組件,使用戶(hù)可以直接執(zhí)行各種Word文檔處理任務(wù), 分為.NET、WPF和Silverlight 三個(gè)版本,本文介紹了如何通過(guò)Spire.Doc在C#,VB.NET的Word中檢索所有TextRanges樣式名稱(chēng)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
程序員可能需要確定一段文本的樣式名稱(chēng),或者找到文檔中以指定樣式名稱(chēng)出現(xiàn)的文本,例如“標(biāo)題1”。本文將展示如何在C#和VB.NET中使用Spire.Doc來(lái)檢索應(yīng)用于Word文檔的樣式名稱(chēng)。
Step 1: 創(chuàng)建一個(gè)文檔實(shí)例。
Document doc = new Document();
Step 2: 加載一個(gè)示例Word文件。
doc.LoadFromFile("Sample.docx");
Step 3: 遍歷文檔中的所有TextRanges,并通過(guò)StyleName屬性獲取樣式名稱(chēng)。
foreach (Section section in doc.Sections) { foreach (Paragraph paragraph in section.Paragraphs) { foreach (DocumentObject docObject in paragraph.ChildObjects) { if (docObject.DocumentObjectType == DocumentObjectType.TextRange) { TextRange text = docObject as TextRange; Console.WriteLine(text.StyleName); } } } }
結(jié)果:
完整代碼:
[C#]
Document doc = new Document(); doc.LoadFromFile("Sample.docx"); foreach (Section section in doc.Sections) { foreach (Paragraph paragraph in section.Paragraphs) { foreach (DocumentObject docObject in paragraph.ChildObjects) { if (docObject.DocumentObjectType == DocumentObjectType.TextRange) { TextRange text = docObject as TextRange; Console.WriteLine(text.StyleName); } } Console.WriteLine(); } }
[VB.NET]
Document doc = New Document() doc.LoadFromFile("Sample.docx") Dim section As Section For Each section In doc.Sections Dim paragraph As Paragraph For Each paragraph In section.Paragraphs Dim docObject As DocumentObject For Each docObject In paragraph.ChildObjects If docObject.DocumentObjectType = DocumentObjectType.TextRange Then Dim text As TextRange = docObject as TextRange Console.WriteLine(text.StyleName) End If Next Console.WriteLine() Next Next
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn