We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Document template saved several times in PDF?

Hello,

I have a docx document of 8 pages with merge fields.
I use IMailMergeDataSource to provide a Dictionary<string, string> collection of data.
After MailMerge.Execute(dataSource), I save the document in PDF.
When I open my PDF, I have 256 pages (32 times the docx document) !!!

Why ?

Here is my method :

public byte[] GeneratePdfFromWordTemplate(byte[] wordTemplateData, ConventionMailMergeDataSource dataSource)
        {
            var wordTemplateDocument = new Document(new MemoryStream(wordTemplateData));
            wordTemplateDocument.MailMerge.TrimWhitespaces = false;
            wordTemplateDocument.MailMerge.Execute(dataSource);

            MemoryStream pdfDocumentStream = new MemoryStream();
            wordTemplateDocument.Save(pdfDocumentStream, SaveFormat.Pdf);
            return pdfDocumentStream.ToArray();
        }

And here is the IMailMergeDataSource implementation :

public class ConventionMailMergeDataSource : IMailMergeDataSource
    {
        private readonly Dictionary<string, string> _dataCollection;
        private int _recordIndex;

        /// <summary>
        /// Name of the data source. Used by Aspose.Words only when executing mail merge with repeatable regions.
        /// </summary>
        public string TableName { get; }


        public ConventionMailMergeDataSource(string tableName)
        {
            TableName = tableName;
            _dataCollection = new Dictionary<string, string>();

            // When the data source is initialized, it must be positioned before the first record.
            _recordIndex = -1;
        }

        public void AddData(Dictionary<string,string> newData)
        {
            foreach (var data in newData)
                _dataCollection[data.Key] = data.Value;
        }

        public bool MoveNext()
        {
            if (!IsEof)
                _recordIndex++;

            return (!IsEof);
        }

        public bool GetValue(string fieldName, out object fieldValue)
        {
            string stringFieldValue = null;
            bool result = _dataCollection.TryGetValue(fieldName, out stringFieldValue);
            fieldValue = stringFieldValue;
            return result;
        }

        public IMailMergeDataSource GetChildDataSource(string tableName)
        {
            return null;
        }

        private bool IsEof
        {
            get { return (_recordIndex >= _dataCollection.Count); }
        }
    }

Thank you for your help.

@PhilippeGroupUp,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output PDF file that shows the undesired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

It’s my IMailMergeDataSource implementation the problem. But I don’t know why ?

If I extract field names and values from my IMailMergeDataSource as this :
var fieldNames = dataSource.DataCollection.Keys.ToArray();
var fieldValues = dataSource.DataCollection.Values.ToArray();

And I execute :
wordTemplateDocument.MailMerge.Execute(fieldNames, fieldValues);

That’s works. I have a Pdf with the same number of pages of my docx template.

@PhilippeGroupUp,

Perhaps, there is some issue in your implementation of IMailMergeDataSource. Please share the requested detail for investigation. Thanks for your cooperation.