ms-wordasp.net-core-2.0syncfusionmailmergedocx-mailmerge

How to remove the extra page at the end of a word document which created during mail merge


I have written a piece of code to create a word document by mail merge using Syncfusion (Assembly Syncfusion.DocIO.Portable, Version=17.1200.0.50,), Angular 7+ and .NET Core. Please see the code below.

private MemoryStream MergePaymentPlanInstalmentsScheduleToPdf(List<PaymentPlanInstalmentReportModel> 
    PaymentPlanDetails, byte[] templateFileBytes)
{
                if (templateFileBytes == null || templateFileBytes.Length == 0)
                {
                    return null;
                }
                var templateStream = new MemoryStream(templateFileBytes);
                var pdfStream = new MemoryStream();
                WordDocument mergeDocument = null;
                using (mergeDocument = new WordDocument(templateStream, FormatType.Docx))
                {
                    if (mergeDocument != null)
                    {
                        var mergeList = new List<PaymentPlanInstalmentScheduleMailMergeModel>();
                        var obj = new PaymentPlanInstalmentScheduleMailMergeModel();
                        obj.Applicants = 0;

                        if (PaymentPlanDetails != null && PaymentPlanDetails.Any()) {
                            var applicantCount = PaymentPlanDetails.GroupBy(a => a.StudentID)
                                .Select(s => new
                                {
                                    StudentID = s.Key,
                                    Count = s.Select(a => a.StudentID).Distinct().Count()                
                                });
                            obj.Applicants = applicantCount?.Count() > 0 ?  applicantCount.Count() : 0;
                        }

                        mergeList.Add(obj);

                        var reportDataSource = new MailMergeDataTable("Report", mergeList);
                        var tableDataSource = new MailMergeDataTable("PaymentPlanDetails", PaymentPlanDetails);

                        List<DictionaryEntry> commands = new List<DictionaryEntry>();
                        commands.Add(new DictionaryEntry("Report", ""));
                        commands.Add(new DictionaryEntry("PaymentPlanDetails", ""));
                        MailMergeDataSet ds = new MailMergeDataSet();
                        ds.Add(reportDataSource);
                        ds.Add(tableDataSource);

                        mergeDocument.MailMerge.ExecuteNestedGroup(ds, commands);
                        mergeDocument.UpdateDocumentFields();
                        using (var converter = new DocIORenderer())
                        {
                            using (var pdfDocument = converter.ConvertToPDF(mergeDocument))
                            {
                                pdfDocument.Save(pdfStream);
                                pdfDocument.Close();
                            }
                        }
                        mergeDocument.Close();
                    }
                }           
                return pdfStream;
 }

Once the document is generated, I notice there is a blank page (with the footer) at the end. I searched for a solution on the internet over and over again, but I was not able to find a solution. According to experts, I have done the initial checks such as making sure that the initial word template file has no page breaks, etc.

I am wondering if there is something that I can do from my code to remove any extra page breaks or anything like that, which can cause this.

Any other suggested solution for this, even including MS Word document modifications also appreciated.


Solution

  • Please refer the below documentation link to remove empty page at the end of Word document using Syncfusion Word library (Essential DocIO). https://www.syncfusion.com/kb/10724/how-to-remove-empty-page-at-end-of-word-document

    Please reuse the code snippet before converting Word to PDF in your sample application.

    Note: I work for Syncfusion.