翻譯|使用教程|編輯:胡濤|2022-05-31 10:10:39.320|閱讀 263 次
概述:本文主要介紹了在 C#、VB.NET 中的 Word 中按樣式名稱(chēng)獲取段落。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
通過(guò)使用Spire.Doc,您不僅可以檢索Word文檔中所有段落的樣式名稱(chēng),還可以獲取具有特定樣式名稱(chēng)的段落。這在您需要獲取標(biāo)題、標(biāo)題 1、副標(biāo)題等中的文本時(shí)尤其有用。
Word 中的段落樣式名稱(chēng) | Spire.Doc 中的段落樣式名稱(chēng) |
標(biāo)題 | 標(biāo)題 |
標(biāo)題 1 | 標(biāo)題1 |
標(biāo)題 2 | 標(biāo)題2 |
標(biāo)題 3 | 標(biāo)題3 |
標(biāo)題 4 | 標(biāo)題3 |
字幕 | 字幕 |
第 1 步:在初始化 Document 對(duì)象時(shí)加載示例 Word 文件。
Document doc = new Document("sample.docx");
第 2 步:瀏覽文檔中的節(jié)和段落,判斷段落樣式名稱(chēng)是否為“Heading1”,如果是,則將段落文本寫(xiě)在屏幕上。
foreach (Section section in doc.Sections) { foreach (Paragraph paragraph in section.Paragraphs) { if (paragraph.StyleName == "Heading1") { Console.WriteLine(paragraph.Text); } } }
輸出:
完整代碼:
[C#]
using Spire.Doc; using Spire.Doc.Documents; using System; namespace GetParagh { class Program { static void Main(string[] args) { Document doc = new Document("sample.docx"); foreach (Section section in doc.Sections) { foreach (Paragraph paragraph in section.Paragraphs) { if (paragraph.StyleName == "Heading1") { Console.WriteLine(paragraph.Text); } } } } } }
[VB.NET]
Imports Spire.Doc Imports Spire.Doc.Documents Namespace GetParagh Class Program Private Shared Sub Main(args As String()) Dim doc As New Document("sample.docx") For Each section As Section In doc.Sections For Each paragraph As Paragraph In section.Paragraphs If paragraph.StyleName = "Heading1" Then Console.WriteLine(paragraph.Text) End If Next Next End Sub End Class End Namespace
歡迎下載|體驗(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