Need to copy a table with header and replace values multiple times.

I have a document that calls a API returning a array of data. That document has a table with a header with a marker (%%Name) that I want to replace the name of. Inside the table there are cells with similar markers (%%Row Value1 etc) that I also want to fill in. If there are multple arrays, I want to insert another table with a header so I can fill in the table with the different array. The code below is not meant to coincide with the documents I have uploaded as they are meant to be examples, but this is what I have thus far, but hasn't given me the results I was expecting. It takes the original table, fills out the data, but when I want to copy the table - it uses the filled out data table. For additional example, in the zip file - i have a sample of the document, and how I wish for the document to look like. Any help or pointers would be greatly appreciated. var result = await _myService.GetInfo(lineId); dynamic resultObject = JsonConvert.DeserializeObject(result); List

symbols = new List(); JArray payloadArray = resultObject.payload as JArray; List ids = payloadArray.Select(j => (int)j["id"]).ToList(); string replacementValue = "HOOT!"; TextSelection locationSelection = currentDoc.Find("Name:", false, false); WParagraph locationParagraph = locationSelection.GetAsOneRange().OwnerParagraph; int locationParagraphIndex = locationParagraph.OwnerTextBody.ChildEntities.IndexOf(locationParagraph); // Get the table below the location paragraph WTable currentTable = locationParagraph.OwnerTextBody.ChildEntities[locationParagraphIndex + 1] as WTable; // Duplicate the location paragraph and table three times for (int i = 0; i < ids.Count() - 1; i++) { int id = (int)payloadArray[i]["id"]; string address = (string)payloadArray[i]["address1"] + " " + (string)payloadArray[i]["address2"] + " " + (string)payloadArray[i]["address3"] + ", " + (string)payloadArray[i]["city"] + ", " + (string)payloadArray[i]["state"]; WParagraph duplicatedLocationParagraph = locationParagraph.Clone() as WParagraph; locationParagraph.OwnerTextBody.ChildEntities.Insert(locationParagraphIndex + 2, duplicatedLocationParagraph); var coverageResult = await _myService.GetInfo2(lineId, id); JObject jsonResponse = JObject.Parse(coverageResult); JObject payload = jsonResponse["payload"] as JObject; Dictionary keyValuePairs = new Dictionary(); // Modify the keys and store the new key-value pairs keyValuePairs["%%Test1"] = (string)payload["value1"]; keyValuePairs["%%Test2"] = (string)payload["value2"]; // Create a new table for each iteration WTable duplicatedTable = currentTable.Clone() as WTable; // Loop through the key-value pairs foreach (KeyValuePair kvp in keyValuePairs) { string newKey = kvp.Key; string newValue = kvp.Value; // Perform the desired logic with the new key-value pairs TextSelection barkSelection = currentDoc.Find(newKey, false, false); FindAndReplaceInTable(currentTable, barkSelection, newValue); locationParagraph.OwnerTextBody.ChildEntities.Insert(locationParagraphIndex + 3, duplicatedTable); } }
Attachment: example_dec6ba80.zip

3 Replies

DS Dharanya Sakthivel Syncfusion Team August 23, 2024 12:48 PM UTC

Hi Sydney,

From the given details, we found that you are populating data into the Word document by replacing placeholders in Word document with values from JSON. From the given attachment, we have found that both documents are the same, and it is not enough to proceed further on our side. So, could you please share us the exact input template document which maintained at your side with placeholders and also input JSON or data file.

Based on the above details, we will check on the feasibility to achieve your requirements using Word library (DocIO) and share more details on it.

Note: If you have confidential data in your inputs, kindly replace them with some dummy values. We just need your inputs to understand about the exact requirement.

Regards,
Dharanya.




SB Sydney Brea August 23, 2024 08:02 PM UTC

Thank you for responding and apologies for the mistake. I thought I had them seperate as I was sanitizing the documents and JSON. :) 

Enclosed should be three files, the initial document, what I want the intended result to be, and the requested JSON format of how things work.

It's been tricky to figure out, so if you can see what I might be doing wrong, it'd be greatly appreciated!


Thanks aqain!
Sydney


Attachment: syncfusiondociohelp_e4d76ed9.zip


DS Dharanya Sakthivel Syncfusion Team August 26, 2024 10:20 AM UTC

Sydney,

We have prepared and attached the sample to get the result as you attached intended result.docx. We can achieve this by performing a mail merge (which is the process of merging data from a data source into a Word template document) using merge fields (placeholders that can be replaced with data during document generation).


To do mail merge, it is needed to design template Word document with merge fields. You can insert merge fields either DocIO programmatically or using Microsoft Word application.



Here are the steps to design a template Word document for executing a mail merge using Microsoft Word:

  1. Insert Merge fields in Word document, where you wish to replace with data from data source (JSON).

2. The merge field "EmployeeName" will now be added to the Word document as like below.



In your requirement, we have achieved this using group mail merge to append multiple records from a data source to a template document. The records are inserted between merge fields marked as start and end groups, and this process is repeated for every record in the data source.

Kindly refer the below Documentation and GitHub sample to know about mail merge in DocIO.
Documentation-working-with-mail-merge
GitHub-sample-Mail-merge

Note: We have modified the template document to replace content with merge fields.

Regards,
Dharanya.



Attachment: Generatetableusingmailmege_feab0d6.zip

Loader.
Up arrow icon