溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java 添加Word內(nèi)容控件

發(fā)布時(shí)間:2020-06-30 02:16:09 來(lái)源:網(wǎng)絡(luò) 閱讀:296 作者:E_iceblue 欄目:編程語(yǔ)言

本文將介紹如何通過(guò)java程序在Word文件中添加內(nèi)容控件,包括組合框內(nèi)容控件(ComboBox)、復(fù)選框內(nèi)容控件(CheckBox)、文本內(nèi)容控件(Text)、圖片內(nèi)容控件(Picture)、日期選取器內(nèi)容控件(DatePicker)、下拉列表內(nèi)容控件等(DropDownList)等。下面將通過(guò)代碼演示。

所需工具:Word類庫(kù)(Free Spire.Doc for Java)
獲取方法1:可通過(guò)官網(wǎng)下載jar包,并將lib文件夾下的Spire.Doc.jar文件導(dǎo)入Java程序;
獲取方法1:通過(guò)maven倉(cāng)庫(kù)安裝導(dǎo)入maven項(xiàng)目。

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){
        //創(chuàng)建Word文檔
        Document document = new Document();
        Section section = document.addSection();
        Paragraph paragraph = section.addParagraph();
        TextRange txtRange = paragraph.appendText("如何給Word文檔添加內(nèi)容控件:");
        txtRange.getCharacterFormat().setBold(true);
        txtRange.getCharacterFormat().setFontName("宋體");
        txtRange.getCharacterFormat().setTextColor(Color.RED);

        //添加組合框內(nèi)容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("組合框內(nèi)容控件:");
        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("中 國(guó)"));
        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);

        //添加復(fù)選框內(nèi)容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("復(fù)選框內(nèi)容控件:  ");
        txtRange.getCharacterFormat().setFontName("宋體");
        sd = new StructureDocumentTagInline(document);
        paragraph.getChildObjects().add(sd);
        sd.getSDTProperties().setSDTType(SdtType.Check_Box);
        sd.getSDTProperties().setAlias("復(fù)選框");
        sd.getSDTProperties().setTag("復(fù)選框");
        SdtCheckBox scb = new SdtCheckBox();
        sd.getSDTProperties().setControlProperties(scb);
        rt = new TextRange(document);
        sd.getChildObjects().add(rt);
        scb.setChecked(true);

        //添加文本內(nèi)容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("文本內(nèi)容控件: ");
        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);

        //添加圖片內(nèi)容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("圖片內(nèi)容控件:  ");
        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);

        //添加日期選取器內(nèi)容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("日期選取器內(nèi)容控件:  ");
        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);

        //添加下拉列表內(nèi)容控件
        paragraph = section.addParagraph();
        txtRange = paragraph.appendText("下拉列表內(nèi)容控件:");
        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("選項(xiàng)1"));
        sddl.getListItems().add(new SdtListItem("選項(xiàng)2"));
        sd.getSDTProperties().setControlProperties(sddl);
        rt = new TextRange(document);
        rt.setText(sddl.getListItems().get(0).getDisplayText());
        rt.getCharacterFormat().setFontName("宋體");
        sd.getSDTContent().getChildObjects().add(rt);

        //保存結(jié)果文檔
        document.saveToFile("addContentControls.docx", FileFormat.Docx_2013);
        document.dispose();
    }
}

內(nèi)容控件添加效果:

Java 添加Word內(nèi)容控件

(本文完)

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI