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> ...

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>     ...

Remove Duplicate Rows in Excel in C# and VB.NET

When an Excel file contains a huge amount of records, there might be duplicate records as well. In this blog, I am going to show you how to remove the duplicate rows in an Excel file programmatically in C# and VB.NET. The library I used: Free Spire.XLS for .NET Free Spire.XLS for .NET is a feature-rich Excel API offered by E-iceblue. It can be easily integrated in your .NET (C#, VB.NET, ASP.NET, .NET Core) applications to create, read, edit, convert and print Excel files without using Microsoft Office. Before coding, you need to get Free Spire.XLS for .NET by installing it via NuGet or downloading it via the official website . C# Code using  Spire.Xls;   using  System.Linq;      namespace  RemoveDuplicateRows   {        class  Program       {            static   void  Main( string [] args) ...