java word导入_Java 添加Word内容控件

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){

//创建Word文档 Document document = new Document();

Section section = document.addSection();

Paragraph paragraph = section.addParagraph();

TextRange txtRange = paragraph.appendText("如何给Word文档添加内容控件:");

txtRange.getCharacterFormat().setBold(true);

txtRange.getCharacterFormat().setFontName("宋体");

txtRange.getCharacterFormat().setTextColor(Color.RED);

//添加组合框内容控件 paragraph = section.addParagraph();

txtRange = paragraph.appendText("组合框内容控件:");

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("中 国"));

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);

//添加复选框内容控件 paragraph = section.addParagraph();

txtRange = paragraph.appendText("复选框内容控件: ");

txtRange.getCharacterFormat().setFontName("宋体");

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);

//添加文本内容控件 paragraph = section.addParagraph();

txtRange = paragraph.appendText("文本内容控件: ");

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);

//添加图片内容控件 paragraph = section.addParagraph();

txtRange = paragraph.appendText("图片内容控件: ");

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);

//添加日期选取器内容控件 paragraph = section.addParagraph();

txtRange = paragraph.appendText("日期选取器内容控件: ");

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);

//添加下拉列表内容控件 paragraph = section.addParagraph();

txtRange = paragraph.appendText("下拉列表内容控件:");

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("选项1"));

sddl.getListItems().add(new SdtListItem("选项2"));

sd.getSDTProperties().setControlProperties(sddl);

rt = new TextRange(document);

rt.setText(sddl.getListItems().get(0).getDisplayText());

rt.getCharacterFormat().setFontName("宋体");

sd.getSDTContent().getChildObjects().add(rt);

//保存结果文档 document.saveToFile("addContentControls.docx", FileFormat.Docx_2013);

document.dispose();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值