Free Support Forum - aspose.com

Replacing text in document that contains a new line

Received : 2007/09/17 11:25:41
Message : Hello,

I want to call document.Range.Replace passing replacement text that contains at least a \r. This is throwing a exception. How can I over come this. is there some preprocessing I can do to my replacement text to substitute the ControlChar equilivant for my \r?


This message was posted using Aspose.Live 2 Forum

Hi

Thanks for your request. You can try to use ReplaceEvaluatur to replace this text. See the following example.

public void TestReplaceText_95167()

{

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

Regex regex = new Regex("Text to replace");

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceAction1), true);

doc.Save(@"200_95167_daithi\out.doc");

}

ReplaceAction ReplaceAction1(object sender, ReplaceEvaluatorArgs e)

{

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

builder.MoveTo(e.MatchNode);

builder.Write("Replaced");

int index = e.MatchNode.Document.FirstSection.Body.Paragraphs.IndexOf(builder.CurrentParagraph);

//insert next paragraph content to current paragraph.

foreach (Run run in e.MatchNode.Document.FirstSection.Body.Paragraphs[index + 1].Runs)

{

builder.CurrentParagraph.AppendChild(run);

}

//remove next paragraph

e.MatchNode.Document.FirstSection.Body.Paragraphs[index + 1].Remove();

return ReplaceAction.Replace;

}

I hope that it will help you.

Best regards

Hi,

How do you pass the “Replaced” text into the ReplaceEvaluator? I’m calling the <span style=“font-size: 10pt; font-family: “Courier New”;”>doc.Range.Replace from a static class.

Thanks,
JP

Hi

Thanks for your request. But it is not quite clear for me what you want to do. Please, provide more information then I will try to help you.


Best regards.



Hi,

In your example, you hard coded the replacement text to be “Replaced”.

SV-555:

<span style=“font-size: 10pt; font-family: “Courier New”;”><o:p></o:p>

builder.Write("Replaced");



I want to pass into the ReplaceEvaluator delegate the replacement text to use. I presume I do this with the sender argument of the delegate...
public delegate ReplaceAction ReplaceEvaluator(object sender, ReplaceEvaluatorArgs e);

I just do not know how to do that at the moment. What type is object sender? I hope you can help.

Thanks,
JP

Hi

Thanks for your explanation. You can create a public property and then read the value from this property during replacing. See the following code.

private string _replacement = string.Empty;

public string Replacement

{

get { return _replacement; }

set { _replacement = value; }

}

public void TestReplaceText2_95167()

{

Document doc = new Document(@"221_95167_jp.ogorman\in.doc");

Regex regex = new Regex("Test");

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceAction2), true);

doc.Save(@"221_95167_jp.ogorman\out.doc");

}

ReplaceAction ReplaceAction2(object sender, ReplaceEvaluatorArgs e)

{

e.Replacement = _replacement;

return ReplaceAction.Replace;

}

I hope that it will help you.

Best regards.

Hi,

Thank you for your reply.

I am currently calling the <span style=“font-size: 10pt; font-family: “Courier New”;”>doc.Range.Replace method from within a static helper class so I do not have any public properties. Is there any other way to pass this information into the delegate? What is the “object sender” argument used for?

If there is no other solution I will look at rearranging my code.

Thanks,
JP

Hi

Try to use the following code.

public void TestReplacetext3_95167()

{

TestClass.Replacement = "Replaced";

TestClass.Replace();

}

static class TestClass

{

private static string _replacement = string.Empty;

public static string Replacement

{

get { return _replacement; }

set { _replacement = value; }

}

public static void Replace()

{

Document doc = new Document(@"221_95167_jp.ogorman\in.doc");

Regex regex = new Regex("Test");

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceAction1), true);

doc.Save(@"221_95167_jp.ogorman\out.doc");

}

static ReplaceAction ReplaceAction1(object sender, ReplaceEvaluatorArgs e)

{

e.Replacement = _replacement;

return ReplaceAction.Replace;

}

}

Best regards

Hi,

Thanks for that.

On a final note, I had one or two issues with your original ReplaceEvaluatorHandler. I simplified it to this…

private static ReplaceAction ReplaceEvaluatorHandler(object sender, ReplaceEvaluatorArgs e)
{
e.Replacement = _replacement;
return ReplaceAction.Replace;
}

It appears to work fine for the control characters and keeps the surrounding formatting in my text. Does this seam correct to you or will this code not work in certain scenarios?

Ta,
JP

Hello!

This should work fine in general. I don't see potential problems. But we cannot guarantee everything and everywhere. If you find something strange please let us know.

Regards,

I am facing one problem here :

e.Replacement = _replacement;

I am reading text from xml file. The text contains 2 new lines. _replacement variable is holding this text. When all replacement is done and when I open the generated document, I see that newline chars are replaced with space by Aspose. I think it is wrong behaviour. How to overcome this problem?

Hi

Thanks for your request. Try to use the following code.

public void TestReplaceText_95167()

{

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

Regex regex = new Regex("Test");

doc.Range.Replace(regex, new ReplaceEvaluator(ReplaceAction1), true);

doc.Save(@"200_95167_daithi\out.doc");

}

ReplaceAction ReplaceAction1(object sender, ReplaceEvaluatorArgs e)

{

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

builder.MoveTo(e.MatchNode);

builder.Write("first line \nSecond line");

e.MatchNode.Remove();

return ReplaceAction.Skip;

}

I hope that it will help you.

Best regards.

Thanks

It worked !