Replace \n (new line character) with "" (empty string) in aspose using linq engine syntax

sample data attached here for ref, and highlighted the chare needs to be replaced. we are using JSON DataSource her

@anupkrsinha You can replace it in your data before building the report. Like this:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("<<[TestData]>>");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, "test data\nnext line".Replace("\n", ""), "TestData");

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

Alternatively you can do the same in the reporting syntax:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("<<[TestData.Replace(\"\\n\", \"\")]>>");

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, "test data\nnext line", "TestData");

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

I have used this linq engine syntax : <<[TestData.Replace("\n", "")]>>" in word file and looks likes its giving error : An error has been encountered at the end of expression 'Benchmark_Disclosure_KIID.replace('. Character '' was unexpected at this position.

Datasource : Json Data

OR

Any way we can ignore \n using aspose linq engine syntax in MS-Word template itself.

@anupkrsinha You are using Java, so you should use Java syntax in the template. The earlier provided code was for .NET. Here is the same code for java:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.write("<<[TestData.replace(\"\\n\", \"\")]>>");
    
ReportingEngine engine = new ReportingEngine();
engine.buildReport(doc, "test data\nnext line", "TestData");
    
doc.save("C:\\Temp\\out.docx");

If you still encounter problems, please attach your sample data and template here for testing.

still i am getting the same exception, attached the word and JSON file for your reference. In our use case we are using Aspose Word Linq Engine version 24.1

test-dcouments.zip (11.8 KB)

this works for me : <<[Benchmark_Disclosure_KIID.replace("\n", "")]>>
Thanks for all your guidance :slight_smile:

1 Like

can we replace multiple words using above function, if yes could you please share the linq syntax here.
for example i want replace “Text1” and "“Text2” to “” (empty).

@anupkrsinha You can simply perform several replace operations
<<[TestData.replace("Test1", "").replace("Test2", "")]>>

Do we any linq syntax to covert string to number?

@anupkrsinha Could you please clarify your requirement? It is not quite clear what you need to achieve. If you need to parse string to integer, you can use Integer.parseInt() the same way as you in in regular Java code. But first you should register Integer as a known type:
https://docs.aspose.com/words/java/setting-up-known-external-types/