最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過Java實(shí)現(xiàn)在Word中創(chuàng)建可填充表單

 更新時(shí)間:2023年03月21日 15:18:15   作者:Carina-baby  
這篇文章主要為大家詳細(xì)介紹了如何通過Java代碼,以編程方式在Word中創(chuàng)建可填充表單,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下

有時(shí)候,我們需要制作一個(gè)Word模板文檔,然后發(fā)給用戶填寫,但我們希望用戶只能在指定位置填寫內(nèi)容,其他內(nèi)容不允許編輯和修改。這時(shí)候我們就可以通過表單控件來輕松實(shí)現(xiàn)這一功能。本文將為您介紹如何通過Java代碼,以編程方式在Word中創(chuàng)建可填充表單。下面是我整理的步驟及方法,并附上Java代碼供大家參考。

程序環(huán)境

方法1:手動(dòng)引入。將 Free Spire.Doc for Java 下載到本地,解壓,找到lib文件夾下的Spire.Doc.jar文件。在IDEA中打開如下界面,將本地路徑中的jar文件引入Java程序

方法2: 如果您想通過 Maven安裝,則可以在 pom.xml 文件中添加以下代碼導(dǎo)入 JAR 文件。

<repositories>

        <repository>

            <id>com.e-iceblue</id>

            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>

        </repository>

    </repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.doc.free</artifactId>

        <version>5.2.0</version>

    </dependency>

</dependencies>

在Word中創(chuàng)建可填充表單

用戶打開下面的生成文檔,只能編輯表格中的窗體,不能修改其他內(nèi)容。詳細(xì)步驟如下:

  • 創(chuàng)建Document對(duì)象。
  • 使用 Document.addSection() 方法添加一個(gè)節(jié)。
  • 使用 Section.addTable() 方法添加表格。
  • 使用 TableCell.addParagraph() 方法將段落添加到特定的表格單元格。
  • 創(chuàng)建 StructureDocumentTagInline 類的實(shí)例,并使用 Paragraph.getChildObjects().add() 方法將其作為子對(duì)象添加到段落中。
  • 使用 StructureDocumentTagInline 對(duì)象的 SDTProperties 屬性和 SDTContent 屬性下的方法指定結(jié)構(gòu)化文檔標(biāo)記的屬性和內(nèi)容。結(jié)構(gòu)化文檔標(biāo)簽的類型可通過 SDTProperties.setSDTType() 方法設(shè)置。
  • 使用 Document.protect() 方法防止用戶編輯表單域之外的內(nèi)容。
  • 使用 Document.saveToFile() 方法保存文檔。

完整代碼

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

import java.util.Date;

public class CreateFillableForm {

    public static void main(String[] args) {

        //創(chuàng)建文檔對(duì)象
        Document doc = new Document();

        //添加一個(gè)節(jié)
        Section section = doc.addSection();

        //添加一個(gè)表格
        Table table = section.addTable(true);
        table.resetCells(7, 2);

        //將文本添加到第一列的單元格
        Paragraph paragraph = table.getRows().get(0).getCells().get(0).addParagraph();
        paragraph.appendText("純文本內(nèi)容控件");
        paragraph = table.getRows().get(1).getCells().get(0).addParagraph();
        paragraph.appendText("富文本內(nèi)容控件");
        paragraph = table.getRows().get(2).getCells().get(0).addParagraph();
        paragraph.appendText("圖片內(nèi)容控件");
        paragraph = table.getRows().get(3).getCells().get(0).addParagraph();
        paragraph.appendText("下拉列表內(nèi)容控件");
        paragraph = table.getRows().get(4).getCells().get(0).addParagraph();
        paragraph.appendText("復(fù)選框內(nèi)容控件");
        paragraph = table.getRows().get(5).getCells().get(0).addParagraph();
        paragraph.appendText("組合框內(nèi)容控件");
        paragraph = table.getRows().get(6).getCells().get(0).addParagraph();
        paragraph.appendText("日期選擇器內(nèi)容控件");

        //向單元格添加純文本內(nèi)容控件 (0,1)
        paragraph = table.getRows().get(0).getCells().get(1).addParagraph();
        StructureDocumentTagInline sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Text);
        sdt.getSDTProperties().setAlias("純文本");
        sdt.getSDTProperties().setTag("純文本");
        sdt.getSDTProperties().isShowingPlaceHolder(true);
        SdtText text = new SdtText(true);
        text.isMultiline(false);
        sdt.getSDTProperties().setControlProperties(text);
        TextRange tr = new TextRange(doc);
        tr.setText("單擊或點(diǎn)擊此處輸入文本。");
        sdt.getSDTContent().getChildObjects().add(tr);

