.NET OpenXML SDK 2 RunProperties 为 Null

发布于 2024-08-19 23:34:46 字数 959 浏览 4 评论 0原文

我正在尝试阅读 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 中是否有可能存在错误?

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

I'm trying to read Word 2007 docx document.

The document looks fine inside Word, but when i try to read id using my code, all Run objects have RunProperites set null.

The property that I'm most interested in is RunProperies.FontSize, but unfortunately it is null as well, the only property I can access is InnerText.

My Code looks like this:

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
        }
    }
}

I've minimized my document to as simple as possible, and it looks like this http://dl.dropbox.com/u/204110/test.docx

I have similar document which is read fine. Is it possible that there is a bug in OpenXML SDK 2?

Has anyone had similar problems? Any help would appreciated.
Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我偏爱纯白色 2024-08-26 23:34:46

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 次运行没有指定字体大小。

FontSize is not a required element, and neither is RunProperties. For each run, verify that r.RunProperties is not null, and then verify that r.RunProperties.FontSize is not null before trying to read the values. Something along the lines of:

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

If you look at the docx file you supplied using the DocReflector tool that comes with the SDK, you can see that the first 3 runs have a font size specified, but the 4th run does not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文