我有word文档,每一段都是很长的一行。类似于:
"NameOfSomeSort     ----ASDdASFA---F-TEXT-FASFASFAS----FASFASF"人物
"TEXT"都被强调了。我需要能够分辨,哪些字符在行高,并得到他们的位置索引在这一行。
我可以通过Interoop来完成,但是整个文件要花5-10个小时才能完成。所以我尝试了OpenXML,但是当我循环遍历段落文本时,我无法获得像突出显示这样的文本属性。
发布于 2015-07-29 12:50:25
高亮显示应用于运行(在runProperties中) (https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.highlight(v=office.14).aspx )
如果您的文本是"aaaaa,我是高亮的bbbb“,openxml将类似于
<w:Paragraph>
  <w:Run><w:Text>aaaaa</w:Text></w:Run>
  <w:Run>
    <w:rPr>
      <w:highlight w:val="yellow" />
    </w:rPr>
    <w:Text>[i am highlight]</w:Text>
  </w:Run>
  <w:Run><w:Text>bbbb</w:Text></w:Run>  
</w:Paragraph>因此,要找到文本是突出显示,您必须使用类似于Paragraph.Descendants<Highlight>()的内容搜索突出标记。
如果需要检索位置,可以使用一些算法,如
// Suppose you have the paragraph p you want to inspec and the run r containing highlight
int pos = 0;
OpenXmlElement oxe = null;
// From the run search for the parent (Paragraph p)
// Add the length of previous text in pos
while ((oxe = r.previousSibling()) != p)
{
  pos += ((Run)oxe).Innertext.Length;
}
// here pos should return where the highlight begin (maybe it's pos+1...)https://stackoverflow.com/questions/31638523
复制相似问题