Advice on how to create a word document with a template and a loop of items

I have to realize something similar to the sample attached, eg.
foreach categories
   print category title
   foreach items in category
        print Item title
        print a table with some label+field values
        print a rich text coming from another docx that could be a file stored on the server or a varbinary field in db

What is the best strategy to achieve this? I would avoid to do all programmately (because the table could have some complicated formatting) and I wish to have a template where substitute the placeholder and generate the output in double nested loop (this is what I would do if it was html/razor output)

Is DocIO the right choice? 

Thanks


Attachment: sample_fdb7e954.zip

14 Replies

SB Sneha Biju Syncfusion Team February 12, 2024 02:07 PM UTC

Hi Sandro,

We suspect that you want to create a document with dynamic data. We have prepared a template document for this purpose. You can programmatically execute a mail merge for the template document using DocIO to create an output similar to the given document. You can find the prepared template document in the below attachment.

To learn more about mail merge, you can refer the following documentation.
https://help.syncfusion.com/file-formats/docio/working-with-mail-merge

In the prepared template document, we have used nested group mail merge. To execute mail merge for nested groups, you can refer to the following documentation:
https://help.syncfusion.com/file-formats/docio/mail-merge/mail-merge-for-nested-groups

If this meets your requirements, we can also prepare a sample that executes mail merge to obtain the desired output document based on the given document and algorithm.

If we have misunderstood any of your requirements, please share the input document and expected output document.

Regards,
Sneha.


Attachment: Template_25d82b8.docx


SR Sandro Rizzetto replied to Sneha Biju February 15, 2024 07:24 AM UTC

> If this meets your requirements, we can also prepare a sample that executes mail merge to obtain the desired output document based on the given document and algorithm.

A sample would be more than appreciated!! thanks in advance!

Remember only that Item fields would more than one but fixed  (not a third loop luckily)

