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

Free Support Forum - aspose.com

Re: Search and replace not working with multi line string

Hi,

I have seen that this replace problem was solved from Aspose.Words for Java 16.7.0 - released in 8/9/2016

I’m using aspose-words-14.4.0 and I have this problem:

Cannot replace strings containing \r\n and if I’m solving this replacing before with ControlChar.LINE_BREAK I have problems on justified format because of soft return (ControlChar.LINE_BREAK = Shift+Enter).

Can anybody help me with an advice how to insert only hard return Enter (not Shift+Enter instead of \r\n) using Aspose, maybe exists some other value than ControlChar.LINE_BREAK which is doing the same (the ControlChar.CR \ ControlChar.CR_LF are not working because of the replacement issue)?

Is this problem also solved in the fixed version Aspose.Words for Java 16.7.0?


Thanks

Hi Horatiu,

Thanks for your inquiry. Please use ControlChar.PARAGRAPH_BREAK instead of ControlChar.LINE_BREAK to get the desired output. Hope this helps you.

If you still face problem, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we'll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click 'Reply' button that will bring you to the 'reply page' and there at the bottom you can include any attachments with that post by clicking the 'Add/Update' button.

The full picture is that I want to replace on a justified line a word with some content which contains line-brakes (is on multiple lines)

As a particular scenario I am replacing in a WORD-doc a key value (“test”) with a String (replacedDataDictionaryValue) which contains line-breaks (\r\n):

// just an example of input for replacement - you can take this String content from Input.docx
String replacedDataDictionaryValue = “text 1 long\r\n text\r\n second text\r\n”;

//(1) check for LINE_BREAK (CR_LF) before replace - for Aspose special LINE_BREAK
replacedDataDictionaryValue = replacedDataDictionaryValue.replaceAll(ControlChar.CR_LF, ControlChar.PARAGRAPH_BREAK);

quoteDocument.getRange().replace(“test”, replacedDataDictionaryValue, false, false);

If I’m replacing ControlChar.CR_LF (\r\n) with ControlChar.PARAGRAPH_BREAK (as suggested by you) I’m getting the same error which I’m getting on doc.getRange().replace(…) if I’m not doing nothing for the replacedDataDictionaryValue (still contains \r\n - and line bellow (1) is commented out)


Caused by: java.lang.IllegalArgumentException: The replace string cannot contain special or break characters.
at com.aspose.words.zzDO.setReplacement(Unknown Source)
at com.aspose.words.zzDO.(Unknown Source)
at com.aspose.words.zzDO.(Unknown Source)
at com.aspose.words.Range.replace(Unknown Source)


If I’m using ControlChar.LINE_BREAK for line (1) replacement for ControlChar.CR_LF

// check for LINE_BREAK (CR_LF) before replace - for Aspose special LINE_BREAK
replacedDataDictionaryValue = replacedDataDictionaryValue.replaceAll(ControlChar.CR_LF, ControlChar.PARAGRAPH_BREAK);

I’m getting the SHIFT+ENTER in the result word (without any error) but instead of correct formated content in the final word I’m having this problem with justified text for that inserted lines (which you can see in the attached zip - Result and ExpectedResult)
I haven’t written the java code but I expect to be clear enough from this message the scenario which I’m using (If you still need a exact Java.class I can provide one).


Is this covered somehow in the latest Aspose.Words for Java version?
Or can I have any solution for 14.4.0 Aspose version which I currently use?

Thank you and appreciate your support,
Horatiu

Hi Horatiu,

Thanks for sharing the detail. The shared "Expected Result.docx" has 0 Kb file size. Could you please share it again? We will then provide you more information about your query along with code.

Hi,

I have reattached the full zip.

Thank you,
Horatiu

Hi Horatiu,

Thanks for sharing the detail. We have tested the scenario using latest version of Aspose.Words for Java 17.1.0 with following code example and have not found the shared issue. Please use Aspose.Words for Java 17.1.0. We have attached the output document with this post for your kind reference.

String replacedDataDictionaryValue = "text 1 long\r\n text\r\n second text\r\n"
.replace("\r\n", ControlChar.PARAGRAPH_BREAK);

Document doc = new Document(MyDir + "input.docx");
doc.getRange().replace("test", replacedDataDictionaryValue, new FindReplaceOptions());

doc.save(MyDir + "Out v17.1.0.docx");

Hi,

It seems this is fixed in latest versions (as is shown in your result). Thank you for your help and clarifications.
I think you cannot see a work-around for version 14.4.0 which I am currently using (and we have license for) until we will be able to migrate to the latest release?

Regards,
Horatiu

Hi Horatiu,

Thanks for your inquiry. We suggest you please implement IFieldMergingCallback interface and in IFieldMergingCallback.FieldMerging method write the content according to your desired output at the location of matched node. Please refer to the following article:
Find and Repalce

Unfortunately, we don't provide support for older released versions of Aspose.Words. Also, please note that we do not provide any fixes or patches for old versions of Aspose products either. All fixes and new features are always added into new versions of our products. So, please use latest version of Aspose.Words for Java 17.1.0.