        //向單元格添加富文本內(nèi)容控件 (1,1)
        paragraph = table.getRows().get(1).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Rich_Text);
        sdt.getSDTProperties().setAlias("富文本");
        sdt.getSDTProperties().setTag("富文本");
        sdt.getSDTProperties().isShowingPlaceHolder(true);
        text = new SdtText(true);
        text.isMultiline(false);
        sdt.getSDTProperties().setControlProperties(text);
        tr = new TextRange(doc);
        tr.setText("單擊或點(diǎn)擊此處輸入文本。");
        sdt.getSDTContent().getChildObjects().add(tr);

        //向單元格添加圖片內(nèi)容控件 (2,1)
        paragraph = table.getRows().get(2).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Picture);
        sdt.getSDTProperties().setAlias("圖片");
        sdt.getSDTProperties().setTag("圖片");
        SdtPicture sdtPicture = new SdtPicture();
        sdt.getSDTProperties().setControlProperties(sdtPicture);
        DocPicture pic = new DocPicture(doc);
        pic.loadImage("圖片2.jpg");
        sdt.getSDTContent().getChildObjects().add(pic);

        //向單元格添加下拉列表內(nèi)容控件(3,1)
        paragraph = table.getRows().get(3).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        sdt.getSDTProperties().setSDTType(SdtType.Drop_Down_List);
        sdt.getSDTProperties().setAlias("下拉列表");
        sdt.getSDTProperties().setTag("下拉列表");
        paragraph.getChildObjects().add(sdt);
        SdtDropDownList sddl = new SdtDropDownList();
        sddl.getListItems().add(new SdtListItem("選擇一個(gè)項(xiàng)目。", "1"));
        sddl.getListItems().add(new SdtListItem("項(xiàng)目2", "2"));
        sddl.getListItems().add(new SdtListItem("項(xiàng)目3", "3"));
        sddl.getListItems().add(new SdtListItem("項(xiàng)目4", "4"));
        sdt.getSDTProperties().setControlProperties(sddl);
        tr = new TextRange(doc);
        tr.setText(sddl.getListItems().get(0).getDisplayText());
        sdt.getSDTContent().getChildObjects().add(tr);

        //向單元格添加兩個(gè)復(fù)選框內(nèi)容控件 (4,1)
        paragraph = table.getRows().get(4).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Check_Box);
        SdtCheckBox scb = new SdtCheckBox();
        sdt.getSDTProperties().setControlProperties(scb);
        tr = new TextRange(doc);
        sdt.getChildObjects().add(tr);
        scb.setChecked(false);
        paragraph.appendText(" 選項(xiàng) 1");

        paragraph = table.getRows().get(4).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Check_Box);
        scb = new SdtCheckBox();
        sdt.getSDTProperties().setControlProperties(scb);
        tr = new TextRange(doc);
        sdt.getChildObjects().add(tr);
        scb.setChecked(false);
        paragraph.appendText(" 選項(xiàng) 2");

        //將組合框內(nèi)容控件添加到單元格 (5,1)
        paragraph = table.getRows().get(5).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Combo_Box);
        sdt.getSDTProperties().setAlias("組合框");
        sdt.getSDTProperties().setTag("組合框");
        SdtComboBox cb = new SdtComboBox();
        cb.getListItems().add(new SdtListItem("選擇一個(gè)項(xiàng)目."));
        cb.getListItems().add(new SdtListItem("項(xiàng)目 2"));
        cb.getListItems().add(new SdtListItem("項(xiàng)目 3"));
        sdt.getSDTProperties().setControlProperties(cb);
        tr = new TextRange(doc);
        tr.setText(cb.getListItems().get(0).getDisplayText());
        sdt.getSDTContent().getChildObjects().add(tr);

        //將日期選擇器內(nèi)容控件添加到單元格(6,1)
        paragraph = table.getRows().get(6).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Date_Picker);
        sdt.getSDTProperties().setAlias("日期選擇器");
        sdt.getSDTProperties().setTag("日期選擇器");
        SdtDate date = new SdtDate();
        date.setCalendarType(CalendarType.Default);
        date.setDateFormat("yyyy.MM.dd");
        date.setFullDate(new Date());
        sdt.getSDTProperties().setControlProperties(date);
        tr = new TextRange(doc);
        tr.setText("單擊或輕按以輸入日期。");
        sdt.getSDTContent().getChildObjects().add(tr);

        //僅允許用戶編輯表單域
        doc.protect(ProtectionType.Allow_Only_Form_Fields, "permission-psd");

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

