private void button1_Click(object sender, EventArgs e) { // Create a new document and load from file string input = @"..\..\..\..\..\..\Data\ComboBox.docx"; Document doc = new Document(); doc.LoadFromFile(input); //Get the combo box from the file foreach (Section section in doc.Sections) { foreach (DocumentObject bodyObj in section.Body.ChildObjects) { if (bodyObj.DocumentObjectType == DocumentObjectType.StructureDocumentTag) { //If SDTType is ComboBox if ((bodyObj as StructureDocumentTag).SDTProperties.SDTType == SdtType.ComboBox) { SdtComboBox combo = (bodyObj as StructureDocumentTag).SDTProperties.ControlProperties as SdtComboBox; //Remove the second list item combo.ListItems.RemoveAt(1); //Add a new item SdtListItem item = new SdtListItem("D", "D"); combo.ListItems.Add(item); //If the value of list items is "D" foreach (SdtListItem sdtItem in combo.ListItems) { if (string.CompareOrdinal(sdtItem.Value, "D") == 0) { //Select the item combo.ListItems.SelectedValue = sdtItem; } } } } } } //Save the document and launch it string output = "ComboBoxItem.docx"; doc.SaveToFile(output, FileFormat.Docx2013); Viewer(output); }
public void ModifyContentControls() { //ExStart:ModifyContentControls Document doc = new Document(MyDir + "Structured document tags.docx"); foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true)) { switch (sdt.SdtType) { case 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); break; } case SdtType.DropDownList: { SdtListItem secondItem = sdt.ListItems[2]; sdt.ListItems.SelectedValue = secondItem; break; } case SdtType.Picture: { Shape shape = (Shape)sdt.GetChild(NodeType.Shape, 0, true); if (shape.HasImage) { shape.ImageData.SetImage(ImagesDir + "Watermark.png"); } break; } } } doc.Save(ArtifactsDir + "WorkingWithSdt.ModifyContentControls.docx"); //ExEnd:ModifyContentControls }
public static void ModifyContentControls(string dataDir) { // ExStart:ModifyContentControls // Open an existing document Document doc = new Document(dataDir + "CheckBoxTypeContentControl.docx"); foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, 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; } else if (sdt.SdtType == SdtType.Picture) { Shape shape = (Shape)sdt.GetChild(NodeType.Shape, 0, true); if (shape.HasImage) { shape.ImageData.SetImage(dataDir + "Watermark.png"); } } } dataDir = dataDir + "ModifyContentControls_out.docx"; doc.Save(dataDir); // ExEnd:ModifyContentControls Console.WriteLine("\nPlain text box, drop down list and picture content modified successfully.\nFile saved at " + dataDir); }