UNIT6-open / TemplateEngine.Docx

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

Hidden RepeatContent inside TableContent generates malformed document #54

Open zii-dmg opened 6 years ago

zii-dmg commented 6 years ago

Word error message when opening (in russian): worderror

Template: HiddenRepeatReproTemplate.docx

Repro:

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

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

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

            var repeat = new RepeatContent("repeat1")
                .AddItem(new FieldContent("val", "123"))
                .AddItem(new FieldContent("val", "456"));

            repeat.Hide(); // breaks document

            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();
            }
        }
    }
}