您的位置:首頁>正文

Spire.Doc 教程:在C,VB.NET的Word中檢索所有TextRanges樣式名稱

程式師可能需要確定一段文本的樣式名稱, 或者找到文檔中以指定樣式名稱出現的文本, 例如“標題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 Documentdoc.LoadFromFile("Sample.docx") Dim section As SectionFor 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 NextNext
同類文章
Next Article
喜欢就按个赞吧!!!
点击关闭提示