And Description is the content of another docx files (I'm still in doubt if keep the file on server, or read the content in DocIO and save in database field)

Image_8948_1707981812219




SB Sneha Biju Syncfusion Team February 16, 2024 04:12 PM UTC

Sandro, we have prepared a sample to meet your requirement and attached below. In the sample we have followed the below steps:
1. Get the mail merge data
2. Execute mail merge
3. Using MergeField event, insert a bookmark at the field and add the bookmark name and another document path to a dictionary
4. After execution of mail merge, replace the added bookmarks with the content of the document at the given path using ReplaceContent API.
5. Save the document.


Attachment: Mailmerge_2eca701e.zip


SR Sandro Rizzetto February 21, 2024 07:52 AM UTC

That's really great Sneha!! Thanks a lot!

Last question... if the Description, instead from an docx, would be coming from an HTML  (image something saved from a sfRichtextBox control), what would be the strategy ?

I guess if cannot put the string as is, because you'll see the "<p>mycontent with tag</p>" etc...

Suppose one.docx will be one.html like attached...

Thanks again
Sandro


Attachment: one_2a32ae3b.zip


SB Sneha Biju Syncfusion Team February 22, 2024 09:46 AM UTC

Sandro, we have found that your requirement is to perform a mail merge with an HTML file as the merge field result. To achieve this, we have modified the existing sample as follows:
1. Give the HTML file path as the field value instead of the DOCX file path.
2. When opening the document, set the format type as Automatic.

Additionally, upon further analysis, we discovered that the HTML file contains an image without an end tag or self-ending tag. This causes an exception when opening the HTML in DocIO. Therefore, we have modified the HTML file by adding a backslash at the end of the image tag.

You can refer the following documentation for HTML conversions in DocIO:
https://help.syncfusion.com/file-formats/docio/html

As we mentioned in the documentation, in Word library (DocIO) we use XmlReader for parsing the content from input HTML. So, the input HTML should meet XML standard (have proper open and close tags), even if you specify XHTMLValidationType parameter as XHTMLValidationType.None. We suggest you check whether the HTML content meets the XHTML 1.0 format, which includes proper open and close tags.

You can find the modified sample in the below attachment.


Attachment: Mail_merge_Modified_201fdcbd.zip


SR Sandro Rizzetto March 1, 2024 10:30 AM UTC

Thanks Sneha,

is your SfRichDocument producing correct XHTML 1.0 compliant code? Because my input will be come from there, so if I have to tune some parameters on your editor please let me know.


Again thank you for your precious help and time to produce the sample.



SB Sneha Biju Syncfusion Team March 4, 2024 02:07 PM UTC

Sandro, we suspect that you are using our RichTextEditor control to create an HTML file and parse it to DocIO.

Could you please confirm if this is the requirement or something else? Based on your confirmation, we will check with the appropriate team and share the details.



SR Sandro Rizzetto March 4, 2024 02:36 PM UTC

Yes, sorry for misspelling... I mean 

<SfRichTextEditor ...>



SR Sandro Rizzetto March 4, 2024 04:15 PM UTC

And BTW... if the HTML content is not coming from a file like in your sample, but from a string
(eg. ItemList.HtmlDescription = "<p>b>Lorem</b></p>" )  how would render the bookmark? The WordDocumentPart seems that have only a method to Load...


WordDocumentPart part = new WordDocumentPart();

part.Load(tempDoc);   // Load from a string ???

nav.ReplaceContent(part);

Thanks



SB Sneha Biju Syncfusion Team March 5, 2024 07:56 AM UTC

Sandro,

Regarding “is your SfRichDocument producing correct XHTML 1.0 compliant code”:
If you want to obtain the formatted HTML strings to convert them into documents using DocIO, you can utilize the EnableXhtml property in RichTextEditor. We have provided a sample for enabling this property and saving the HTML file using DocIO. You can find the sample (RTE_Sample_for_Clean_HTML.zip) in the attachment below.

Regarding “if the HTML content is not coming from a file like in your sample, but from a string”:
We have made modifications to the existing sample to retrieve the content from an HTML string. In the revised sample, we have followed the steps outlined below:
1. Retrieve the HTML description into the dictionary.
2. While replacing the bookmarks, open a new Word document as tempDoc and append the HTML string from the dictionary.
3. Replace the content of the bookmark as before.

You can find the sample (Mail merge with HTML string.zip) in the attachment below.


Attachment: Attachments_d463f254.zip


SR Sandro Rizzetto March 6, 2024 09:42 AM UTC

Wonderful... have no words to thank you enough...


Last (I swear) question... if the HTML stirng contains an image, even with absolute URL, in word is not shown.

Eg. 

 items.Add(new ItemsList("Title1", "<p><b>Lorem</b><br/><img src=\"https://www.rizzetto.com/images/site/logo.png\" /></p>", field));


Is it a limitation of Word ?



SB Sneha Biju Syncfusion Team March 6, 2024 01:04 PM UTC

Sandro, to import the images referred as a URL in the HTML string, we suggest you import these images using ImageNodeVisited event in DocIO.

Please refer the below links to know more about ImageNodeVisited event,
https://help.syncfusion.com/file-formats/docio/html#customize-image-data
https://www.syncfusion.com/kb/13054/how-to-get-image-from-url-while-opening-html-in-asp-net-core

We have modified the existing sample to trigger the ImageNodeVisited event and preserve the images in the output Word document. In the sample, we have made the following modifications:
1. Trigger the ImageNodeVisited event before appending the HTML to tempDoc.
2. In the ImageNodeVisited event method, fetch the image data from the URL and set the imageStream.

You can find the sample (Mail merge with HTML string Modified.zip) in the attachment below.


Attachment: Mail_merge_with_HTML_string_Modified_c334603.zip


SR Sandro Rizzetto March 7, 2024 07:12 AM UTC

Again perfect... if someone says syncfusion is expensive compared to the competitors, maybe they don't know the level of support offered.

Thanks a lot.



CA Chrispine Agunja Imbo Syncfusion Team March 8, 2024 09:42 AM UTC

Sandro,

Thank you for sharing your perspective! Some individuals indeed view our product as expensive. However, what truly distinguishes us is the outstanding support we provide. Our responsive customer service and comprehensive documentation are invaluable assets for businesses.


Loader.
Up arrow icon