将内容控件插入Java中的Word文档

Introduction

内容控件非常适合创建模板,因为内容控件可以帮助我们固定内容的位置,指定内容的类型(例如日期,图片或文本)以及限制或启用对内容的编辑。 在本文中,我将演示如何将以下类型的内容控件插入Java中的Word文档中。

  1. 组合框复选框文本图片日期选择器下拉列表

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:
Alt Text

from: https://dev.to//eiceblue/insert-content-controls-into-word-document-in-java-1oc6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值