C # Adding Content controls to Word documents

Source: Internet
Author: User
Tags dateformat

C # to Word Document Add content control

In MS Word, we can create a structured Word document by inserting a predefined module into a Word document, specifying the module's content format (slice, date, list or formatted text, and so on) through a content control. Here's a look at how to use C # to add content controls such as combo boxes, text, pictures, date pickers, and drop-down lists to Word documents (here I have a word component spire.doc).

To add a combo box content control

A combo box is used to display a list of items that the user can select. Unlike drop-down lists, combo boxes allow users to edit or add items.

//Add a content control to the paragraph and specify its SDT type as combo BoxStructuredocumenttaginline SD =NewStructuredocumenttaginline (document);p aragraph. Childobjects.add (SD); SD. Sdtproperties.sdttype=Sdttype.combobox;//Create a combo Box, add an option and assign it to the content controlSdtcombobox cb =NewSdtcombobox (); CB. Listitems.add (NewSdtlistitem ("Cat") ); CB. Listitems.add (NewSdtlistitem ("Dog") ); Sd. Sdtproperties.controlproperties=CB;//Set display textTextRange RT =NewTextRange (document); Rt. Text= cb. listitems[0]. Displaytext;sd. SDTCONTENT.CHILDOBJECTS.ADD (RT);

Add a text content control

A plain text control contains text, but cannot contain other items, such as tables, pictures, or other content controls. In addition, all text in a plain text control has the same format.

The steps to add a text content control are similar to adding a combo box content control, with the following core code:

Paragraph =new=new sdttext (truetrue  =new"Text"; sd. SDTCONTENT.CHILDOBJECTS.ADD (RT);

Add a picture content control

Picture controls are used to display images. We can specify the image at design time or at runtime, and the user can also click the control to select the image to insert in the document.

Core code:

Paragraph =new=new  ;p ic. LoadImage (Image.FromFile ("c:\\icon.jpg")), SD. SDTContent.ChildObjects.Add (pic);

Add a date picker content control

The date picker provides a calendar UI for selecting dates. The calendar is displayed when the end user clicks the drop-down arrow in the control.

Core code:

Paragraph =Section . Addparagraph (); SD=NewStructuredocumenttaginline (document);p aragraph. Childobjects.add (SD); SD. Sdtproperties.sdttype=Sdttype.datepicker; Sdtdate Date=Newsdtdate ();d ate. CalendarType=calendartype.default;date. DateFormat="yyyy. Mm.dd";d ate. Fulldate=DATETIME.NOW;SD. Sdtproperties.controlproperties=Date;rt=NewTextRange (document); Rt. Text="1990.02.08"; Sd. SDTCONTENT.CHILDOBJECTS.ADD (RT);

Add a drop-down list content control

The drop-down list shows a list of items that the user can select. Unlike combo boxes, a drop-down list does not allow users to add or edit items.

Core code:

Paragraph =Section . Addparagraph (); SD=NewStructuredocumenttaginline (document);p aragraph. Childobjects.add (SD); SD. Sdtproperties.sdttype=sdttype.dropdownlist; Sdtdropdownlist SDDL=Newsdtdropdownlist (); Sddl. Listitems.add (NewSdtlistitem ("Harry") ; Sddl. Listitems.add (NewSdtlistitem ("Jerry") ); Sd. Sdtproperties.controlproperties=Sddl;rt=NewTextRange (document); Rt. Text= SDDL. listitems[0]. Displaytext;sd. SDTCONTENT.CHILDOBJECTS.ADD (RT);

All code:

usingSystem;usingSystem.Drawing;usingSpire.doc;usingSpire.Doc.Documents;usingSpire.Doc.Fields;namespaceinsert_content_control_in_word_document{classProgram {Static voidMain (string[] args) {            //Create a new Word documentDocument document =NewDocument (); Section Section=document.            AddSection (); Paragraph Paragraph=Section .             Addparagraph (); //To Add a combo box content controlStructuredocumenttaginline SD =NewStructuredocumenttaginline (document); Paragraph.            Childobjects.add (SD); Sd. Sdtproperties.sdttype=Sdttype.combobox; Sdtcombobox CB=NewSdtcombobox (); Cb. Listitems.add (NewSdtlistitem ("Cat")); Cb. Listitems.add (NewSdtlistitem ("Dog")); Sd. Sdtproperties.controlproperties=CB; TextRange RT=NewTextRange (document); Rt. Text= cb. listitems[0].            DisplayText; Sd.            SDTCONTENT.CHILDOBJECTS.ADD (RT); //Add a text content controlParagraph =Section .            Addparagraph (); SD=NewStructuredocumenttaginline (document); Paragraph.            Childobjects.add (SD); Sd. Sdtproperties.sdttype=Sdttype.text; Sdttext text=NewSdttext (true); Text. Ismultiline=true; Sd. Sdtproperties.controlproperties=text; RT=NewTextRange (document); Rt. Text="Text"; Sd.             SDTCONTENT.CHILDOBJECTS.ADD (RT); //Add a picture content controlParagraph =Section .            Addparagraph (); SD=NewStructuredocumenttaginline (document); Paragraph.            Childobjects.add (SD); Sd. Sdtproperties.sdttype=sdttype.picture; Docpicture pic=NewDocpicture (document) {Width =Ten, Height =Ten }; Pic. LoadImage (Image.FromFile ("c:\\icon.jpg")); Sd.             SDTContent.ChildObjects.Add (pic); //Add a date picker content controlParagraph =Section .            Addparagraph (); SD=NewStructuredocumenttaginline (document); Paragraph.            Childobjects.add (SD); Sd. Sdtproperties.sdttype=Sdttype.datepicker; Sdtdate Date=Newsdtdate (); Date. CalendarType=Calendartype.default; Date. DateFormat="yyyy. Mm.dd"; Date. Fulldate=DateTime.Now; Sd. Sdtproperties.controlproperties=date; RT=NewTextRange (document); Rt. Text="1990.02.08"; Sd.             SDTCONTENT.CHILDOBJECTS.ADD (RT); //Add a drop- down list content controlParagraph =Section .            Addparagraph (); SD=NewStructuredocumenttaginline (document); Paragraph.            Childobjects.add (SD); Sd. Sdtproperties.sdttype=sdttype.dropdownlist; Sdtdropdownlist SDDL=Newsdtdropdownlist (); Sddl. Listitems.add (NewSdtlistitem ("Harry")); Sddl. Listitems.add (NewSdtlistitem ("Jerry")); Sd. Sdtproperties.controlproperties=SDDL; RT=NewTextRange (document); Rt. Text= SDDL. listitems[0].            DisplayText; Sd.             SDTCONTENT.CHILDOBJECTS.ADD (RT); //Save and restart the file            stringResultfile ="Sample.docx"; Document.            SaveToFile (Resultfile, Fileformat.docx);                   System.Diagnostics.Process.Start (Resultfile); }    }}

C # Adding Content controls to Word documents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.