Skip to main content

Insert Content Controls into Word Document in Java

 
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
Free Spire.Doc for Java
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
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);
    }
}
 
Output:
 

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>                   <url>http: //repo.e-iceblue.com/nexus/content/groups/public/</url>                </repository>       </repositories>       <dependencies>           <dependency>               <g

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();         workbook.loadFromFile( "Sample.xlsx" );         //Fit to page         workbook.getConverterSetting().setShee

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>                   <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>               </repository>       </repositories>       <dependencies>           <dependency>               <groupId>