Read through paragraph and replace with another text in Aspose word in Dot net

Hi Team,

We have a requirement where we have a prefilled word template with multiple paragraphs and we would need to read through each paragraph and replace it with different text and then again replace it different text, delete a paragraph and we may insert a new paragraph in the middle as well. All paragraphs will have a numerical bulletin numbering so when we insert/ delete a new paragraph numbering should be altered accordingly as well.

If this is all feasible in Aspose, kindly share us a sample code to read through each para in a template and replace it with a different text and delete a paragraph and insert a paragraph in the middle.

Thanks,
Karthikeyan

@Karthik_Test_account Sure you can achieve this using Aspose.Words. There are several approaches, which can be used to loop through nodes in the document. For example you can use DocumentVisitor or simply get nodes of the specified type and loop through them:
in.docx (13.3 KB)
out.docx (10.7 KB)

Document doc = new Document(@"C:\Temp\in.docx");

// Get all paragraphs.
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
// Loop through the paragraphs
foreach (Paragraph p in paragraphs)
{
    string text = p.ToString(SaveFormat.Text).Trim();
    Console.WriteLine(text);

    // Lets edit paragraph is text is is "Edit this"
    if (text == "Edit this")
    {
        p.RemoveAllChildren();
        p.AppendChild(new Run(doc, "This is new text of paragraph."));
    }

    // If text is "Add paragraph after this" add new paragraph after it.
    if (text == "Add paragraph after this")
    {
        // Clone the current paragraph to keep formatting.
        Paragraph newPara = (Paragraph)p.Clone(false);
        newPara.AppendChild(new Run(doc, "This is a newly added paragraph."));
        p.ParentNode.InsertAfter(newPara, p);
    }
}

doc.Save(@"C:\Temp\out.docx");

Content editing also can be implemented with several approaches, in the above example, I simply removed old content and put new, also, you can use Range.Replace feature or put content into bookmark or field.

Thanks @alexey.noskov. This helps.

I just need to add one more point in the requirement that I mentioned above. There is one condition while looping through paras that is say there may to 30 paras in entire doc out of this I need to skip a few para in the starting and end of the docs while looping and the content of this para will be dynamic and can’t be same every time. Out of 30 paras I May need to start from para 4 and end with para 25.

Is there a way to dynamically do this through dot net code ? while looping through can we differentiate between a header and the content ? If yes kindly share a sample code for this

Thanks,
Karthikeyan

@Karthik_Test_account Looks like you need to get content between paragraphs based on styles. You can find such example from our documentation and GitHub. Once you get heading paragraphs you can build your logic to skip paragraphs after headings.