views:

171

answers:

1

Hello,

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!

+2  A: 

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.

Adam Sheehan
DocReflector is great....gave me ideas, to get to solution...Thank You very much!
Daniil Harik