コード例 #1
0
        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);
        }
コード例 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            //Creat a new word document.
            Document  document  = new Document();
            Section   section   = document.AddSection();
            Paragraph paragraph = section.AddParagraph();
            TextRange txtRange  = paragraph.AppendText("The following example shows how to add content controls in a Word document.");

            paragraph = section.AddParagraph();

            //Add Combo Box Content Control.
            paragraph = section.AddParagraph();
            txtRange  = paragraph.AppendText("Add Combo Box Content Control:  ");
            txtRange.CharacterFormat.Italic = true;
            StructureDocumentTagInline sd = new StructureDocumentTagInline(document);

            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.ComboBox;
            SdtComboBox cb = new SdtComboBox();

            cb.ListItems.Add(new SdtListItem("Spire.Doc"));
            cb.ListItems.Add(new SdtListItem("Spire.XLS"));
            cb.ListItems.Add(new SdtListItem("Spire.PDF"));
            sd.SDTProperties.ControlProperties = cb;
            TextRange rt = new TextRange(document);

            rt.Text = cb.ListItems[0].DisplayText;
            sd.SDTContent.ChildObjects.Add(rt);

            section.AddParagraph();

            //Add Text Content Control.
            paragraph = section.AddParagraph();
            txtRange  = paragraph.AppendText("Add Text Content Control:  ");
            txtRange.CharacterFormat.Italic = true;
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.Text;
            SdtText text = new SdtText(true);

            text.IsMultiline = true;
            sd.SDTProperties.ControlProperties = text;
            rt      = new TextRange(document);
            rt.Text = "Text";
            sd.SDTContent.ChildObjects.Add(rt);

            section.AddParagraph();

            //Add Picture Content Control.
            paragraph = section.AddParagraph();
            txtRange  = paragraph.AppendText("Add Picture Content Control:  ");
            txtRange.CharacterFormat.Italic = true;
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.Picture;
            DocPicture pic = new DocPicture(document);

            pic.Width  = 10;
            pic.Height = 10;
            pic.LoadImage(Image.FromFile(@"..\..\..\..\..\..\Data\logo.png"));
            sd.SDTContent.ChildObjects.Add(pic);

            section.AddParagraph();

            //Add Date Picker Content Control.
            paragraph = section.AddParagraph();
            txtRange  = paragraph.AppendText("Add Date Picker Content Control:  ");
            txtRange.CharacterFormat.Italic = true;
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.DatePicker;
            SdtDate date = new SdtDate();

            date.CalendarType = CalendarType.Default;
            date.DateFormat   = "yyyy.MM.dd";
            date.FullDate     = DateTime.Now;
            sd.SDTProperties.ControlProperties = date;
            rt      = new TextRange(document);
            rt.Text = "1990.02.08";
            sd.SDTContent.ChildObjects.Add(rt);

            section.AddParagraph();

            //Add Drop-Down List Content Control.
            paragraph = section.AddParagraph();
            txtRange  = paragraph.AppendText("Add Drop-Down List Content Control:  ");
            txtRange.CharacterFormat.Italic = true;
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.DropDownList;
            SdtDropDownList sddl = new SdtDropDownList();

            sddl.ListItems.Add(new SdtListItem("Harry"));
            sddl.ListItems.Add(new SdtListItem("Jerry"));
            sd.SDTProperties.ControlProperties = sddl;
            rt      = new TextRange(document);
            rt.Text = sddl.ListItems[0].DisplayText;
            sd.SDTContent.ChildObjects.Add(rt);

            //Save the document.
            string resultfile = "Output.docx";

            document.SaveToFile(resultfile, FileFormat.Docx);

            //Launch the Word file.
            FileViewer(resultfile);
        }