Free Support Forum - aspose.com

Replace Line Feed character

Hi,


I have a word document in which there are special character like Line feed (Chr(10)).

I need to replace all such character at once.

Is there any way I could do it using

document.Range.text.replace()

Thanks,
Adriel

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Yes, of course, please try using the following code:

// Open document.

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

doc.Range.Replace(ControlChar.LineFeed, "Test", false, false);

doc.Save("out.doc");

Best regards,

Hi Andrey,


I have tried this method out and it doesn’t seem to be working for me.

Also, I need to replace the special characters and immediately start reading from the document.

If I add in a condition to check if the line feed character exists, the conditions shows true, but then it doesn’t replace the characters.

Thanks

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Could you please attach your document here for testing? I will check the problem on my side and provide you more information.

Best regards,

Hi Andrey,


I have attached the Word Document for your reference.

The line feed character appears between the 1st and 2nd line in the document.

Thanks

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. Please try using the following code:

// Open document.

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

doc.Range.Replace(ControlChar.LineBreak, "Test", false, false);

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

Best regards,

Hi Andrey,


Thanks a lot for the reply.

I needed another favor too.

I need to replace the line feed character with the enter key Chr(13)

Thanks

Hello

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. In this case you should use IReplacingCallback. Please see the following link to learn more:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/find-and-replace-overview.html

// Open document.

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

doc.Range.Replace(new Regex(ControlChar.LineBreak), new CustomReplaceEvaluator(), true);

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

private class CustomReplaceEvaluator : IReplacingCallback

{

///

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

///

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

// Create Document Buidler aond insert text

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.InsertParagraph();

return ReplaceAction.Replace;

}

}

Best regards,

Hey Andrey,


Thank you so much.

It worked :slight_smile:

Thanks
Adriel

Hi Andrey,


I am facing an issue again with the line feed character replacement.

When I try replacing with the Two Questions.docx it works fine but then when I try the Three Questions.docx it dose not replace the line feeds.

Thanks,
Adriel

Hello

Thanks for your request. In this case you just need to change the one line of code:

doc.Range.Replace(new Regex(ControlChar.LineBreak), new CustomReplaceEvaluator(), false);

Best regards,

Hi Andrey,


I did it that way but then it still doesn’t work.

I have attached the original document and the document saved after replacing the line feed characters.

Thanks
Adriel

Hi Adriel,

Thanks you for additional information. But I cannot reproduce the problem on my side using the latest version of Aspose.Words (10.1.0) and the following code:

Document doc = new Document("C:\\Temp\\Three+Questions-Original.docx");

doc.Range.Replace(new Regex(ControlChar.LineBreak), new CustomReplaceEvaluator(), false);

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

private class CustomReplaceEvaluator : IReplacingCallback

{

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

// Create Document Buidler and insert paragraph

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.InsertParagraph();

return ReplaceAction.Replace;

}

}

I send the document produced on my side to your e-mail.

Best regards,

Hi Andrey,


Thanks for the reply.
I realized that the code was placed wrongly.
It works fine now.

Thanks,
Adriel

Hello

It is perfect, that you already found solution. Please let me know in case of any issues. I will be glad to help you.

Best regards,