Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Nov 05, 2025 4:22 pm

Hi

If I have a simple mail merge doc and use
Code: Select all
document.MailMerge.Execute(mergeData.Tables[0]);
I get a document with a section break after every resulting document (each row in mergeData) so effectively each letter would get printed on a different page.

We now have a requirement for more complex merge docs with sub merges (parent-child type setup) where data in the sub-merge is filtered based on a value in the main merge. We can implement this following the Mail Merge with Nested Regions example. However, it then does not add a section break after each main record. They just run on within one page, as per example attached.

I've tried all sorts of tricks to get a section break between each main record but failed. Is this possible?

Thanks

Jason

jaustin
 
Posts: 2
Joined: Mon Mar 23, 2015 3:03 pm

Thu Nov 06, 2025 6:12 am

Hello

Thank you for your letter. Which version of Spire.Doc you are currently using? I tested our latest Spire.Doc Pack(hot fix) Version:13.10.3 using the .NET Framework 4.8 project, the section breaks in the results document are effective. Please refer to the attached result and my testing code.
If you still have problems using the latest version, please provide your complete test code so that we can further determine the cause of the problem. Thanks in advance.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Thu Nov 06, 2025 11:15 am

Thank you for your prompt reply.

We are using an earlier version - I will upgrade, just in case.

However, we are not using document.MailMerge.Execute as per your code - this works fine with section breaks appearing.

We need to follow your "Mail Merge with Nested Regions" example as we need to repeat a section based on a filter using the data in the main section. So we call document.MailMerge.ExecuteWidthNestedRegion. It is in this scenario that we do not get section breaks.

Thanks

Jason

jaustin
 
Posts: 2
Joined: Mon Mar 23, 2015 3:03 pm

Fri Nov 07, 2025 9:09 am

Hello

Thank you for your feedback.
After merging using the ExecuteWidthNestedRegion method, there were indeed no section breaks. I also have tried the following code, but I have encountered a new "NullReferenceException" issue. I have logged this issue into our issue tracking system using the number SPIREDOC-11652. Our development team colleagues will further assist in investigating it. I will provide feedback to you once there is any progress. Thanks for your understanding.
Code: Select all
            List<DictionaryEntry> list = new List<DictionaryEntry>();
            DataSet dsData = new DataSet();
            dsData.ReadXml(@"in.xml");
            Document document = new Document();
            document.LoadFromFile(@"template.DOCX");
            DictionaryEntry dictionaryEntry = new DictionaryEntry("Main", string.Empty);
            list.Add(dictionaryEntry);
            dictionaryEntry = new DictionaryEntry("StaffAlert", "Main_Id = %Main.Main_Id%");
            list.Add(dictionaryEntry);
            document.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);
            document.MailMerge.ExecuteWidthNestedRegion(dsData, list);
            document.SaveToFile(@"out.docx", FileFormat.Docx)
        }

        static void MailMerge_MergeField(object sender, MergeFieldEventArgs args)
        {
            // Check if the current row index is greater than the lastIndex
            if (args.RowIndex > lastIndex)
            {
                // Update the lastIndex with the current row index
                lastIndex = args.RowIndex;

                // Add a page break before the current merge field
                AddPageBreakForMergeField(args.CurrentMergeField);
            }
        }

        static void AddPageBreakForMergeField(IMergeField mergeField)
        {
            //Find position of needing to add page break
            bool foundGroupStart = false;
            Paragraph paragraph = mergeField.PreviousSibling.Owner as Paragraph;
            MergeField previousMergeField = null;

            // Find the group start merge field by traversing the previous sibling paragraphs
            while (!foundGroupStart)
            {
                paragraph = paragraph.PreviousSibling as Paragraph;

                for (int i = 0; i < paragraph.Items.Count; i++)
                {
                    previousMergeField = paragraph.Items[i] as MergeField;

                    if ((previousMergeField != null) && (previousMergeField.Prefix == "GroupStart"))
                    {
                        foundGroupStart = true;
                        break;
                    }
                }
            }

            // Append a page break to the paragraph
            paragraph.AppendBreak(BreakType.PageBreak);
        }

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1533
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc