温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java 添加Word内容控件

发布时间:2020-06-30 02:16:09 来源:网络 阅读:282 作者:E_iceblue 栏目:编程语言

本文将介绍如何通过java程序在Word文件中添加内容控件,包括组合框内容控件(ComboBox)、复选框内容控件(CheckBox)、文本内容控件(Text)、图片内容控件(Picture)、日期选取器内容控件(DatePicker)、下拉列表内容控件等(DropDownList)等。下面将通过代码演示。

所需工具:Word类库(Free Spire.Doc for Java)
获取方法1:可通过官网下载jar包,并将lib文件夹下的Spire.Doc.jar文件导入Java程序;
获取方法1:通过maven仓库安装导入maven项目。

Java代码示例(供参考)

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;

import java.awt.*;
import java.util.Date;

public class AddStructureDocumentTagInline {
    public static void main(String[]args){
        //创建Word文档
        Document document = new Document();
        Section section = document.addSection();
        Paragraph paragraph = section.addParagraph();
        TextRange txtRange = paragraph.appendText("如何给Word文档添加内容控件:");
        txtRange.getCharacterFormat().setBold(true);
        txtRange.getCharacterFormat().setFontName("宋体");
        txtRange.getCharacterFormat().setTextColor(Color.RED);

        //添加组合框内容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("组合框内容控件:");
        txtRange.getCharacterFormat().setFontName("宋体");
        StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Combo_Box);
        sd.getSDTProperties().setAlias("组合框");
        sd.getSDTProperties().setTag("组合框");
        SdtComboBox cb = new SdtComboBox();
        cb.getListItems().add(new SdtListItem("中 国"));
        cb.getListItems().add(new SdtListItem("日 本"));
        cb.getListItems().add(new SdtListItem("澳大利亚"));
        sd.getSDTProperties().setControlProperties(cb);
        TextRange rt = new TextRange(document);
        rt.setText(cb.getListItems().get(0).getDisplayText());
        rt.getCharacterFormat().setFontName("宋体");
        sd.getSDTContent().getChildObjects().add(rt);

        //添加复选框内容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("复选框内容控件:  ");
        txtRange.getCharacterFormat().setFontName("宋体");
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Check_Box);
        sd.getSDTProperties().setAlias("复选框");
        sd.getSDTProperties().setTag("复选框");
        SdtCheckBox scb = new SdtCheckBox();
        sd.getSDTProperties().setControlProperties(scb);
        rt = new TextRange(document);
        sd.getChildObjects().add(rt);
        scb.setChecked(true);

        //添加文本内容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("文本内容控件: ");
        txtRange.getCharacterFormat().setFontName("宋体");
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Text);
        sd.getSDTProperties().setAlias("文本");
        sd.getSDTProperties().setTag("文本");
        SdtText text = new SdtText(true);
        text.isMultiline(true);
        sd.getSDTProperties().setControlProperties(text);
        rt = new TextRange(document);
        rt.setText("文 本");
        rt.getCharacterFormat().setFontName("宋体");
        sd.getSDTContent().getChildObjects().add(rt);

        //添加图片内容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("图片内容控件:  ");
        txtRange.getCharacterFormat().setFontName("宋体");
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setControlProperties(new SdtPicture());
        sd.getSDTProperties().setAlias("图片");
        sd.getSDTProperties().setTag("图片");
        DocPicture pic = new DocPicture(document);
        pic.setWidth(10f);
        pic.setHeight(10f);
        pic.loadImage("1.png");
        sd.getSDTContent().getChildObjects().add(pic);

        //添加日期选取器内容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("日期选取器内容控件:  ");
        txtRange.getCharacterFormat().setFontName("宋体");
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Date_Picker);
        sd.getSDTProperties().setAlias("日期");
        sd.getSDTProperties().setTag("日期");
        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);

        //添加下拉列表内容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("下拉列表内容控件:");
        txtRange.getCharacterFormat().setFontName("宋体");
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Drop_Down_List);
        sd.getSDTProperties().setAlias("下拉列表");
        sd.getSDTProperties().setTag("下拉列表");
        SdtDropDownList sddl = new SdtDropDownList();
        sddl.getListItems().add(new SdtListItem("选项1"));
        sddl.getListItems().add(new SdtListItem("选项2"));
        sd.getSDTProperties().setControlProperties(sddl);
        rt = new TextRange(document);
        rt.setText(sddl.getListItems().get(0).getDisplayText());
        rt.getCharacterFormat().setFontName("宋体");
        sd.getSDTContent().getChildObjects().add(rt);

        //保存结果文档
        document.saveToFile("addContentControls.docx", FileFormat.Docx_2013);
        document.dispose();
    }
}

内容控件添加效果:

Java 添加Word内容控件

(本文完)

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI