Good afternoon,
My company uses your product for specific document creation. One of our main need is to copy a document from a word template (in docx format) where we have inserted many content controls then fill out this copy with data from our database.
I can’t find any appropriate way to search a specific content control into my template and replace the content by the data I want. Is there any method(s) to get a specific content control of my document? If not, how do I have to build my template to use Aspose correctly?
Best Regards,
Quentin
Hi Quentin,
Thanks for your inquiry.
The
StructuredDocumentTag class represents a structured document tag (SDT or content control) in a document. The
StructuredDocumentTag.Tag
property specifies a tag associated with the current SDT node. A tag is
an arbitrary string which applications can associate with SDT in order
to identify it without providing a visible friendly name.
You can add/modify contents of content controls (
StructuredDocumentTag) by using Aspose.Words. Here is the code to insert plain text and select the Drop Down List item from the content control:
Document doc = new Document(@“c:\test\SDTs.docx”);
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true, true))
{
if (sdt.SdtType == SdtType.PlainText)
{
sdt.RemoveAllChildren();
Paragraph para = sdt.AppendChild(new Paragraph(doc)) as Paragraph;
Run run = new Run(doc, “new text goes here”);
para.AppendChild(run);
}
else if (sdt.SdtType == SdtType.DropDownList)
{
SdtListItem secondItem = sdt.ListItems[2];
sdt.ListItems.SelectedValue = secondItem;
}
}
doc.Save(@“c:\test\out.docx”);