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

Free Support Forum - aspose.com

Get the previous sibling text which is of Heading - C#

Hi Team,

I want to get the previous text which is of a Heading section.
For a inserted text after heading section, i want to get the previous text value.
Ex: In the attached Test.docx, I have Main Data as my heading and below that i have inserted a new sentence.
Output shall be as:
Previous Text: Main Data
Inserted/Actual Text: Great knowledge of data science will create many jobs in the world.
Type: InsertionTest.docx (14.5 KB)

@sureshkap,
Please check the following code example, showing how to get the text of the previous sibling of the inserted node:

Document doc = new Document("Test.docx");
Paragraph insertedParagraph = (Paragraph)doc.Revisions[0].ParentNode;

Console.WriteLine("Previous Text: " + insertedParagraph.PreviousSibling.GetText());
Console.WriteLine("Inserted Text: " + insertedParagraph.GetText());
Console.WriteLine("Revision type: " + doc.Revisions[0].RevisionType);

Output is:

Previous Text: Main Data
Inserted Text: Great knowledge of data science will create many jobs in the world.
Revision type: Insertion

Hi Team,

But sometimes we may have more than 2 or 3 spaces between the paragraphs in that case how we can get the previous sibling or previous text?

@sureshkap,
In docx document a single line break character is considered as a separate paragraph. If you want to get the previous paragraph with text, please use the following code example:

Paragraph previousParagraphWithText = (Paragraph)insertedParagraph.PreviousSibling;
while (previousParagraphWithText.GetText().Trim().Length == 0)
    previousParagraphWithText = (Paragraph)previousParagraphWithText.PreviousSibling;
Console.WriteLine("Previous Text: " + previousParagraphWithText.GetText());