none
Retrieving numbering value for Headings associated with Styles RRS feed

  • Question

  • Hello

    Our documents autonumber the 'heading1' 'heading2' 'heading3' based on leveling.  My code is extracting those 'paragraphs' which have a style of 'Heading*' and capturing their content using this code.  Can you please advise how I can modify this  code to retrieve the numbering values for those paragraphs which are headings?  Thanks - David

     public void GetHeadings()
            {

                using (Package wdPackage = Package.Open(_fileName, FileMode.Open, FileAccess.Read))
                {
                    PackageRelationship docPackageRelationship =
                        wdPackage.GetRelationshipsByType(documentRelationshipType).FirstOrDefault();

                    if (docPackageRelationship != null)
                    {
                        Uri documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative),
                            docPackageRelationship.TargetUri);
                        PackagePart documentPart = wdPackage.GetPart(documentUri);

                        //  Load the document XML in the part into an XDocument instance.
                        _xDoc = XDocument.Load(System.Xml.XmlReader.Create(documentPart.GetStream()));

                        //  Find the styles part. There will only be one.
                        PackageRelationship styleRelation =
                            documentPart.GetRelationshipsByType(stylesRelationshipType).FirstOrDefault();

                        if (styleRelation != null)
                        {
                            Uri styleUri = PackUriHelper.ResolvePartUri(documentUri, styleRelation.TargetUri);
                            PackagePart stylePart = wdPackage.GetPart(styleUri);

                            //  Load the style XML in the part into an XDocument instance.
                            _styleDoc = XDocument.Load(System.Xml.XmlReader.Create(stylePart.GetStream()));

                        }
                    }
                }

                _defaultParaStyle =
                    (string)(
                        from style in _styleDoc.Root.Elements(_w + "style")
                        where (string)style.Attribute(_w + "type") == "paragraph" &&
                              (string)style.Attribute(_w + "default") == "1"
                        select style
                    ).First().Attribute(_w + "styleId");

                Console.WriteLine("The default style is: {0}", _defaultParaStyle);

                // Following finds all paragraphs in the document and their styles.
                var paragraphs =
                    from para in _xDoc
                                 .Root
                                 .Element(_w + "body")
                                 .Descendants(_w + "p")
                    let styleNode = para
                                    .Elements(_w + "pPr")
                                    .Elements(_w + "pStyle")
                                    .FirstOrDefault()
                    select new
                    {
                        ParagraphNode = para,
                        StyleName = styleNode != null ?
                            (string)styleNode.Attribute(_w + "val") :
                            _defaultParaStyle
                    };

                _paraList.Clear();

                string theStyleName;
                foreach (var p in paragraphs)
                {
                    theStyleName = (string)p.StyleName;
                   //  Console.WriteLine("StyleName:{0}", p.StyleName);
                    _paraList.Add(theStyleName);
                }
              
                // Following retrieves the text of each paragraph.
                var paraWithText =
                    from para in paragraphs
                    select new
                    {
                        ParagraphNode = para.ParagraphNode,
                        StyleName = para.StyleName,
                        Text = para
                               .ParagraphNode
                               .Elements(_w + "r")
                               .Elements(_w + "t")
                               .Aggregate(
                                   new StringBuilder(),
                                   (s, i) => s.Append((string)i),
                                   s => s.ToString()
                               )
                    };
                int paraNo = 0;
                foreach (var p in paraWithText)
                {
                    paraNo++;
                    string z = p.StyleName.ToString();

                        // Console.WriteLine("StyleName:{0}", p.StyleName);
                        if (z.StartsWith("Heading"))
                        {
                            Console.WriteLine("StyleName:{0}", p.StyleName);
                            _documentSection = new SectionEntity();
                            _documentSection.SectionHeader = "" + p.Text;
                            DocumentSections.Add(_documentSection);
                            Debug.WriteLine(@"Count of sections: " + DocumentSections.Count.ToString());

     


    Night Skywatcher a/k/a David Diaz
    Thursday, September 16, 2010 12:21 PM