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

Free Support Forum - aspose.com

Add a New Repeating Section Content Control in Word Document Programmatically using C# or Java

Hi,

I would like to add a new Repeating section content control programmatically - in a Word 2013 document - using Aspose.Words. I am trying to create a Word 2013 template programmatically using aspose.words.

The "SdtType" enumeration do not support any repeating content control.. Please provide me a sample of how a new repeating section control can be added.

Thanks....

Hi Nandha,


Thanks for your inquiry. Could you please attach your expected Word document here for our reference. 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 using Microsoft Word. We will then provide you code to achieve the same using Aspose.Words.

Best regards,

Hi,

Please find attached a Word 2013 document - where I have created a table with repeating section content control. .. These are the steps that I followed:

- Created a new table with 2 columns

- Added a 'Plain Text' content control in each column (named Cell1 and Cell2)

- Added a 'Repeating Section' content control - that wraps these plain text content controls.

Now, if this document is used as a 'template', the user can add multiple rows to it and Cell1 and Cell2 content controls will repeat for every row.

Please let me know, how this can be achieved using Aspose.Words

Thanks,

Nandha

Hi Nandha,

Thanks for the additional information. We have logged this requirement in our issue tracking system as WORDSNET-12439. Our product team will further look into the details of this problem and we will keep you updated on the status of this issue. We apologize for your inconvenience.

Best regards,

@NandhaV,

It is to update you that starting from the 19.9 versions, you can use Aspose.Words for .NET and Aspose.Words for Java APIs to create Structured Document Tag nodes (Content Controls) of the Repeating Section and Repeating Section Item types. The new items have also been added into the SdtType enumeration.

You can use the following C# example code to create a Table Repeating Section and map it to a custom XML part:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

CustomXmlPart xmlPart = doc.CustomXmlParts.Add("Books",
"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" +
"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" +
"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>");

Table table = builder.StartTable();

builder.InsertCell();
builder.Write("Title");

builder.InsertCell();
builder.Write("Author");

builder.EndRow();
builder.EndTable();

StructuredDocumentTag repeatingSectionSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);

StructuredDocumentTag repeatingSectionItemSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);

Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);

StructuredDocumentTag titleSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
row.AppendChild(titleSdt);

StructuredDocumentTag authorSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
row.AppendChild(authorSdt);

doc.Save("E:\\Temp\\Table Repeating Section mapped to a custom XML part.docx");