Aspose.Words.Examples.CSharp.Programming_Documents.Working_With_Document.Common.ParagraphsByStyleName C# (CSharp) Method

ParagraphsByStyleName() public static method

public static ParagraphsByStyleName ( Document doc, string styleName ) : ArrayList
doc Document
styleName string
return System.Collections.ArrayList
        public static ArrayList ParagraphsByStyleName(Document doc, string styleName)
        {
            // Create an array to collect paragraphs of the specified style.
            ArrayList paragraphsWithStyle = new ArrayList();
            // Get all paragraphs from the document.
            NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
            // Look through all paragraphs to find those with the specified style.
            foreach (Paragraph paragraph in paragraphs)
            {
                if (paragraph.ParagraphFormat.Style.Name == styleName)
                    paragraphsWithStyle.Add(paragraph);
            }
            return paragraphsWithStyle;
        }
        // ExStart:CommonGenerateDocument

Usage Example

        public static void Run()
        {
            // ExStart:ExtractContentBetweenParagraphStyles
            // The path to the documents directory.
            string   dataDir  = RunExamples.GetDataDir_WorkingWithDocument();
            string   fileName = "TestFile.doc";
            Document doc      = new Document(dataDir + fileName);

            // Gather a list of the paragraphs using the respective heading styles.
            ArrayList parasStyleHeading1 = Common.ParagraphsByStyleName(doc, "Heading 1");
            ArrayList parasStyleHeading3 = Common.ParagraphsByStyleName(doc, "Heading 3");

            // Use the first instance of the paragraphs with those styles.
            Node startPara1 = (Node)parasStyleHeading1[0];
            Node endPara1   = (Node)parasStyleHeading3[0];

            // Extract the content between these nodes in the document. Don't include these markers in the extraction.
            ArrayList extractedNodes = Common.ExtractContent(startPara1, endPara1, false);

            // Insert the content into a new separate document and save it to disk.
            Document dstDoc = Common.GenerateDocument(doc, extractedNodes);

            dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
            dstDoc.Save(dataDir);
            // ExEnd:ExtractContentBetweenParagraphStyles
            Console.WriteLine("\nExtracted content betweenn the paragraph styles successfully.\nFile saved at " + dataDir);
        }