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 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
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!