C# OpenXML 判断段落是否为隐藏文本

C# OpenXML Determine if paragraph is hidden text

我想知道如何确定一个段落是否有隐藏文本。通过互操作很容易做到,但我似乎无法在 openxml 中弄清楚。非常感谢任何帮助

MainDocumentPart mdp = wordprocessingDocument.MainDocumentPart;
XDocument xDoc = mdp.GetXDocument();
IEnumerable<XElement> = xDoc.Descendants(W.p);

foreach (var paragraph in paragraphs)
{
    // I want to check if this paragraph has hidden text or not, if it does I want to skip over it.
    // if (paragraph.hasHiddenText()) { continue; }
    // else, get the content
    string paraText = paragraph.Descendants(W.t).Select(t => (String)t).StringConcatenate();

}

如果您继续使用 open xml sdk 而不是 linq-to-xml 会更简单:

MainDocumentPart mdp = wordprocessingDocument.MainDocumentPart;

foreach(var paragraph in mdp.Document.Body.Descendants<Paragraph>())
{
    ...
    string paraText = paragraph.Descendants<Run>()
                               .Where(r => r.RunProperties.Vanish != null)
                               .Aggregate("", (text,r) => text += r.InnerText);
}

虽然sdk可能会稍微慢一些,但使用起来更方便。除非你每分钟处理数百个文档,否则我不会担心。