We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Repeating Section Content Control example

Hello, I am looking into using the Repeating Section Content Control to populate a table in a Word 2016 docx document. After searching through the documentation and the Github I can’t see to find a good example of how to use the repeating content control with a set of data.

I would like to have a table with a row containing a set of content controls (i.e. Rich Text or Plain Text) with the row inside a repeating content control and then some how supply a set of data (datatable or xml) to populate the content controls and add more rows to reflect the number of rows in the data.

Is there any way this could be done?

Thanks

@software.sboe.ncsbe,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • XML data file
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Your expected DOCX Word document. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document by using Microsoft Word. Please also list the complete steps that you performed in MS Word to get the desired output
  • Please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please do not include Aspose.Words.dll files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

It’s not that I’m having a problem with the Repeating Section Content Control, it’s that I don’t know how to use with Aspose.

I’m just looking for an example. Once I have that I’ll be in a better position to ask subsequent questions if they’re needed.

@software.sboe.ncsbe,

I think, you can meet this requirement by using the ‘Mail Merge with Regions’ feature of Aspose.Words. Please refer to the following sections of documentation.

How to Execute Mail Merge with Regions
How to Use Nested Mail Merge Regions

I would like to avoid using Mail Merge.

I just want to see an example of using the Repeating Section Content Control through Aspose.

@software.sboe.ncsbe,

We are checking this scenario and will get back to you soon.

A post was split to a new topic: Repeating section functionality

@software.sboe.ncsbe,

We have logged your requirement in our issue tracking system. Your ticket number is WORDSNET-17614. We will further look into the details of this requirement and will keep you updated on the status of the linked issue.

@software.sboe.ncsbe,

Regarding WORDSNET-17614, here is an example of how to bind the repeating section of the input document to XML:

Document doc = new Document("D:\\Temp\\Repeating Section Content Control.docx");

Table table = doc.FirstSection.Body.Tables[0];

StructuredDocumentTag repeatingSection = (StructuredDocumentTag)table.GetChild(NodeType.StructuredDocumentTag, 0, false);
Debug.Assert(repeatingSection != null && repeatingSection.SdtType == SdtType.RepeatingSection);
StructuredDocumentTag repeatingItem = (StructuredDocumentTag)repeatingSection.GetChild(NodeType.StructuredDocumentTag, 0, false);
Debug.Assert(repeatingItem != null);
StructuredDocumentTag[] repeatingSdts = (StructuredDocumentTag[])repeatingItem.GetChildNodes(NodeType.StructuredDocumentTag, true).ToArray();
Debug.Assert(repeatingSdts.Length == 4);

const string xml =
    @"<?xml version=""1.0"" encoding=""utf-8""?>
    <root>
        <Employee>
            <Name>John Doe</Name>
            <HireDate>2018-03-01</HireDate>
            <Gender>Male</Gender>
            <Department>B</Department>
        </Employee>
        <Employee>
            <Name>Jane Adams</Name>
            <HireDate>2017-01-17</HireDate>
            <Gender>Female</Gender>
            <Department>A</Department>
        </Employee>
        <Employee>
            <Name>John Smith</Name>
            <HireDate>2017-12-01</HireDate>
            <Gender>Male</Gender>
            <Department>C</Department>
        </Employee>
    </root>";

CustomXmlPart customXmlPart = doc.CustomXmlParts.Add(Guid.NewGuid().ToString("B"), xml);

repeatingSection.XmlMapping.SetMapping(customXmlPart, "/root/Employee", null);
repeatingSdts[0].XmlMapping.SetMapping(customXmlPart, "/root/Employee/Name", null);
repeatingSdts[1].XmlMapping.SetMapping(customXmlPart, "/root/Employee/HireDate", null);
repeatingSdts[2].XmlMapping.SetMapping(customXmlPart, "/root/Employee/Gender", null);
repeatingSdts[3].XmlMapping.SetMapping(customXmlPart, "/root/Employee/Department", null);

doc.Save("D:\\temp\\18.11.docx");

Example if input XML contains namespace declarations:

const string xml =
    @"<?xml version=""1.0"" encoding=""utf-8""?>
    <ns1:root xmlns:ns1='SomeNamespace'>
        <ns1:Employee>
            <ns1:Name>John Doe</ns1:Name>
            <ns1:HireDate>2018-03-01</ns1:HireDate>
            <ns1:Gender>Male</ns1:Gender>
            <ns1:Department>B</ns1:Department>
        </ns1:Employee>
        <ns1:Employee>
            <ns1:Name>Jane Adams</ns1:Name>
            <ns1:HireDate>2017-01-17</ns1:HireDate>
            <ns1:Gender>Female</ns1:Gender>
            <ns1:Department>A</ns1:Department>
        </ns1:Employee>
        <ns1:Employee>
            <ns1:Name>John Smith</ns1:Name>
            <ns1:HireDate>2017-12-01</ns1:HireDate>
            <ns1:Gender>Male</ns1:Gender>
            <ns1:Department>C</ns1:Department>
        </ns1:Employee>
    </ns1:root>";
CustomXmlPart customXmlPart = doc.CustomXmlParts.Add(Guid.NewGuid().ToString("B"), xml);
 
const string namespaceMapping = "xmlns:ns0='SomeNamespace'";
repeatingSection.XmlMapping.SetMapping(customXmlPart, "/ns0:root/ns0:Employee", namespaceMapping);
repeatingSdts[0].XmlMapping.SetMapping(customXmlPart, "/ns0:root/ns0:Employee/ns0:Name", namespaceMapping);
repeatingSdts[1].XmlMapping.SetMapping(customXmlPart, "/ns0:root/ns0:Employee/ns0:HireDate", namespaceMapping);
repeatingSdts[2].XmlMapping.SetMapping(customXmlPart, "/ns0:root/ns0:Employee/ns0:Gender", namespaceMapping);
repeatingSdts[3].XmlMapping.SetMapping(customXmlPart, "/ns0:root/ns0:Employee/ns0:Department", namespaceMapping);
 
doc.Save(myDir + "Out2.docx");

Hope, this helps.

@software.sboe.ncsbe,

Regarding WORDSNET-17614, it is to update you that we have completed the analysis of this issue and come to a conclusion that we would not be able to implement the fix to this issue. Your issue (WORDSNET-17614) has now been closed with ‘Won’t Fix’ resolution. Please see my previous reply for details.