效果圖

到此這篇關(guān)于通過Java實(shí)現(xiàn)在Word中創(chuàng)建可填充表單的文章就介紹到這了,更多相關(guān)Java Word創(chuàng)建可填充表單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • minio的原理、部署、操作方法(十分鐘精通MinIO)

    minio的原理、部署、操作方法(十分鐘精通MinIO)

    MinIO是一個(gè)簡(jiǎn)單易用的云存儲(chǔ)服務(wù),可以將文件上傳到互聯(lián)網(wǎng)上,方便在不同地方訪問,部署MinIO需要拉取鏡像、啟動(dòng)端口、設(shè)置賬號(hào)密碼和映射文件地址,在Spring?Boot中集成MinIO,需要添加依賴、創(chuàng)建客戶端并進(jìn)行單元測(cè)試,感興趣的朋友一起看看吧
    2025-02-02
  • SpringBoot校園綜合管理系統(tǒng)實(shí)現(xiàn)流程分步講解

    SpringBoot校園綜合管理系統(tǒng)實(shí)現(xiàn)流程分步講解

    這篇文章主要介紹了SpringBoot+Vue實(shí)現(xiàn)校園綜合管理系統(tǒng)流程分步講解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-09-09
  • Filter在springboot中的使用方法詳解

    Filter在springboot中的使用方法詳解

    這篇文章主要介紹了Filter在springboot中的使用方法詳解,filter(過濾器)作用于在intreceptor(攔截器)之前,不像intreceptor一樣依賴于springmvc框架,只需要依賴于serverlet,需要的朋友可以參考下
    2023-08-08
  • 基于SpringIOC創(chuàng)建對(duì)象的四種方式總結(jié)

    基于SpringIOC創(chuàng)建對(duì)象的四種方式總結(jié)

    這篇文章主要介紹了基于SpringIOC創(chuàng)建對(duì)象的四種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Springboot初始化項(xiàng)目并完成登入注冊(cè)的全過程

    Springboot初始化項(xiàng)目并完成登入注冊(cè)的全過程

    工作之余,想要學(xué)習(xí)一下SpringBoot,通過網(wǎng)絡(luò)大量教程最終成功運(yùn)行SpringBoot項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于Springboot初始化項(xiàng)目并完成登入注冊(cè)的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Java transient關(guān)鍵字使用小記

    Java transient關(guān)鍵字使用小記

    這篇文章主要為大家詳細(xì)介紹了Java transient關(guān)鍵字的使用方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Java日期格式化如何避免YYYY引發(fā)的時(shí)間異常

    Java日期格式化如何避免YYYY引發(fā)的時(shí)間異常

    在編程中,日期格式化是一個(gè)常見的任務(wù),使用不同的格式化選項(xiàng)可能會(huì)導(dǎo)致一些意外的結(jié)果,下面我們就來學(xué)習(xí)一下Java如何避免YYYY引發(fā)的時(shí)間異常吧
    2023-11-11
  • Java將本地項(xiàng)目部署到Linux服務(wù)器的實(shí)踐

    Java將本地項(xiàng)目部署到Linux服務(wù)器的實(shí)踐

    本文主要介紹了Java將本地項(xiàng)目部署到Linux服務(wù)器的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧<BR>
    2022-06-06
  • Sharding-Proxy基本功能用法介紹

    Sharding-Proxy基本功能用法介紹

    這篇文章介紹了Sharding-Proxy基本功能用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • java調(diào)用shell命令并獲取執(zhí)行結(jié)果的示例

    java調(diào)用shell命令并獲取執(zhí)行結(jié)果的示例

    今天小編就為大家分享一篇java調(diào)用shell命令并獲取執(zhí)行結(jié)果的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07

最新評(píng)論

疏附县| 金阳县| 会昌县| 屯昌县| 巍山| 云阳县| 财经| 章丘市| 西贡区| 招远市| 钟山县| 阿坝| 东光县| 怀远县| 文水县| 富锦市| 舒城县| 曲周县| 大同县| 康马县| 屏南县| 健康| 孝感市| 赣州市| 吴川市| 治多县| 西丰县| 喜德县| 武邑县| 汝阳县| 嘉义县| 上犹县| 定襄县| 社旗县| 维西| 顺平县| 永吉县| 邵武市| 迁安市| 灵武市| 盘锦市|