Reading text part under Heading 1, Heading 2, Heading 3.... of a word document using OpenXml sdk RRS feed

  • Question

  • I want to read the text part under Heading 1, Heading 2, Heading 3... etc of a word document using open xml sdk. The reading should be guided by Table of Contents. From the Table of Contents I want to go to the particular heading (mimicing the CTRL + Click) and then read the text under the said heading using Open Xml sdk. Could anyone please help. I am new to Open Xml sdk.
    Please mark as answer if this helps you. Thanks and warm regards Ambar Ray Solution Architect - Microsoft Technologies
    Friday, October 7, 2011 1:33 PM


  • Hi Ambar,


    Thanks for posting in the MSDN Forum.


    According to you description I create a Word document to reproduce your issue. This is my document’s screen shooting.


    In the document, I put 3 bookmarks as anchor and use hyperlink in head line of the table to visit these anchors.


    This is the snippet. I hope it can approach your goal.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    namespace ConsoleApplication10
        class Program
            static void Main(string[] args)
                using (WordprocessingDocument wWordprocessingDocument = WordprocessingDocument.Open(@"C:\********\test.docx", true))
                    MainDocumentPart wMainDocumentPart = wWordprocessingDocument.MainDocumentPart;
                    Document wDocument = wMainDocumentPart.Document;
                    Table wTable = wDocument.Descendants<Table>().First();
                    TableRow wTableRow = wTable.Descendants<TableRow>().First();
                    List<Paragraph> wParagraphList = wDocument.Descendants<Paragraph>().ToList();
                    foreach (TableCell wTableCell in wTableRow.Descendants<TableCell>().ToList())
                        Paragraph wParagraph = wTableCell.Descendants<Paragraph>().First();
                        Hyperlink wHyperlink = wParagraph.Descendants<Hyperlink>().First();
                        FindContent(wParagraphList, wHyperlink.Anchor.Value);
            private static void FindContent(List<Paragraph> wParagraphList, string p)
                Console.WriteLine("Found " + p);
                for(int i = 0; i < wParagraphList.Count;i++)
                    Paragraph wParagraph = wParagraphList[i];
                        BookmarkStart wBookmarkStart = wParagraph.Descendants<BookmarkStart>().First();
                        if (wBookmarkStart.Name.Value.Equals(p))
                            Paragraph wParagraph2 = wParagraphList[i + 1];
                            Run wRun = wParagraph2.Descendants<Run>().FirstOrDefault();
                            Text wText = wRun.Descendants<Text>().FirstOrDefault();
                            Console.WriteLine("Content is: " + wText.Text);

    Have a good day,





    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 11, 2011 3:05 AM