5

我正在尝试阅读 Word 2007 docx 文档。

该文档在 Word 中看起来不错,但是当我尝试使用我的代码读取 id 时,所有 Run 对象都将 RunProperites 设置为 null。

我最感兴趣的属性是 RunProperies.FontSize,但不幸的是它也是 null,我唯一可以访问的属性是 InnerText。

我的代码如下所示:

using (WordprocessingDocument doc = WordprocessingDocument.Open(filename, true))
{
    MainDocumentPart mainPart = doc.MainDocumentPart;
    IList<Paragraph> paragraphList = doc.MainDocumentPart.Document.Body.Elements<Paragraph>().ToList<Paragraph>();

    foreach (Paragraph pr in paragraphList)
    {   
        IList<Run> runList = pr.Elements<Run>().ToList<Run>();
        foreach (Run r in runList)
        {
            // Some logic
        }
    }
}

我已将我的文档最小化为尽可能简单,它看起来像这样http://dl.dropbox.com/u/204110/test.docx

我有类似的文件,可以很好地阅读。OpenXML SDK 2 中是否可能存在错误?

有没有人遇到过类似的问题?任何帮助将不胜感激。谢谢你!

4

1 回答 1

4

FontSize不是必需元素,RunProperties也不是。对于每次运行,验证r.RunProperties不为空,然后在尝试读取值之前验证r.RunProperties.FontSize不为空。类似于以下内容:

uint fontSize = SOME_DEFAULT_FONT_SIZE;
RunProperties propertiesElement = r.RunProperties;
if (propertiesElement != null) {
  FontSize sizeElement = propertiesElement.FontSize;
    if (sizeElement != null) {
      fontSize = sizeElement.Val.Value;
    }
  }
}

如果您使用 SDK 附带的 DocReflector 工具查看您提供的 docx 文件,您可以看到前 3 次运行指定了字体大小,但第 4 次运行没有。

于 2010-01-28T20:34:45.017 回答