Use string to populate merge marks

Hi,

I’m trying to use data feed into the C# app to populate the merge marks but only working when I have one merge mark and not multiple.

It works when it’s C:\Temp|Template_INVOICE|“Contact_Details”|“James Bond”

But when I’m feeding this in C:\Temp|Template_INVOICE|Contact_Details,Company_Name|“James Bond”,“M1” it doesn’t.

Code being used:

class MailMerge
{
    static void Main(string[] args)
    {
        var arguments = string.Join("|", args).Split(’|’);
        if (arguments.Length > 1)
        {
            var Directory = arguments[0];
            var TemplateNm = arguments[1];
            var MergeFields = arguments[2];
            var MergeData = arguments[3];

            Document doc = new Document(Directory + TemplateNm + ".doc");

            doc.MailMerge.TrimWhitespaces = false;

            doc.MailMerge.Execute(
            new string[] { MergeFields },
            new object[] { MergeData });

            PdfSaveOptions so = new PdfSaveOptions();
            so.PreserveFormFields = true;

            string OutputFile = Directory + TemplateNm + "_OUT.pdf";
            doc.Save(OutputFile, so);
        }
    }
}

Thanks

Hi there,

Thanks for your inquiry. Could you please attach your input and output Word documents here for testing? We will investigate the issue on our side and provide you more information.

Please find attached the template used.

Out1.pdf is when I just feed in one field and one data item.

Out2.Pdf is when I add more than one.

Thanks

Phil

Hi Phil,

Thanks for sharing the detail. In your case, you need to convert the string to string array. Please use following modified code to get the correct output.

var arguments = string.Join("|", args).Split(’|’);
if (arguments.Length > 1)
{
    var Directory = arguments[0];
    var TemplateNm = arguments[1];
    string[] MergeFields = arguments[2].Split(',');
    object[] MergeData = arguments[3].Split(',');
    doc.MailMerge.TrimWhitespaces = false;
    doc.MailMerge.Execute(MergeFields, MergeData);
}

Thanks Tahir

Hi Phil,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.