0
votes

I want to find all the headings in a word document which is of font size greater than the text below it, all the headings are of same font size else all the headings are bold while the text below it are not bold and I want to save all these headings in a list. How can I achieve this?? I'm learner in Office.Interop.Word any assistance will help me a lot.

1
Do these heading paragraphs have specific Styles applied? Typically headings are styled. If this is the case, refer to question 11327304. "save all these headings in a list" do you mean a list data structure? Or do you mean a Word list (e.g. bulleted, numbereed, multilievel)? This is definitely something we can help with but you need to clear up your question and make it more specific.JohnZaj
What about consecutive 'headings', do these belong in the list as well? What about the 'last' paragraph in the document. Ignore it? (the last paragraph is generally not a heading...but still a corner case)JohnZaj
Ya, though this is not a perfect way to find all headings.. I will be happy if I partially succeeded in finding the headings of a documentDolo

1 Answers

0
votes

Loop through each paragraph and if the paragraph meets your requirements (which you should clarify) then add it to a list (which you should also clarify whether a list structure such as List<> or a list in a word document such as ListParagraph). If this code doesn't help please say so and clarify your question:

foreach (MSWord.Paragraph paragraph in doc.Paragraphs)
{
    if (paragraph.Next() != null)
    {
        if (IsHeading(paragraph))
        {
            myList.Add(paragraph.Range.Text.ToString());
        }
    }
}

private static bool IsHeading(MSWord.Paragraph paragraph)
{
    float para1FontSize = 0;
    float para2FontSize = 0;
    bool para1IsBold = false;
    bool para2IsBold = false;

    para1FontSize = paragraph.Range.Font.Size;
    para2FontSize = paragraph.Next().Range.Font.Size;
    para1IsBold = paragraph.Range.Font.Bold.Equals(1);
    para2IsBold = paragraph.Next().Range.Font.Bold.Equals(0);

    return ((para1FontSize > para2FontSize) || (para1IsBold && !para2IsBold));
}