Java 在Word中添加内容控件
以下经验内容将介绍通过Java程序在Word文档中添加内容控件的方法,包括添加组合框内容控件、复选框内容控件、文本内容控件、图片内容控件、日期选取器内容控件以及下拉列表内容控件等。
工具/原料
Free Spire.Doc for Java (免费版)
IntelliJ IDEA
jar文件获取及导入:
1、方法1:通过eiceblue官网下载jar文件包。下载后,解压文件,并将lib文件夹下的Spire.Doc.jar文件导入java程序。参考如下导入效果:
2、方法2:可通过maven仓库导入。
Java代码示例
1、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 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文档添加内容控件:"); section.addParagraph(); //添加组合框内容控件 paragraph = section.addParagraph(); txtRange = paragraph.appendText("组合框内容控件:"); txtRange.getCharacterFormat().setItalic(true); 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()); sd.getSDTContent().getChildObjects().add(rt); section.addParagraph(); //添加复选框内容控件 paragraph = section.addParagraph(); txtRange = paragraph.appendText("复选框内容控件: "); txtRange.getCharacterFormat().setItalic(true); 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); section.addParagraph(); //添加文本内容控件 paragraph = section.addParagraph(); txtRange = paragraph.appendText("文本内容控件: "); txtRange.getCharacterFormat().setItalic(true); 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("文本"); sd.getSDTContent().getChildObjects().add(rt); section.addParagraph(); //添加图片内容控件 paragraph = section.addParagraph(); txtRange = paragraph.appendText("图片内容控件: "); txtRange.getCharacterFormat().setItalic(true); 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); section.addParagraph(); //添加日期选取器内容控件 paragraph = section.addParagraph(); txtRange = paragraph.appendText("日期选取器内容控件: "); txtRange.getCharacterFormat().setItalic(true); 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); section.addParagraph(); //添加下拉列表内容控件 paragraph = section.addParagraph(); txtRange = paragraph.appendText("下拉列表内容控件:"); txtRange.getCharacterFormat().setItalic(true); 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()); sd.getSDTContent().getChildObjects().add(rt); //保存结果文档 document.saveToFile("addContentControls1.docx", FileFormat.Docx_2013); document.dispose(); }}
2、内容控件添加效果: