Free Support Forum - aspose.com

Removing a particular line using c#

hi, i have c# application where i need to generate templates and append into a final template. in that templates i want to remove some specific lines where it has no data/spaces. I am doing it as doc.Range.Replace("##[Address Line 2]##", " ", false, false);. But i am getting a space but that line is not removing. please help me in regarding this. I have attached the output doc also i need to remove a blank line below address line 1 and address line 3 should come up


Hello

Thanks for your request. In this case you should use IReplacingCallback. Please see the following link for more information:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.ireplacingcallback.html

Please see the following code:

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

doc.Range.Replace(new Regex(Regex.Escape("##[Address Line 2]##")), new RemoveAddressLine(), false);

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

private class RemoveAddressLine : IReplacingCallback

{

///

/// This is called during a replace operation each time a match is found.

/// This method appends a number to the match string and returns it as a replacement string.

///

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

DocumentBuilder builder = new DocumentBuilder(e.MatchNode.Document as Document);

// This is a Run node that contains either the beginning or the complete match.

Node currentNode = e.MatchNode;

builder.MoveTo(currentNode);

builder.CurrentParagraph.Remove();

return ReplaceAction.Replace;

}

}

Best regards,

Thanx for the reply. But Do I need to create the Interface becuase it is saying the interface not found

Hi

Thanks for your inquiry. Most likely, you are using old version of Aspose.Words. In older versions of Aspose.Words you should using ReplaceEvaluator instead of IReplacingCallback. For instance see the following code:

// Open the source document.

Document doc = new Document(@"Test001\in.doc");

doc.Range.Replace(new Regex("test"), new ReplaceEvaluator(MyFieldEvent), false);

// Save output document

doc1.Save(@"Test001\out.doc");

==================================================================

private static ReplaceAction MyFieldEvent(object sender, ReplaceEvaluatorArgs e)

{

// Here you should put your logic.

return ReplaceAction.Skip;

}

Best regards,