Novacode Determine If Word Style Is A Table

2019-07-18 06:14发布

问题:

I need to parse several .docx files and find using Novacode DocX is making this a much easier task. The way I need to parse these documents is from a top-down approach where when I come across a certain "object" ( word table, picture, graphic, equation, ... ) do something specific.

I wrote the following code. Given a document this code will navigate through all the paragraph instances in-order and print out the styles. I noticed that some of these styles ( "Normal" in this case ) are actually associated with a table object.

using Novacode;
using System;

namespace resrap
{
    internal class Program
    {
        private static void Main( string[] args )
        {
            using ( DocX document = DocX.Load( args[0] ) )
            {
                foreach ( var paraType in document.Paragraphs )
                {
                    Console.WriteLine( paraType.StyleName );
                }
            }
        }
    }
}

Is there a way for me to determine if a given paragraph is associated with a Word table? I know how to grab all the tables in the document but since I need to parse the document in order ( and later put the parsed contents in order of sorts ) using something like this isn't that helpful since I don't know where these tables are actually located within the document.

using Novacode;
using System;

namespace resrap
{
    internal class Program
    {
        private static void Main( string[] args )
        {
            using ( DocX document = DocX.Load( args[0] ) )
            {
                for ( int index = 0; index < document.Tables.Count; index++ )
                {
                    var table = document.Tables[index];
                    // do something with table
                }
            }
        }
    }
}

I am unsure if my approach I have ( first code example ) is the way to do this but I will continue to figure this out while I wait for any possible guidance/tips.

回答1:

I figured it out. I had to look at the ParentContainer properties to check for a cell value.