UNIT6-open / TemplateEngine.Docx

Smart docx template engine for .NET
Other
406 stars 137 forks source link

Error while hiding nested RepeatContent. #47

Open mikhail-ivanov-travelline opened 6 years ago

mikhail-ivanov-travelline commented 6 years ago

Exception locate is file RepeatProcessor.cs, line 105 (code: contentControl.DescendantsAndSelf(W.sdt).Remove();)

Error message: The parent is missing

Input file: InputTemplate.docx

Programm code:

using System.IO;
using TemplateEngine.Docx;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            File.Delete("OutputDocument.docx");
            File.Copy("InputTemplate.docx", "OutputDocument.docx");

            var valuesToFill = new Content(
                new RepeatContent("RepeatParentContent")
                    .AddItem(
                        new RepeatContent("RepeatChildrenContent").Hide()
                            .AddItem(new FieldContent("OrganizationName", "Рога и копыта")))
            );

            using (var outputDocument = new TemplateProcessor("OutputDocument.docx")
                .SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            }
        }
    }
}