UNIT6-open / TemplateEngine.Docx

Smart docx template engine for .NET
Other
408 stars 136 forks source link

Empty content exception #55

Open zii-dmg opened 6 years ago

zii-dmg commented 6 years ago

When generating empty content (for example, RepeatContent) library throws. Some lists can be empty in generated document so I think library should not throw but generate as hidden.

Stack:

Unhandled Exception: System.ArgumentNullException: Value cannot be null.
Parameter name: source
   at System.Linq.Enumerable.SelectMany[TSource,TResult](IEnumerable`1 source, Func`2 selector)
   at TemplateEngine.Docx.Container.<>c.<get_FieldNames>b__26_1(RepeatContent t)
   at System.Linq.Enumerable.<SelectManyIterator>d__17`2.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Linq.Enumerable.<SelectManyIterator>d__17`2.MoveNext()
   at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at TemplateEngine.Docx.TableContent.get_FieldNames()
   at TemplateEngine.Docx.Processors.TableProcessor.FillContent(XContainer contentControl, IContentI
tem item)
   at TemplateEngine.Docx.Processors.TableProcessor.FillContent(XElement contentControl, IEnumerable
`1 items)
   at TemplateEngine.Docx.Processors.ContentProcessor.FillContent(XElement content, IEnumerable`1 da
ta)
   at TemplateEngine.Docx.TemplateProcessor.FillContent(Content content)
   at HiddenRepeatRepro.Program.Main(String[] args) in D:\Work\HiddenRepeatRepro\Program.cs:line 49

Template: EmptyContentTemplate.docx

Repro:

using System;
using System.IO;
using TemplateEngine.Docx;

namespace EmptyContentRepro
{
    static class Program
    {
        static void Main(string[] args)
        {
            string templateFile = "EmptyContentTemplate.docx";
            string outputFile = "EmptyContentRepro.docx";

            File.Delete(outputFile);
            File.Copy(templateFile, outputFile);

            var repeat = new RepeatContent("repeat1"); // empty
            var table = new TableContent("table1").AddRow(repeat);
            var content = new Content(table);

            using (var template = new TemplateProcessor(outputFile).SetRemoveContentControls(true))
            {
                template.FillContent(content);
                template.SaveChanges();
            }
        }
    }
}