UNIT6-open / TemplateEngine.Docx

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

RepeatContent appears not to work #31

Closed RThomasHyde closed 7 years ago

RThomasHyde commented 7 years ago

Either RepeatContent doesn't work, or I have the wrong idea about how to use it. I have created a very simple docx (in Word 2013) containing a single repeating section with Title and Tag both set to "TestRepeater." The content of the repeating section is a single text field named/tagged "TestField" followed by a carriage return. When I try to fill it with two lines of content using code like below, I end up with a "Field content for field '' not found" error message in the output, and the repeating section remains unfilled and is not removed, but is duplicated.

var valuesToFill = new Content(
    new RepeatContent("TestRepeater")
        .AddItem(new FieldContent("TestField", "value 1"))
        .AddItem(new FieldContent("Testfield", "value 2")));

using (var outputDocument = new TemplateProcessor(@"f:\RepeatContentTest.docx")
    .SetRemoveContentControls(true))
{
    outputDocument
        .FillContent(valuesToFill)
        .SaveChanges();
}

What I really want is a repeating section with a header field and a table inside, so that I can generate a series of (an unknown number of) tables with each table bearing a title. But until I have a repeating section working, I don't see how to proceed.

RepeatingSectionTest.docx

RThomasHyde commented 7 years ago

Never mind, I was misguided. I assumed that RepeatingContent was the same thing as a repeating section in docx, but after looking through unit tests I have seen the error of my ways

vetonshala commented 7 years ago

Hi RThomasHyde, I didn't understand you exactly, so RepeatingContent we can't use as repeating section in docx. Then how can I fill repeating section with data for example: [This is repeating section [TestField1] etc]

Then output I want to be like: This is repeating section value 1 etc This is repeating section value 2 etc

How can I accomplish this? Can you explain please!

quisitive-crogers commented 7 years ago

I finally figured this out. In order to repeat content, you just put your content placeholders inside a Rich Content control. Do not use the Repeat Section Content Control as it will not work. The template engine takes care of the process of making copies of the items.