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

Free Support Forum - aspose.com

Date Comparison IN & NOT IN Template Syntax in LINQ Conditional Blocks | IF ELSE IF | Build Reports using C# .NET DataTable | DOTX

Hello Team,

I am a paid customer of Aspose. I am facing issue with conditional expressions. There are some conditional expressions in the source document attached below, which will be evaluated on the basis of dynamic value. I researched on Aspose.words api but could not found any useful solution that I can implement.
I have attached my source template and output document below.
Can you please suggest the best possible way to implement this using Aspose.words api ?

Below are the information regarding source template and output document.

Source template Information:
In this document, properties and property types are used where properties are Property state, Property_County, judgment_hearing_date, principal_balance etc
which is placed inside the curly braces. Property types are DT and BR. Application identifies the property on the basis of property type and evalute its value.

Output Document Information:
Use the below mentioned dynamic values:
Property state = “AL”
Property_County = “Monroe"
judgment_hearing_date = “5/6/2020”
principal_balance = “50,000”
P1 = false
P2 = true

Queries:

  • Is Source template format correct ? If not please update it into correct format and send me.
  • Once template format is correct, please provide the .net source code for how can we get all the properties along with its types from source template. Then pass the values (as provided the fixed values above).
  • Execute the expressions then provide the final output as mentioned in Output document.

Sorce_and_Output document.zip (30.0 KB)

It is an urgent requirement.
please update me and also let me know if you need any other info from my side.

@johnkaltz,

There are two ways that you can use to build reports - using classic Mail Merge Reporting technique and by using the advanced LINQ Reporting Engine. Please see the following template document that demonstrates two examples of each method.

.NET Code is as follows:

Document doc = new Document("E:\\Temp\\Sorce_and_Output document\\template.docx");

doc.MailMerge.Execute(new string[] { "Property state", "judgment_hearing_date", "principal_balance", "P1", "P2", "Property_County" },
                        new object[] { "AL", "5/6/2020", "50,000", "false", "true", "Monroe" });

DataTable dt = GetDataTable();
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, dt.Rows[0], "firstrow");

doc.Save("E:\\Temp\\Sorce_and_Output document\\20.7.docx");

private static DataTable GetDataTable()
{
    DataTable dataTable = new DataTable("tbl");

    dataTable.Columns.Add(new DataColumn("property_state"));
    dataTable.Columns.Add(new DataColumn("judgment_hearing_date"));
    dataTable.Columns.Add(new DataColumn("principal_balance"));
    dataTable.Columns.Add(new DataColumn("P1"));
    dataTable.Columns.Add(new DataColumn("P2"));
    dataTable.Columns.Add(new DataColumn("Property_County"));

    DataRow dataRow = dataTable.NewRow();
    dataRow[0] = "AL";
    dataRow[1] = "5/6/2020";
    dataRow[2] = "50,000";
    dataRow[3] = "false";
    dataRow[4] = "true";
    dataRow[5] = "Monroe";
    dataTable.Rows.Add(dataRow);
            
    return dataTable;
}

Please also refer to the following section of LINQ Reporting documentation:

1 Like

Hello Awais,
Thanks for the solution. My templates are not static. It gets changes very frequently by user so I need to fetch all properties mentioned in the document like property_state, judgment_hearing_date etc. how can i get all properties name at server side (.net).

@johnkaltz,

Please try the following code:

Document doc = new Document("E:\\Sorce_and_Output document\\Source template.dotx");

FindReplaceOptions opts = new FindReplaceOptions();
ReplaceWithMergefield handler = new ReplaceWithMergefield();
opts.ReplacingCallback = handler;
opts.Direction = FindReplaceDirection.Backward;
doc.Range.Replace(new Regex(@"\{(.*?)\}"), "", opts);

foreach (string str in handler.list)
    Console.WriteLine(str);

private class ReplaceWithMergefield : IReplacingCallback
{
    public ArrayList list = new ArrayList();
    public ReplaceAction Replacing(ReplacingArgs e)
    {
        if (!list.Contains(e.Match.Groups[1].Value.Trim()))
            list.Add(e.Match.Groups[1].Value.Trim());

        return ReplaceAction.Skip;
    }
}

<<if DT{Property state} = “AL”>>Print this text if its in Alabama<</if>>
I have to support above syntax but it is not working.

Document doc = new Document("E:\\Temp\\Sorce_and_Output document\\template.docx");

DataTable dt = GetDataTable();
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, dt.Rows[0], "firstrow");

private static DataTable GetDataTable()
{
    DataTable dataTable = new DataTable("tbl");
    dataTable.Columns.Add(new DataColumn("DT{Property state}"));
    DataRow dataRow = dataTable.NewRow();
    dataRow[0] = "AL";
    dataTable.Rows.Add(dataRow);   
    return dataTable;
}
doc.Save("E:\\Temp\\Sorce_and_Output document\\20.7.docx");

I have to distinguish the properties/tag so here I used DT{property state} & BR{P1}
you can also suggest other way to distinguish tags so that I can fetch at server side the way you suggested in previous comment.

Thank you for help.
I need to support in and not in operator. Below syntax is not working

<<if [property_state = “AL”]>>Print this text if its in Alabama <<else if [property_state in (“GA”, “TX”)]>>Else If it is in GA or TX then print this<</if>>

Please also suggest date comparison syntax for template.

@johnkaltz,

We are checking these scenarios and will get back to you soon.

ok. Please let me know if you need any other info.

Hello Awais,

Any update ?

@johnkaltz,

You can do it like this:

Document doc = new Document("E:\\Temp\\Sorce_and_Output document\\Source template.dotx");

FindReplaceOptions opts = new FindReplaceOptions();
ReplaceWithMergefield handler = new ReplaceWithMergefield();
opts.ReplacingCallback = handler;
opts.Direction = FindReplaceDirection.Forward;

Console.WriteLine("BR tags");
Console.WriteLine("------------------");
doc.Range.Replace(new Regex(@"BR\{(.*?)\}"), "", opts);

foreach (string str in handler.list)
    Console.WriteLine(str);

Console.WriteLine("------------------");
Console.WriteLine("DT tags");
Console.WriteLine("------------------");
handler = new ReplaceWithMergefield();
opts.ReplacingCallback = handler;
doc.Range.Replace(new Regex(@"DT\{(.*?)\}"), "", opts);

foreach (string str in handler.list)
    Console.WriteLine(str);

Console.WriteLine("------------------");

We are working on your remaining questions and will get back to you soon.

Hello Awais,

My question was, If I use this syntax DT{Property state} and same as datatable column name. It doesn’t work and throws an error.
I want correct template syntax for the below:
<<if DT{Property state} = “AL”>>Print this text if its in Alabama<</if>>

@johnkaltz

Please see attached input and output Word documents (Docs.zip (39.7 KB)) and try running the following code:

Document doc = new Document("E:\\Temp\\res\\template.docx");

DataTable dt = GetDataTable();
ReportingEngine engine = new ReportingEngine();
engine.KnownTypes.Add(typeof(DateTime));
engine.BuildReport(doc, dt.Rows[0], "firstrow");

doc.Save("E:\\Temp\\res\\20.7.docx");

private static DataTable GetDataTable()
{
    DataTable dataTable = new DataTable("tbl");

    dataTable.Columns.Add(new DataColumn("property_state"));
    dataTable.Columns.Add(new DataColumn("judgment_hearing_date", typeof(DateTime)));
    dataTable.Columns.Add(new DataColumn("principal_balance"));
    dataTable.Columns.Add(new DataColumn("P1"));
    dataTable.Columns.Add(new DataColumn("P2"));
    dataTable.Columns.Add(new DataColumn("Property_County"));

    DataRow dataRow = dataTable.NewRow();
    dataRow[0] = "AL"; // AL GA TX
    dataRow[1] = new DateTime(2020, 6, 5);
    dataRow[2] = "50,000";
    dataRow[3] = "false";
    dataRow[4] = "true";
    dataRow[5] = "Monroe";
    dataTable.Rows.Add(dataRow);

    return dataTable;
}

We also have managed to observe the exception on our end. But, we are checking this scenario further and will get back to you soon.

@johnkaltz,

Please note that the LINQ Reporting Engine of Apsose.Words has its own Syntax and you need to follow it to be able to build reports. For more information, please refer to the Template Syntax section of documentation.

I am afraid, we cannot support such custom Syntax; however, if you cannot use our template syntax and want to use your own template syntax instead then using of text replacements (replace placeholders etc) would be the easiest option. So, you can write your own utility to transform your custom template syntax into the one Aspose.Words’ LINQ Reporting Engine understands.

Thank you so much. Solutions that you provided helped me a lot.