We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Perform replacement with newlines in replacement string

Hi There!
We need to perform replacements in a Aspose.Word document (using your Range class), where the replacement string contains newlines, which need to be preserved. For instance:

String fromString = "some_token";
String toString = "this is a first paragraph.\n\nthis is a second paragraph.";
document.getRange.replace(fromString, toString, true, false);

We then expect that the two line breaks are included, but they just seem to be omited. It’s known that paragraph breaks (\r) and so on are not supported. But is there any workaround for preserving these newlines? We really depend on this feature.
Thanks a lot for your help,

Hi Remo,

Thanks for your inquiry. Please use the ControlChar.LINE_BREAK as shown below to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
String fromString = "some_token";
String toString = "this is a first paragraph." + ControlChar.LINE_BREAK + ControlChar.LINE_BREAK + "this is a second paragraph.";
doc.getRange().replace(fromString, toString, true, false);
doc.save(MyDir + "Out.docx");

Thank you for your quick response. That did the trick!

Hi Remo,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.