Introduction
Content controls are ideal for creating templates because content controls can help us fix the position of content, specify the kind of content (for example, a date, a picture, or text), and restrict or enable editing on content. In this article, I will demonstrate how to insert the following types of content controls into a Word document in Java.
- Combo box
- Check box
- Text
- Picture
- Date picker
- Drop-down list
Required library
Before using the below code, we need to download Free Spire.Doc for Java and then import the Spire.Doc.jar file into our project. For maven project, you can refer this online tutorial to install Free Spire.Doc for Java from maven repository.
Code example
<span>import</span> <span>com.spire.doc.*</span><span>;</span><span>import</span> <span>com.spire.doc.documents.*</span><span>;</span><span>import</span> <span>com.spire.doc.fields.*</span><span>;</span><span>import</span> <span>java.util.Date</span><span>;</span><span>public</span> <span>class</span> <span>ContentControls</span> <span>{</span><span>public</span> <span>static</span> <span>void</span> <span>main</span><span>(</span><span>String</span><span>[]</span> <span>args</span><span>){</span><span>//create a new Word document</span><span>Document</span> <span>document</span> <span>=</span> <span>new</span> <span>Document</span><span>();</span><span>Section</span> <span>section</span> <span>=</span> <span>document</span><span>.</span><span>addSection</span><span>();</span><span>Paragraph</span> <span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>TextRange</span> <span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"The following example shows how to add content controls in a Word document."</span><span>);</span><span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>//add combo box content control</span><span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Combo Box Content Control: "</span><span>);</span><span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span><span>StructureDocumentTagInline</span> <span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span><span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Combo_Box</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"ComboBox"</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"ComboBox"</span><span>);</span><span>SdtComboBox</span> <span>cb</span> <span>=</span> <span>new</span> <span>SdtComboBox</span><span>();</span><span>cb</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Item 1"</span><span>));</span><span>cb</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Item 2"</span><span>));</span><span>cb</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Item 3"</span><span>));</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>cb</span><span>);</span><span>TextRange</span> <span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span><span>rt</span><span>.</span><span>setText</span><span>(</span><span>cb</span><span>.</span><span>getListItems</span><span>().</span><span>get</span><span>(</span><span>0</span><span>).</span><span>getDisplayText</span><span>());</span><span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span><span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>//add checkbox content control</span><span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Check Box Content Control: "</span><span>);</span><span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span><span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span><span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Check_Box</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"CheckBox"</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"CheckBox"</span><span>);</span><span>SdtCheckBox</span> <span>scb</span> <span>=</span> <span>new</span> <span>SdtCheckBox</span><span>();</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>scb</span><span>);</span><span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span><span>sd</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span><span>scb</span><span>.</span><span>setChecked</span><span>(</span><span>true</span><span>);</span><span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>//add text content control</span><span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Text Content Control: "</span><span>);</span><span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span><span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span><span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Text</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"Text"</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"Text"</span><span>);</span><span>SdtText</span> <span>text</span> <span>=</span> <span>new</span> <span>SdtText</span><span>(</span><span>true</span><span>);</span><span>text</span><span>.</span><span>isMultiline</span><span>(</span><span>true</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>text</span><span>);</span><span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span><span>rt</span><span>.</span><span>setText</span><span>(</span><span>"Text"</span><span>);</span><span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span><span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Picture Content Control: "</span><span>);</span><span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span><span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span><span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>new</span> <span>SdtPicture</span><span>());</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"Picture"</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"Picture"</span><span>);</span><span>DocPicture</span> <span>pic</span> <span>=</span> <span>new</span> <span>DocPicture</span><span>(</span><span>document</span><span>);</span><span>pic</span><span>.</span><span>setWidth</span><span>(</span><span>10</span><span>f</span><span>);</span><span>pic</span><span>.</span><span>setHeight</span><span>(</span><span>10</span><span>f</span><span>);</span><span>pic</span><span>.</span><span>loadImage</span><span>(</span><span>"logo.png"</span><span>);</span><span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>pic</span><span>);</span><span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>//add date picker content control</span><span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Date Picker Content Control: "</span><span>);</span><span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span><span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span><span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Date_Picker</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"Date"</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"Date"</span><span>);</span><span>SdtDate</span> <span>date</span> <span>=</span> <span>new</span> <span>SdtDate</span><span>();</span><span>date</span><span>.</span><span>setCalendarType</span><span>(</span><span>CalendarType</span><span>.</span><span>Default</span><span>);</span><span>date</span><span>.</span><span>setDateFormat</span><span>(</span><span>"yyyy.MM.dd"</span><span>);</span><span>date</span><span>.</span><span>setFullDate</span><span>(</span><span>new</span> <span>Date</span><span>());</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>date</span><span>);</span><span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span><span>rt</span><span>.</span><span>setText</span><span>(</span><span>"2018.12.25"</span><span>);</span><span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span><span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>//add drop-down list content control</span><span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span><span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Drop-Down List Content Control: "</span><span>);</span><span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span><span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span><span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Drop_Down_List</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"DropDownList"</span><span>);</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"DropDownList"</span><span>);</span><span>SdtDropDownList</span> <span>sddl</span> <span>=</span> <span>new</span> <span>SdtDropDownList</span><span>();</span><span>sddl</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Option 1"</span><span>));</span><span>sddl</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Option 2"</span><span>));</span><span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>sddl</span><span>);</span><span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span><span>rt</span><span>.</span><span>setText</span><span>(</span><span>sddl</span><span>.</span><span>getListItems</span><span>().</span><span>get</span><span>(</span><span>0</span><span>).</span><span>getDisplayText</span><span>());</span><span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span><span>//save and launch the file</span><span>document</span><span>.</span><span>saveToFile</span><span>(</span><span>"addContentControls.docx"</span><span>,</span> <span>FileFormat</span><span>.</span><span>Docx_2013</span><span>);</span><span>}</span><span>}</span><span>import</span> <span>com.spire.doc.*</span><span>;</span> <span>import</span> <span>com.spire.doc.documents.*</span><span>;</span> <span>import</span> <span>com.spire.doc.fields.*</span><span>;</span> <span>import</span> <span>java.util.Date</span><span>;</span> <span>public</span> <span>class</span> <span>ContentControls</span> <span>{</span> <span>public</span> <span>static</span> <span>void</span> <span>main</span><span>(</span><span>String</span><span>[]</span> <span>args</span><span>){</span> <span>//create a new Word document</span> <span>Document</span> <span>document</span> <span>=</span> <span>new</span> <span>Document</span><span>();</span> <span>Section</span> <span>section</span> <span>=</span> <span>document</span><span>.</span><span>addSection</span><span>();</span> <span>Paragraph</span> <span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>TextRange</span> <span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"The following example shows how to add content controls in a Word document."</span><span>);</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>//add combo box content control</span> <span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Combo Box Content Control: "</span><span>);</span> <span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span> <span>StructureDocumentTagInline</span> <span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span> <span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Combo_Box</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"ComboBox"</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"ComboBox"</span><span>);</span> <span>SdtComboBox</span> <span>cb</span> <span>=</span> <span>new</span> <span>SdtComboBox</span><span>();</span> <span>cb</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Item 1"</span><span>));</span> <span>cb</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Item 2"</span><span>));</span> <span>cb</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Item 3"</span><span>));</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>cb</span><span>);</span> <span>TextRange</span> <span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span> <span>rt</span><span>.</span><span>setText</span><span>(</span><span>cb</span><span>.</span><span>getListItems</span><span>().</span><span>get</span><span>(</span><span>0</span><span>).</span><span>getDisplayText</span><span>());</span> <span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>//add checkbox content control</span> <span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Check Box Content Control: "</span><span>);</span> <span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span> <span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span> <span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Check_Box</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"CheckBox"</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"CheckBox"</span><span>);</span> <span>SdtCheckBox</span> <span>scb</span> <span>=</span> <span>new</span> <span>SdtCheckBox</span><span>();</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>scb</span><span>);</span> <span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span> <span>sd</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span> <span>scb</span><span>.</span><span>setChecked</span><span>(</span><span>true</span><span>);</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>//add text content control</span> <span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Text Content Control: "</span><span>);</span> <span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span> <span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span> <span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Text</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"Text"</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"Text"</span><span>);</span> <span>SdtText</span> <span>text</span> <span>=</span> <span>new</span> <span>SdtText</span><span>(</span><span>true</span><span>);</span> <span>text</span><span>.</span><span>isMultiline</span><span>(</span><span>true</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>text</span><span>);</span> <span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span> <span>rt</span><span>.</span><span>setText</span><span>(</span><span>"Text"</span><span>);</span> <span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Picture Content Control: "</span><span>);</span> <span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span> <span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span> <span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>new</span> <span>SdtPicture</span><span>());</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"Picture"</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"Picture"</span><span>);</span> <span>DocPicture</span> <span>pic</span> <span>=</span> <span>new</span> <span>DocPicture</span><span>(</span><span>document</span><span>);</span> <span>pic</span><span>.</span><span>setWidth</span><span>(</span><span>10</span><span>f</span><span>);</span> <span>pic</span><span>.</span><span>setHeight</span><span>(</span><span>10</span><span>f</span><span>);</span> <span>pic</span><span>.</span><span>loadImage</span><span>(</span><span>"logo.png"</span><span>);</span> <span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>pic</span><span>);</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>//add date picker content control</span> <span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Date Picker Content Control: "</span><span>);</span> <span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span> <span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span> <span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Date_Picker</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"Date"</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"Date"</span><span>);</span> <span>SdtDate</span> <span>date</span> <span>=</span> <span>new</span> <span>SdtDate</span><span>();</span> <span>date</span><span>.</span><span>setCalendarType</span><span>(</span><span>CalendarType</span><span>.</span><span>Default</span><span>);</span> <span>date</span><span>.</span><span>setDateFormat</span><span>(</span><span>"yyyy.MM.dd"</span><span>);</span> <span>date</span><span>.</span><span>setFullDate</span><span>(</span><span>new</span> <span>Date</span><span>());</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>date</span><span>);</span> <span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span> <span>rt</span><span>.</span><span>setText</span><span>(</span><span>"2018.12.25"</span><span>);</span> <span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>//add drop-down list content control</span> <span>paragraph</span> <span>=</span> <span>section</span><span>.</span><span>addParagraph</span><span>();</span> <span>txtRange</span> <span>=</span> <span>paragraph</span><span>.</span><span>appendText</span><span>(</span><span>"Drop-Down List Content Control: "</span><span>);</span> <span>txtRange</span><span>.</span><span>getCharacterFormat</span><span>().</span><span>setItalic</span><span>(</span><span>true</span><span>);</span> <span>sd</span> <span>=</span> <span>new</span> <span>StructureDocumentTagInline</span><span>(</span><span>document</span><span>);</span> <span>paragraph</span><span>.</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>sd</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setSDTType</span><span>(</span><span>SdtType</span><span>.</span><span>Drop_Down_List</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setAlias</span><span>(</span><span>"DropDownList"</span><span>);</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setTag</span><span>(</span><span>"DropDownList"</span><span>);</span> <span>SdtDropDownList</span> <span>sddl</span> <span>=</span> <span>new</span> <span>SdtDropDownList</span><span>();</span> <span>sddl</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Option 1"</span><span>));</span> <span>sddl</span><span>.</span><span>getListItems</span><span>().</span><span>add</span><span>(</span><span>new</span> <span>SdtListItem</span><span>(</span><span>"Option 2"</span><span>));</span> <span>sd</span><span>.</span><span>getSDTProperties</span><span>().</span><span>setControlProperties</span><span>(</span><span>sddl</span><span>);</span> <span>rt</span> <span>=</span> <span>new</span> <span>TextRange</span><span>(</span><span>document</span><span>);</span> <span>rt</span><span>.</span><span>setText</span><span>(</span><span>sddl</span><span>.</span><span>getListItems</span><span>().</span><span>get</span><span>(</span><span>0</span><span>).</span><span>getDisplayText</span><span>());</span> <span>sd</span><span>.</span><span>getSDTContent</span><span>().</span><span>getChildObjects</span><span>().</span><span>add</span><span>(</span><span>rt</span><span>);</span> <span>//save and launch the file</span> <span>document</span><span>.</span><span>saveToFile</span><span>(</span><span>"addContentControls.docx"</span><span>,</span> <span>FileFormat</span><span>.</span><span>Docx_2013</span><span>);</span> <span>}</span> <span>}</span>import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.*; import java.util.Date; public class ContentControls { public static void main(String[] args){ //create 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."); section.addParagraph(); //add combo box content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Combo Box Content Control: "); txtRange.getCharacterFormat().setItalic(true); StructureDocumentTagInline sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Combo_Box); sd.getSDTProperties().setAlias("ComboBox"); sd.getSDTProperties().setTag("ComboBox"); SdtComboBox cb = new SdtComboBox(); cb.getListItems().add(new SdtListItem("Item 1")); cb.getListItems().add(new SdtListItem("Item 2")); cb.getListItems().add(new SdtListItem("Item 3")); sd.getSDTProperties().setControlProperties(cb); TextRange rt = new TextRange(document); rt.setText(cb.getListItems().get(0).getDisplayText()); sd.getSDTContent().getChildObjects().add(rt); section.addParagraph(); //add checkbox content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Check Box Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Check_Box); sd.getSDTProperties().setAlias("CheckBox"); sd.getSDTProperties().setTag("CheckBox"); SdtCheckBox scb = new SdtCheckBox(); sd.getSDTProperties().setControlProperties(scb); rt = new TextRange(document); sd.getChildObjects().add(rt); scb.setChecked(true); section.addParagraph(); //add text content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Text Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Text); sd.getSDTProperties().setAlias("Text"); sd.getSDTProperties().setTag("Text"); SdtText text = new SdtText(true); text.isMultiline(true); sd.getSDTProperties().setControlProperties(text); rt = new TextRange(document); rt.setText("Text"); sd.getSDTContent().getChildObjects().add(rt); section.addParagraph(); paragraph = section.addParagraph(); txtRange = paragraph.appendText("Picture Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setControlProperties(new SdtPicture()); sd.getSDTProperties().setAlias("Picture"); sd.getSDTProperties().setTag("Picture"); DocPicture pic = new DocPicture(document); pic.setWidth(10f); pic.setHeight(10f); pic.loadImage("logo.png"); sd.getSDTContent().getChildObjects().add(pic); section.addParagraph(); //add date picker content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Date Picker Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Date_Picker); sd.getSDTProperties().setAlias("Date"); sd.getSDTProperties().setTag("Date"); SdtDate date = new SdtDate(); date.setCalendarType(CalendarType.Default); date.setDateFormat("yyyy.MM.dd"); date.setFullDate(new Date()); sd.getSDTProperties().setControlProperties(date); rt = new TextRange(document); rt.setText("2018.12.25"); sd.getSDTContent().getChildObjects().add(rt); section.addParagraph(); //add drop-down list content control paragraph = section.addParagraph(); txtRange = paragraph.appendText("Drop-Down List Content Control: "); txtRange.getCharacterFormat().setItalic(true); sd = new StructureDocumentTagInline(document); paragraph.getChildObjects().add(sd); sd.getSDTProperties().setSDTType(SdtType.Drop_Down_List); sd.getSDTProperties().setAlias("DropDownList"); sd.getSDTProperties().setTag("DropDownList"); SdtDropDownList sddl = new SdtDropDownList(); sddl.getListItems().add(new SdtListItem("Option 1")); sddl.getListItems().add(new SdtListItem("Option 2")); sd.getSDTProperties().setControlProperties(sddl); rt = new TextRange(document); rt.setText(sddl.getListItems().get(0).getDisplayText()); sd.getSDTContent().getChildObjects().add(rt); //save and launch the file document.saveToFile("addContentControls.docx", FileFormat.Docx_2013); } }
© 版权声明
THE END
暂无评论内容