UNIT6-open / TemplateEngine.Docx

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

Error when using RepeatContent #73

Open ashadu21 opened 5 months ago

ashadu21 commented 5 months ago

Scenario - I have a document template with few fields lets say USERNAME, AGE, SEX and ADDRESS in a table. All these fields are setup as "Plain Text Content Control". This table row along will all these fields are setup as "Repeating Section Content Control" as shown below.

image

Question - How do I repeat the table row and populate these fields for various USESRNAME. I have written the below code.

File.Copy(Repeater, RepeaterResult);

var repeatertoFill = new Content(new RepeatContent("Repeater2")
    .AddItem(
        new FieldContent("NAME1", "Smith"),
        new FieldContent("AGE1", "50"),
        new FieldContent("SEX1", "M"),
        new FieldContent("ADDRESS1", "Address Smith"))
    .AddItem(
        new FieldContent("NAME1", "Mary"),
        new FieldContent("AGE1", "50"),
        new FieldContent("SEX1", "F"),
        new FieldContent("ADDRESS1", "Address Mary"))
    .AddItem(
        new FieldContent("NAME1", "David"),
        new FieldContent("AGE1", "50"),
        new FieldContent("SEX1", "M"),
        new FieldContent("ADDRESS1", "Address David"))

using (var outputDocument = new TemplateProcessor(RepeaterResult))
{
    outputDocument.FillContent(repeatertoFill);
    outputDocument.SaveChanges();
}

However, I get this error "Field content for field '' not found".

I would like my final word file to look something like this..

image

I am new to this package and hence any insights to use RepeatContent on a table is appreciated.

GoldSucc commented 4 months ago

You can use Table content instead, it works fine.