Optional paragraphs in a document template

Hi Guys,

We are considering Aspose.Words for one of our enterprise wide solutions, but we would like to know if the following requirements are feasible and how they could be implemented:

-. Could we display/hide entire paragraphs of a generated MS Word doc (from a mail-merge template) based on hard-coded C# logic? These paragraphs could contain mail-merge fields.
For Ex: depending on the recipient age (coming from DB) some paragraphs should be displayed or not in the generated doc…

-. Could we fill a mail-merged field with formatted text (i.e. coming from a Web-Form based RFT control) ??

Many thanks in advance for any help/guidelines
Cheers

Jose Parra

Hi Jose,

Thanks for your interest in Aspose.Words. Yes, you can achieve this using mail merge functionality of Aspose.Words. I have attached sample input/output documents here for your reference. The following code demonstrates how you can meet this requirement:

Document doc = new Document(@"C:\Temp\input.docx");
doc.MailMerge.FieldMergingCallback = new HandleMergeFields();
doc.MailMerge.Execute(new string[]
{
    "age"
}, new object[]
{
    25
});
doc.Save(@"C:\Temp\output.docx");

private class HandleMergeFields: IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        if (args.FieldName == "age")
        {
            if (args.FieldValue.ToString() == "25")
            {
                // here we will only hide first paragraph
                Paragraph paragraph = ((Document) args.Document).FirstSection.Body.FirstParagraph;
                paragraph.ParagraphBreakFont.Hidden = true;
                foreach(Run run in paragraph.Runs)
                run.Font.Hidden = true;
            }
        }
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        /* do something */
    }
}

Moreover, please refer to the following article which outlines how to apply custom formatting during mail merge:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/

I hope, this helps.

Best regards,