Getting Parent Paragraph


#1

We have a paid version of Aspose. We need to get the parent paragraph of a child paragraph. I have attached a sample document along with this. When we read AsposeSupportHelp.pdf (8.8 KB)

Contacting Aspose Support" paragraph, we want to get 1.01 Project as Parent paragraph.

We tried different ways and the following is one way we tried but it is getting the whole document paragraph. Please advise on how to achieve our use-case.

Thanks for your help

   Paragraph para = new Paragraph(doc);
                 NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
                 foreach (Node n in paragraphs)
                 {
                       Paragraph p = (Paragraph)n;
                       string parentParagraph = p.ParentNode.GetText();// PreviousSibling?.GetText();
                       string childParagraph = p.FirstChild?.GetText();
                       string currentParagraph = p.GetText();
                       
                 }

#2

@filgimathew,

You attached a PDF file; but, please also ZIP and upload your input Word document here for testing. We will then investigate the scenario on our end and provide you code to achieve this. Thanks for your cooperation.


#3

Please see the attached zip file. As mentioned above, when we read a paragraph(for example Contacting Aspose Support), we want to get 1.01 Project as parent paragraohAsposeSupportHelp.zip (12.8 KB)


#4

@filgimathew,

You can build logic on the following code to achieve what you are looking for;

Document doc = new Document("E:\\AsposeSupportHelp\\AsposeSupportHelp.docx");

Paragraph targetPara = null;
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.ToString(SaveFormat.Text).Trim().Contains("Contacting Aspose Support"))
    {
        targetPara = para;
        break;
    }
}

if (targetPara != null)
{
    Paragraph prevPara = (Paragraph) targetPara.PreviousSibling;
    while(targetPara.ParagraphFormat.LeftIndent == prevPara.ParagraphFormat.LeftIndent)
    {
        prevPara = (Paragraph) prevPara.PreviousSibling;
    }

    Console.WriteLine("Parent Paragraph is: [{0}]", prevPara.ToString(SaveFormat.Text).Trim());
}