Replace Multiple Paragraph in word document

Hello,

I want to replace few paragraph in word document with blank text which is start with some text and end with some text.

And it is possible that there are many paragraphs between start and end text. So How can I achieve this functionality using Aspose.Words.

e.g. word contains below text:

Hello world

This is sample word document.

We can easily manipulate word using Aspose.Words.

We now convert this to PDF.

End of document

So I want to replace all the text between “This is sample…” to “to PDF”

Out put after replace is

Hello World

End of document

Thanks

Hi Jignesh,

Thanks for your inquiry. Please refer to the following articles:

https://docs.aspose.com/words/java/find-and-replace/
https://docs.aspose.com/words/java/find-and-replace/

You can also achieve the same using following code:

Document doc = new Document(MyDir + @"in.docx");
int startIndex = 0;
int endIndex = 0;
Node[] paras = doc.GetChildNodes(NodeType.Paragraph, true).ToArray();
for (int i = 0; i < paras.Length; i++)
{
    if (paras[i].ToString(SaveFormat.Text).Contains("This is sample"))
        startIndex = i;
    if (paras[i].ToString(SaveFormat.Text).Contains("to PDF"))
        endIndex = i;
}
while (startIndex <= endIndex)
{
    paras[startIndex].Remove();
    startIndex++;
}
doc.Save(MyDir + @"15.7.0.docx");

I hope, this helps.

Best regards,

Hello Hafeez,

Thanks for your reply. Its really help for me and it works same way as I want but only concern is I need to apply this rules only for first page not in another page. So can you please guide me how it is possible to check above thing for only first page. Currently i add same code in my application and it is works perfectly but it replace text in all the pages.

Thanks,

Hi Jignesh,

Thanks for your inquiry. Could you please attach your input Word document and expected Word document here for testing? We will investigate the issue on our end and provide you more information.

Best regards,