MailMerge Issue - LF

I found an issue where when there is a mergefield with a line feed in the IF statement, it does not collapse up during mailmerge when empty.

<MERGEFIELD Data1>
<IF <MERGEFIELD Data2>
= "TEST" "" "">
<MERGEFIELD Data3>

In the above example, the output should be 2 lines of text. Instead we get a blank line in between the 2 lines of text. This is caused by the line feed before the = operator. I need to detect this bug so that the documents don’t hit our system when they are setup like this. How can I detect IF fields with the line feeds in the whitespace area (not in the left hand, right hand, true, or false areas).

@Robert343

To detect IF fields with line feeds in the whitespace area, you can implement a script that scans your document for the specific pattern. Look for instances of <<IF followed by any whitespace characters (including line feeds) before the = operator.

You can use regular expressions to identify these patterns. For example, a regex pattern like <<IF\s+<<MERGEFIELD\s+\w+>>\s*=\s* can help you find IF statements with unwanted line feeds.

Once identified, you can flag these documents for review before they enter your system. This should help maintain the integrity of your mail merge outputs.

Best,
Professionalize.Discourse Bot

That did not work

@Robert343 Could you please attach your problematic input document and simple code that will allow us to reproduce the problem? We will check the issue and provide you more information.

Have you tried using MailMergeCleanupOptions.RemoveEmptyParagraphs?

Hi. I have tried all of the cleanup options. I am attaching the docx and the output differences from version 14.5 and 25.6. Below is the code used to replicate the issue. Again, the issue is related to the line break inside the white text before the operator in the IF condition. The old aspose ignores it and the new aspose will output it into the final PDF.

25.6.pdf (15.8 KB)

14.5.pdf (15.0 KB)

merge.docx (18.9 KB)

Aspose.Words.License lic = new Aspose.Words.License();
lic.SetLicense("C:\\aspose\\Aspose.Total.NET.lic");

Aspose.Words.Loading.LoadOptions loadOptions = new Aspose.Words.Loading.LoadOptions();
loadOptions.PreserveIncludePictureField = true;

DataTable table = new DataTable("TestData");

table.Columns.Add("ClientName", typeof(string));
table.Columns.Add("ExtraData", typeof(string));

DataRow row = table.NewRow();
row["ClientName"] = "Acme Services";
row["ExtraData"] = "";

table.Rows.Add(row);

using (FileStream docStream = new FileStream("C:\\Temp\\merge.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    Aspose.Words.Document doc = new Aspose.Words.Document(docStream, loadOptions);


    // Optional cleanup: remove leftover fields/empty paragraphs if needed
    doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions |
                                            MailMergeCleanupOptions.RemoveContainingFields |
                                            MailMergeCleanupOptions.RemoveEmptyParagraphs |
                                            MailMergeCleanupOptions.RemoveUnusedFields;

    // Execute mail merge with the single-row DataTable
    doc.MailMerge.Execute(table);

    doc.Save("C:\\Temp\\test_output.pdf");
}

@Robert343
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28662

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.