We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to remove a paragraph and its contents based on Heading styles

Hi Team,

We are using the aspose Total license and is using aspose words for downloading the word documents. We have a requirement such that, it should be able to remove the paragraph and its sub paragraphs based on the heading style. For example, we may need to find a paragraph whose heading style is ‘Heading 3’ and remove all the items under that. We have tried some samples available in your site and use the Remove() method of Paragraph. But, this is not working as expected all the times. It is removing only the heading of the paragraph and the contents of the paragraphs remain same!! Please see my code for this.

Document doc = new Document(mStream);
int i = 1;
DocumentBuilder builder = new DocumentBuilder(doc);
NodeCollection nodes = doc.GetChildNodes(NodeType.Paragraph, true);

        //Get  sub heading style
        List<string> lstSubHeadings = splitString(proposalHeadingStyle.HeadingLevel2);

        foreach (Paragraph para in nodes)
        {
            if (para.ParagraphFormat.IsHeading && lstSubHeadings.Contains(para.ParagraphFormat.StyleName)) // Select sub sections
            {
                // If current subsection is not selected, remove it from the document
                var obj = OriginalInputFiles.Where(x => x.DocumentTitle == para.GetText()).SingleOrDefault();
                if (obj == null)
                    para.Remove();
                i++;
            }
        }

        doc.UpdateFields();
        MemoryStream stream = new MemoryStream();
        doc.Save(stream, SaveFormat.Docx);

        return stream;

How to remove a paragraph and its contents based on Heading styles ? Any immediate help will be highly appreciated!

regards,
Anish

@anishv3,

To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words 18.11 generated output DOCX file showing the undesired behavior
  • Your expected DOCX document showing the correct output. You can create expected document by using MS Word.

As soon as you get these pieces of information ready, we will start further investigation into your issue and provide you more information. Thanks for your cooperation.

aspose.zip (34.6 KB)
Please find the attached files with sample docs. Since its a confidential client document, I can’t provide the exact document. But, the sample I have provided here will give you an idea about the issue.

I’m trying to remove the paragraph and its contents whose heading style is ‘Heading 2’. Only the heading is getting removed correctly. But, as you can see, there are contents under each heading. (There is an icon to expand or collapse the paragraph contents with its title). Hope this helps. Thanks in advance.

@anishv3,

We are working on your query and will get back to you soon.

Any updates on this ?

One more query. I need some shapes to be removed which is staying just above a paragraph. This is for some specific paragraphs only which we are selected to remove. How do we identify the shapes before that paragraphs and delete the same?

@anishv3,

Please spare us some time to investigate this scenario. We will hopefully provide you code to achieve this today.

@anishv3,

As we can see MS Word collapses all the content to the next higher Heading. For example, Heading1 collapses all content in particular section because it is the highest Heading. Heading 7 the lowest one.

You can build logic on the following code to get the desired output:

Document doc = new Document("D:\\temp\\Aspose\\input.docx");

ArrayList list = new ArrayList();
foreach (Paragraph para in doc.FirstSection.Body.Paragraphs)
{
    if (para.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2)
    {
        list.Add(para);

        Node currentNode = para.NextSibling;
        bool isContinue = true;

        while (currentNode != null && isContinue)
        {
            if (currentNode.NodeType.Equals(NodeType.Paragraph))
            {
                Paragraph tempPara = (Paragraph)currentNode;
                if (tempPara.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading2 ||
                    tempPara.ParagraphFormat.StyleIdentifier == StyleIdentifier.Heading1)
                {
                    isContinue = false;
                    continue;
                }
            }

            list.Add(currentNode);
            currentNode = currentNode.NextSibling;
        }
    }
}

foreach (Node node in list)
{
    node.Remove();
}

doc.Save("D:\\temp\\Aspose\\18.11.docx");