private void button1_Click(object sender, EventArgs e) { //Create a document Document document = new Document(); //Add a new section. Section section = document.AddSection(); //Add a paragraph Paragraph paragraph = section.AddParagraph(); //Append textRange for the paragraph TextRange txtRange = paragraph.AppendText("The following example shows how to add RichText content control in a Word document. \n"); //Append textRange txtRange = paragraph.AppendText("Add RichText Content Control: "); //Set the font format txtRange.CharacterFormat.Italic = true; //Create StructureDocumentTagInline for document StructureDocumentTagInline sdt = new StructureDocumentTagInline(document); //Add sdt in paragraph paragraph.ChildObjects.Add(sdt); //Specify the type sdt.SDTProperties.SDTType = SdtType.RichText; //Set displaying text SdtText text = new SdtText(true); text.IsMultiline = true; sdt.SDTProperties.ControlProperties = text; //Crate a TextRange TextRange rt = new TextRange(document); rt.Text = "Welcome to use "; rt.CharacterFormat.TextColor = Color.Green; sdt.SDTContent.ChildObjects.Add(rt); rt = new TextRange(document); rt.Text = "Spire.Doc"; rt.CharacterFormat.TextColor = Color.OrangeRed; sdt.SDTContent.ChildObjects.Add(rt); //Save the document. document.SaveToFile("Output.docx", FileFormat.Docx); //Launch the Word file. WordDocViewer("Output.docx"); }
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); }