原創|使用教程|編輯:王香|2017-12-27 14:11:11.000|閱讀 700 次
概述:Spire.Doc 是一個MS Word 組件,使用戶可以直接執行各種Word文檔處理任務, 分為.NET、WPF和Silverlight 三個版本,本文介紹了如何通過Spire.Doc在C#,VB.NET的Word中檢索所有TextRanges樣式名稱。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
程序員可能需要確定一段文本的樣式名稱,或者找到文檔中以指定樣式名稱出現的文本,例如“標題1”。本文將展示如何在C#和VB.NET中使用Spire.Doc來檢索應用于Word文檔的樣式名稱。
Step 1: 創建一個文檔實例。
Document doc = new Document();
Step 2: 加載一個示例Word文件。
doc.LoadFromFile("Sample.docx");
Step 3: 遍歷文檔中的所有TextRanges,并通過StyleName屬性獲取樣式名稱。
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); } } } }
結果:
完整代碼:
[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
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn