Parse word template to identify the business objects required for JSON data source

@NaraSg

Can you please provide more details about the structure of your Word template and the specific business objects you are working with?

@NaraSg Unfortunately, there is no built-in method to get LINQ syntax in the document. However, you can use regular expression to get LINQ tags.

(?<=<<\s|\w*\[|\G)([^\]\[\.]+)\.(?=[^][]*\])

Sure, you can pass as many data sources as it is required. Just use the overload that accepts an array of data sources:
https://reference.aspose.com/words/net/aspose.words.reporting/reportingengine/buildreport/#buildreport_2

Thank you Alexey. I shall try it

1 Like
<<foreach [in persons]>> <<[Name]>> <<[Role]>> <<[Dept]>> <<[Country]>> <</foreach>>

<<foreach [in customers] >> <<[FirstName]>> <<[LastName]>> <<[Industry]>> <<[Country]>> <</foreach>>

I am passing two datasources (one for persons, one for customers) for the above two foreach. But get error

System.InvalidOperationException
  HResult=0x80131509
  Message=An error has been encountered at the end of expression 'in customers] '. Can not get the value of member 'customers' on type 'System.Data.DataRow'.

If I just use one datasource & try out each of the above foreach, it works. When we have multiple datasources, should we use the datsource name to resolve?

Please advise

I added the datasourcename (Persosn, Customers) as below & it works

<<foreach [in Persons.persons]>> <<[Name]>> <<[Role]>> <<[Dept]>> <<[Country]>> <</foreach>>
<<foreach [in Customers.customers] >> <<[FirstName]>> <<[LastName]>> <<[Industry]>> <<[Country]>> <</foreach>>

I could not find template samples that explains the above.

@NaraSg Most likely your data source is configured improperly, I tested the following syntax with the following data source and everything works as expected:

<<foreach [in persons]>> <<[Name]>> <<[Role]>> <<[Dept]>> <<[Country]>> 
<</foreach>>

<<foreach [in customers] >> <<[FirstName]>> <<[LastName]>> <<[Industry]>> <<[Country]>> 
<</foreach>>

Data:

{
  "persons": [
    {
      "Name": "Name1",
      "Role": "Role1",
      "Dept": "Dept1",
      "Country": "Country1"
    },
    {
      "Name": "Name2",
      "Role": "Role2",
      "Dept": "Dept2",
      "Country": "Country2"
    }
  ],
  "customers": [
    {
      "FirstName": "FirstName1",
      "LastName": "LastName1",
      "Industry": "Industry1",
      "Country": "Country1"
    },
    {
      "FirstName": "FirstName2",
      "LastName": "LastName2",
      "Industry": "Industry2",
      "Country": "Country2"
    }
  ]
}

Here are template, data and output:
data.zip (311 Bytes)
in.docx (14.1 KB)
out.docx (11.2 KB)

Thank you very much. It works. However, when I try to populate into a table with foreach loop outside table it gives, error

System.InvalidOperationException
  HResult=0x80131509
  Message=An error has been encountered at the end of expression 'person.Name]>'. Can not get the value of member 'person' on type 'System.Data.DataRow'.

I will
Template1.docx (20.3 KB)

attach my template where does not work.

@NaraSg The table in your template is floating and it’s actual position in the document object model is before opening tag of foreach. Just make the table inline to see the problem. Here is the modified template:
in.docx (20.3 KB)

Thank you. It works with error but my expectation was that the table will be repeated multiple times for each list item. However, we that it overwrites on the same row. How to show multiple tables, one for each item in the list.
FilledDocument.docx (14.1 KB)

Attached the expected document
FilledDocumentExpected.docx (21.2 KB)
But want it to be separate tables, not as one table with headers in between. What is seen in the document is very similar to having foreach in header

@NaraSg The problem occurs because table is floating in your document. Please try avoid using floating tables. You can simply set ReportBuildOptions.RemoveEmptyParagraphs to get the expected output:

JsonDataSource ds = new JsonDataSource(@"C:\Temp\data.json");

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

ReportingEngine engine = new ReportingEngine();
engine.Options = ReportBuildOptions.RemoveEmptyParagraphs;
engine.BuildReport(doc, ds, "Persons");
            
doc.Save(@"C:\Temp\out.docx");

out.docx (14.1 KB)

Yes, will try out.
Thk you

1 Like

Hello,

Attached is a template & json data source. I am unable to render the Data in the third row (first two rows are headers) onwards
Template1.docx (30.0 KB)

data.zip (745 Bytes)

"TableData": [
      {
        "Data": [
          "1",
          "1",
          "10.551",
          "10.551",
          "30.551"
        ]
      },
      {
        "Data": [
          "1",
          "1",
          "10.551",
          "10.551",
          "30.551"
        ]
      },
      {
        "Data": [
          "1",
          "1",
          "10.551",
          "10.551",
          "30.551"
        ]
      },
      {
        "Data": [
          "1",
          "1",
          "10.551",
          "10.551",
          "30.551"
        ]
      },
      {
        "Data": [
          "1",
          "1",
          "10.551",
          "10.551",
          "30.551"
        ]
      }
    ]

Please help to understand

@NaraSg

By looking at your data (that is, an array of arrays), it seems like the thing you expect in a result document is a cross (pivot) table. Working with Cross (Pivot) Tables explains how to build those.

Some points to note:

  • For a table-row data band occupying one table column, a greedy switch should be applied to a closing foreach tag (see the last part of Working with Table-Row Data Bands).

  • Operator ++ is not supported by LINQ Reporting Engine. The list of supported operators can be found at Using Operators.

  • To reference an item of a simple-type JSON array in a template, the name of the array plus “_Text” suffix should be used as per the second note at Accessing JSON Data.

Combining all these, the last cell of your template table should contain something like this:

<<foreach [in ResultTable.TableData]>><<foreach [in Data] -horz>><<[Data_Text]>><</foreach>><</foreach -greedy>>

Hello ,
Thank you for the pointers on Pivot tables & accessing data arrays
I tried your suggestion. Get the attached error. please help to understand.
System.InvalidOperationException.zip (614 Bytes)

Template1.docx (30.2 KB)

Regards,
NaraSg

@NaraSg

This part is not considered in your template, which causes the exception. However, when adding a greedy switch, another exception is thrown, which is unexpected. We are about to log the issue for the sake of correction. After it is fixed, your template should be modified to include a greedy switch as per the quote.

@NaraSg

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28338

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

Thank you for the support. In the meantime, I will continue to evaluate other features that may be help our use cases

1 Like

Hello,
When using word templates, I notice that the replaced text follows the formatting (font, color) of evaluated expression. I like the feature very much. However, I have cases where the expressions are lengthy & I want to keep the font size smaller for those lengthy expression and have different formatting for replaced text. Is it possible?

@NaraSg You can try using HTML to specify formatting of the inserted value that differs from the formatting of the tag. But I would prefer to keep value formatting the same as formatting of the tags. This make easier to analyze the output.