本文将介绍如何使用Spire.Doc for Java给Word文档添加组合框、复选框、文本、图片、日期选取器和下拉列表内容控件。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;
import java.util.Date;
public class ContentControls {
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("Spire.Doc"));
cb.getListItems().add(new SdtListItem("Spire.XLS"));
cb.getListItems().add(new SdtListItem("Spire.PDF"));
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("logo.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);
}
}
生成文档: