Hi Aspose team!
I’m using Aspose Total (21.11 for Words.NET) and sometimes executing simple MailMerge list numbering become broken. Below is an example of such a document.
DataSource:
public class DocumentDataSource
{
public string RegDate { get; set; }
public string No { get; set; }
}
public class DocumentMailMergeDataSource : IMailMergeDataSource
{
private bool _isRead;
private readonly DocumentDataSource _source;
public const string DocRegDateFieldName = "DOKREGDATUMS";
public const string DocNoFieldName = "DOKREGNUMURS";
public DocumentMailMergeDataSource(DocumentDataSource source)
{
_source = source;
}
public string TableName => null;
public IMailMergeDataSource GetChildDataSource(string tableName)
{
return null;
}
public bool GetValue(string fieldName, out object fieldValue)
{
switch (fieldName)
{
case DocRegDateFieldName:
fieldValue = _source.RegDate;
return true;
case DocNoFieldName:
fieldValue = _source.No;
return true;
default:
fieldValue = null;
return false;
}
}
public bool MoveNext()
{
if (_isRead)
{
return false;
}
_isRead = true;
return _isRead;
}
}
Code used for MailMerge:
var doc = new Words.Document(@"C:\\Docs\bad_doc.docx");
var documentDataSource = new DocumentDataSource()
{
RegDate = "Some date",
No = "Number"
};
doc.MailMerge.UseNonMergeFields = true;
doc.MailMerge.PreserveUnusedTags = false;
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOptions.RemoveUnusedRegions;
doc.MailMerge.Execute(new DocumentMailMergeDataSource(documentDataSource));
doc.Save(@"C:\\Docs\afterMerge.docx");
bad_doc.docx (170.4 KB)
After MailMerge list numbering is:
1.Some text.
1.Some other text:
1.1. Bla-bla-bla;
1.2. Again bla-bla-bla.