Monday, May 5, 2014

Mail Merge using Spire.doc

Let us think about this scenario: You are employed by an IT company. One of your company’s products has been updated in a major scale. You need to inform all the customers of that product. And it is a quiet long list. It is very silly to write the notice one by one. It costs much time and man-power. You need a better solution to fulfill the work quickly and more efficiently. Here I provide you a solution to do the work by using a component.  You can check the component here (https://freeword.codeplex.com/).

It is a notice and the context is the same. First we create a template. This template is used to create all the notices. Check the template below.
Then what need to be done is to replace the text in brackets to the information of users. Here is code to do the work:
               private int lastIndex = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            //create word document
            Document document = new Document();
            document.LoadFromFile("template.doc");
            lastIndex = 0;

            //informaton of customers
            List<CustomerRecord> customerRecords = new List<CustomerRecord>();
            CustomerRecord c1 = new CustomerRecord();
            c1.ContactName = "Lucy";
            c1.Fax = "786-324-10";
            c1.Date = DateTime.Now;
            customerRecords.Add(c1);

            CustomerRecord c2 = new CustomerRecord();
            c2.ContactName = "Lily";
            c2.Fax = "779-138-13";
            c2.Date = DateTime.Now;
            customerRecords.Add(c2);

            CustomerRecord c3 = new CustomerRecord();
            c3.ContactName = "James";
            c3.Fax = "363-287-02";
            c3.Date = DateTime.Now;
            customerRecords.Add(c3);

            //execute mailmerge
            document.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);
            document.MailMerge.ExecuteGroup(new MailMergeDataTable("Customer", customerRecords));

            //save doc file.
            document.SaveToFile("result.doc", FileFormat.Doc);

            //viewer the result file.
            System.Diagnostics.Process.Start("result.doc");
        }

        void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
        {
            //next row
            if (args.RowIndex > lastIndex)
            {
                lastIndex = args.RowIndex;
                AddPageBreakForMergeField(args.CurrentMergeField);
            }
        }

        void AddPageBreakForMergeField(IMergeField mergeField)
        {
            //find position of needing to add page break
            bool foundGroupStart = false;
            Paragraph paramgraph = mergeField.PreviousSibling.Owner as Paragraph;
            MergeField merageField = null;
            while (!foundGroupStart)
            {
                paramgraph = paramgraph.PreviousSibling as Paragraph;
                for (int i = 0; i < paramgraph.Items.Count; i++)
                {
                    merageField = paramgraph.Items[i] as MergeField;
                    if ((merageField != null) && (merageField.Prefix == "GroupStart"))
                    {
                        foundGroupStart = true;
                        break;
                    }
                }
            }

            paramgraph.AppendBreak(BreakType.PageBreak);
        }


    //class to represent customers
    public class CustomerRecord
    {
        private string m_contactName;
        public string ContactName
        {
            get
            {
                return m_contactName;
            }
            set
            {
                m_contactName = value;
            }
        }

        private string m_fax;
        public string Fax
        {
            get
            {
                return m_fax;
            }
            set
            {
                m_fax = value;
            }
        }

        private DateTime m_date;
        public DateTime Date
        {
            get
            {
                return m_date;
            }
            set
            {
                m_date = value;
            }
        }
    }
Screenshots of the result file:

No comments:

Post a Comment