资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Java添加Word内容控件-创新互联

本文将介绍如何通过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内容控件

(本文完)

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:Java添加Word内容控件-创新互联
转载来于:http://cdkjz.cn/article/ioihi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220