我正在尝试阅读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中是否存在错误?
有没有人有类似的问题?任何帮助将不胜感激。 谢谢!
答案 0 :(得分:2)
FontSize 不是必需元素, RunProperties 也不是。对于每次运行,请验证 r.RunProperties 是否为null,然后在尝试读取值之前验证 r.RunProperties.FontSize 是否为null。有点像:
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次运行没有。