有時候,我們需要制作一個Word模板檔案,然后發給用戶填寫,但我們希望用戶只能在指定位置填寫內容,其他內容不允許編輯和修改,這時候我們就可以通過表單控制元件來輕松實作這一功能,本文將為您介紹如何通過Java代碼,以編程方式在Word中創建可填充表單,下面是我整理的步驟及方法,并附上Java代碼供大家參考,
程式環境:
方法1:手動引入,將 Free Spire.Doc for Java 下載到本地,解壓,找到lib檔案夾下的Spire.Doc.jar檔案,在IDEA中打開如下界面,將本地路徑中的jar檔案引入Java程式

方法2: 如果您想通過 Maven安裝,則可以在 pom.xml 檔案中添加以下代碼匯入 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中創建可填充表單
用戶打開下面的生成檔案,只能編輯表格中的表單,不能修改其他內容,詳細步驟如下:
- 創建Document物件,
- 使用 Document.addSection() 方法添加一個節,
- 使用 Section.addTable() 方法添加表格,
- 使用 TableCell.addParagraph() 方法將段落添加到特定的表格單元格,
- 創建 StructureDocumentTagInline 類的實體,并使用 Paragraph.getChildObjects().add() 方法將其作為子物件添加到段落中,
- 使用 StructureDocumentTagInline 物件的 SDTProperties 屬性和 SDTContent 屬性下的方法指定結構化檔案標記的屬性和內容,結構化檔案標簽的型別可通過 SDTProperties.setSDTType() 方法設定,
- 使用 Document.protect() 方法防止用戶編輯表單域之外的內容,
- 使用 Document.saveToFile() 方法保存檔案,
完整代碼
Java
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) { //創建檔案物件 Document doc = new Document(); //添加一個節 Section section = doc.addSection(); //添加一個表格 Table table = section.addTable(true); table.resetCells(7, 2); //將文本添加到第一列的單元格 Paragraph paragraph = table.getRows().get(0).getCells().get(0).addParagraph(); paragraph.appendText("純文本內容控制元件"); paragraph = table.getRows().get(1).getCells().get(0).addParagraph(); paragraph.appendText("富文本內容控制元件"); paragraph = table.getRows().get(2).getCells().get(0).addParagraph(); paragraph.appendText("圖片內容控制元件"); paragraph = table.getRows().get(3).getCells().get(0).addParagraph(); paragraph.appendText("下拉串列內容控制元件"); paragraph = table.getRows().get(4).getCells().get(0).addParagraph(); paragraph.appendText("復選框內容控制元件"); paragraph = table.getRows().get(5).getCells().get(0).addParagraph(); paragraph.appendText("組合框內容控制元件"); paragraph = table.getRows().get(6).getCells().get(0).addParagraph(); paragraph.appendText("日期選擇器內容控制元件"); //向單元格添加純文本內容控制元件 (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("單擊或點擊此處輸入文本,"); sdt.getSDTContent().getChildObjects().add(tr); //向單元格添加富文本內容控制元件 (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("單擊或點擊此處輸入文本,"); sdt.getSDTContent().getChildObjects().add(tr); //向單元格添加圖片內容控制元件 (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); //向單元格添加下拉串列內容控制元件(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("選擇一個專案,", "1")); sddl.getListItems().add(new SdtListItem("專案2", "2")); sddl.getListItems().add(new SdtListItem("專案3", "3")); sddl.getListItems().add(new SdtListItem("專案4", "4")); sdt.getSDTProperties().setControlProperties(sddl); tr = new TextRange(doc); tr.setText(sddl.getListItems().get(0).getDisplayText()); sdt.getSDTContent().getChildObjects().add(tr); //向單元格添加兩個復選框內容控制元件 (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(" 選項 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(" 選項 2"); //將組合框內容控制元件添加到單元格 (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("選擇一個專案.")); cb.getListItems().add(new SdtListItem("專案 2")); cb.getListItems().add(new SdtListItem("專案 3")); sdt.getSDTProperties().setControlProperties(cb); tr = new TextRange(doc); tr.setText(cb.getListItems().get(0).getDisplayText()); sdt.getSDTContent().getChildObjects().add(tr); //將日期選擇器內容控制元件添加到單元格(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"); //保存結果檔案 doc.saveToFile("WordForm.docx", FileFormat.Docx_2013); } }
效果圖

—本文完—
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/547608.html
標籤:Java
