spire.doc支持获取word文档中段落(paragraph)和文本范围(textrange)的样式,例如标题(title)、标题1(heading 1)、副标题(subtitle)等。当然,我们也可以根据标题样式获取对应的文本。
word 段落样式名称 | spire.doc 中对应的样式名称 |
title | title |
heading 1 | heading1 |
heading 2 | heading2 |
heading 3 | heading3 |
heading 4 | heading3 |
subtitle | subtitle |
本文将展示如何从以下文档中获取2级标题对应的文本。
c#
//创建document对象
document doc = new document();
//加载word文档
doc.loadfromfile(@"c:\users\administrator\desktop\sample.docx");
//遍历章节
foreach (section section in doc.sections)
{
//遍历段落
foreach (paragraph paragraph in section.paragraphs)
{
//判断段落样式是否为heading 2
if (paragraph.stylename == "heading2")
{
//输出标题2对应的文本
system.console.writeline(paragraph.text);
}
}
}
vb.net
'创建document对象
dim doc as document = new document
'加载word文档
doc.loadfromfile("c:\users\administrator\desktop\sample.docx")
'遍历章节
for each section as section in doc.sections
'遍历段落
for each paragraph as paragraph in section.paragraphs
'判断段落样式是否为heading 2
if (paragraph.stylename = "heading2") then
'输出标题2对应的文本
system.console.writeline(paragraph.text)
end if
next
next
结果: