Conditional Table Headers For extended pages

document.docx (14.6 KB)
JsonData Passed:

{
  "DataArray": [
    {
      "header1": "test",
      "header2": "test",
      "header3": "ABGGFDSSBOTTLFSDGSDFSDABORATORIESOT"
    },
    {
      "header1": "test",
      "header2": "test",
      "header3": "ABGGFDSSBOTTLFSDGSDFSDABORATORIESOT"
    }]
}

I am passing json data which contains DataArray key which is an array of some json elements. The word document consists of table which is to be filled with the elements of DataArray key. The header of this table is repeated on next subsequent pages. Now I want the first column of the header to have value “Value1” when total size of elements in DataArray is less or equal to 20 and “Value2” when it is greater than 20. Is it possible to retrieve the size of the passed json Key and have different table headers?

@patilnishant0311

To achieve conditional table headers based on the size of the DataArray in your JSON data, you can follow these steps:

  1. Count the Elements: First, you need to determine the number of elements in the DataArray. This can be done by checking the length of the array.

  2. Set Header Values: Based on the count, you can set the value of the first column in the header. If the count is less than or equal to 20, set it to “Value1”; otherwise, set it to “Value2”.

  3. Create the Table: When creating the table in your document, you can dynamically set the header based on the condition evaluated in the previous step.

Here is a sample code snippet that demonstrates how to implement this logic:

import com.aspose.words.*;

public class ConditionalTableHeader {
    public static void main(String[] args) throws Exception {
        // Load the document
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Sample JSON data
        String jsonData = "{ \"DataArray\": [ { \"header1\": \"test\", \"header2\": \"test\", \"header3\": \"ABGGFDSSBOTTLFSDGSDFSDABORATORIESOT\" }, { \"header1\": \"test\", \"header2\": \"test\", \"header3\": \"ABGGFDSSBOTTLFSDGSDFSDABORATORIESOT\" } ] }";
        
        // Parse JSON and count elements in DataArray
        int dataArraySize = 2; // Assume you parsed the JSON and found 2 elements

        // Determine header value based on the size of DataArray
        String headerValue = (dataArraySize <= 20) ? "Value1" : "Value2";

        // Start creating the table
        Table table = builder.startTable();
        
        // Insert header row
        builder.insertCell();
        builder.write(headerValue); // First column header
        builder.insertCell();
        builder.write("Header2"); // Second column header
        builder.insertCell();
        builder.write("Header3"); // Third column header
        builder.endRow();

        // Insert data rows (example data)
        for (int i = 0; i < dataArraySize; i++) {
            builder.insertCell();
            builder.write("Data1"); // Replace with actual data
            builder.insertCell();
            builder.write("Data2"); // Replace with actual data
            builder.insertCell();
            builder.write("Data3"); // Replace with actual data
            builder.endRow();
        }

        // End the table
        builder.endTable();

        // Save the document
        doc.save("ConditionalTableHeader.docx");
    }
}

In this example, the header of the table is set based on the size of the DataArray. You can replace the placeholder data with actual values from your JSON data as needed. This approach allows you to dynamically adjust the table headers based on the conditions you specified.

If you have any further questions or need additional assistance, feel free to ask!

I have constraints on not making any change on the Java code end. Is it possible on the micrososft level using word aspose linq language to dynamically retrieve the size of passed DataArray and insert a conditional statement in word to change the table header in the extended page.
To sum up:
I want one table header for the first page and another for the next subsuquent extended pages without making any changes on the code level

@patilnishant0311 You can use if condition in your table header, like this:

<<if [DataArray.count()>1]>>More Than One<<else>>One<</if>>

Here are the modified template and output document:
in.docx (14.9 KB)
out.docx (11.9 KB)

@alexey.noskov
output.pdf (23.8 KB)

As you can see here on both the pages more than one has been printed. My requirement is that I want One for the first page and on the next subsequent pages I want “More than One”. Is it possible?

@patilnishant0311 To achieve this, there must be separate tables on each page. This is impossible to achieve with one table and repetitive table header row.