使用内容控制SDT
在Microsoft Word中,您可以通过从模板开始并添加内容控件(包括checkboxes、文本框、日期选择器和下拉列表)来创建表单。 在Aspose.Words中,来自加载到Aspose.Words中的任何文档的结构化文档标记或内容控件作为StructuredDocumentTag节点导入。 结构化文档标签(SDT或内容控件)允许将客户定义的语义及其行为和外观嵌入到文档中。
StructuredDocumentTag可以在以下位置出现在文档中:
- 块级-在段落和表格中,作为正文、HeaderFooter、注释、脚注或形状节点的子级。
- 行级别-表中的行之间,作为表节点的子节点。
- 单元格级别-在表行中的单元格之间,作为行节点的子节点。
- 内联级别-内联内容之间,作为段落的子级。
- 嵌套在另一个StructuredDocumentTag内。
将内容控件插入到文档中
在此版本的Aspose.Words中,可以创建以下类型的SDT或内容控件:
- Checkbox
- DropDownList
- ComboBox
- 日期
- BuildingBlockGallery
- 团体
Picture
- RichText
- PlainText
下面的代码示例演示如何创建类型为checkbox的内容控件:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CheckBoxTypeContentControl.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
StructuredDocumentTag stdCheckBox = new StructuredDocumentTag(doc, SdtType.CHECKBOX, MarkupLevel.INLINE); | |
builder.insertNode(stdCheckBox); | |
doc.save(dataDir + "output.doc"); |
下面的代码示例演示如何创建富文本框类型的内容控件:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(RichTextBoxContentControl.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.BLOCK); | |
Paragraph para = new Paragraph(doc); | |
Run run = new Run(doc); | |
run.setText("Hello World"); | |
run.getFont().setColor(Color.MAGENTA); | |
para.getRuns().add(run); | |
sdtRichText.getChildNodes().add(para); | |
doc.getFirstSection().getBody().appendChild(sdtRichText); | |
doc.save(dataDir + "output.doc"); |
下面的代码示例演示如何创建组合框类型的内容控件:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ComboBoxContentControl.class); | |
// Open the document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.COMBO_BOX, MarkupLevel.BLOCK); | |
sdt.getListItems().add(new SdtListItem("Choose an item", "3")); | |
sdt.getListItems().add(new SdtListItem("Item 1", "1")); | |
sdt.getListItems().add(new SdtListItem("Item 2", "2")); | |
doc.getFirstSection().getBody().appendChild(sdt); | |
doc.save(dataDir + "output.doc"); |
如何更新内容控件
本节介绍如何以编程方式更新SDT或内容控件的值。
下面的代码示例演示如何设置checkbox的当前状态:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(SetCurrentStateOfCheckBox.class); | |
// Open the document. | |
Document doc = new Document(dataDir + "CheckBoxTypeContentControl.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
StructuredDocumentTag SdtCheckBox = (StructuredDocumentTag) doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true); | |
//StructuredDocumentTag.Checked property gets/sets current state of the Checkbox SDT | |
if (SdtCheckBox.getSdtType() == SdtType.CHECKBOX) | |
SdtCheckBox.setChecked(true); | |
doc.save(dataDir + "output.doc"); |
下面的代码示例演示如何修改纯文本框、下拉列表和图片类型的内容控件:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ModifyContentControls.class); | |
// Open the document. | |
Document doc = new Document(dataDir + "CheckBoxTypeContentControl.docx"); | |
for (Object t : doc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) { | |
StructuredDocumentTag std = (StructuredDocumentTag) t; | |
if (std.getSdtType() == SdtType.PLAIN_TEXT) { | |
std.removeAllChildren(); | |
Paragraph para = (Paragraph) std.appendChild(new Paragraph(doc)); | |
Run run = new Run(doc, "new text goes here"); | |
para.appendChild(run); | |
} | |
if (std.getSdtType() == SdtType.DROP_DOWN_LIST) { | |
SdtListItem secondItem = std.getListItems().get(2); | |
std.getListItems().setSelectedValue(secondItem); | |
} | |
if (std.getSdtType() == SdtType.PICTURE) { | |
Shape shape = (Shape) std.getChild(NodeType.SHAPE, 0, true); | |
if (shape.hasImage()) { | |
shape.getImageData().setImage(dataDir + "Watermark.png"); | |
} | |
} | |
doc.save(dataDir + "output.doc"); |
将内容控件绑定到自定义XML部件
您可以将内容控件与Word文档中的XML数据(custom XML part)绑定。
下面的代码示例演示如何将内容控件绑定到自定义XML部件:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(BindingContentControlwithXML.class); | |
Document doc = new Document(); | |
CustomXmlPart xmlPart = doc.getCustomXmlParts().add(UUID.fromString("38400000-8cf0-11bd-b23e-10b96e4ef00d").toString(), "<root><text>Hello, World!</text></root>"); | |
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK); | |
doc.getFirstSection().getBody().appendChild(sdt); | |
sdt.getXmlMapping().setMapping(xmlPart, "/root[1]/text[1]", ""); | |
dataDir = dataDir + "BindSDTtoCustomXmlPart_out.doc"; | |
// Save the document to disk. | |
doc.save(dataDir); |
清除内容控件的内容
您可以通过显示占位符来清除内容控件的内容。 **StructuredDocumentTag.clear()**方法清除此结构化文档标记的内容,并在定义占位符时显示占位符。 但是,如果内容控件具有修订,则无法清除其内容控件的内容。 如果内容控件没有占位符,则会像MSWord中那样插入五个空格(重复部分、重复部分项、组、复选框、引用除外)。 如果内容控件映射到自定义XML,则引用的XML节点将被清除。
下面的代码示例演示如何清除内容控件的内容:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ClearContentsControl.class); | |
Document doc = new Document(dataDir + "input.docx"); | |
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true); | |
sdt.clear(); | |
dataDir = dataDir + "ClearContentsControl_out.doc"; | |
// Save the document to disk. | |
doc.save(dataDir); |
更改内容控制背景和边框颜色
StructuredDocumentTag.Color
属性允许您获取或设置内容控件的颜色。 颜色在两种情况下影响内容控制:
- MSWord当鼠标移到内容控件上时,突出显示内容控件的背景。 这有助于识别内容控制。 突出显示的颜色比Color有点"柔和"。 例如,当Color为红色时,MSWord以粉红色突出显示背景。
- 当您与内容控件交互(编辑,选择等)时,内容控件的边框用Color着色。
下面的代码示例演示如何更改内容控件的颜色:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// The path to the documents directory. | |
Document doc = new Document(dataDir + "input.docx"); | |
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true); | |
sdt.setColor(Color.RED); | |
dataDir = dataDir + "SetContentControlColor_out.docx"; | |
// Save the document to disk. | |
doc.save(dataDir); |
如何设置样式以格式化输入到内容控件中的文本
如果要设置内容控件的样式,可以使用StructuredDocumentTag.Style
或StructuredDocumentTag.StyleName
属性。 当您将文本键入输出文档中的内容控件时,键入的文本将具有样式"Quote"。
下面的代码示例演示如何设置内容控件的样式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(dataDir + "input.docx"); | |
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true); | |
Style style = doc.getStyles().getByStyleIdentifier(StyleIdentifier.QUOTE); | |
sdt.setStyle(style); | |
dataDir = dataDir + "SetContentControlStyle_out.docx"; | |
doc.save(dataDir); |
使用重复部分内容控制
重复部分内容控件允许重复其中包含的内容。 使用Aspose.Words,可以创建重复部分和重复部分项类型的结构化文档标记节点,为此,SdtType枚举类型提供REPEATING_SECTION_ITEM成员。
下面的代码示例演示如何将重复部分内容控件绑定到表:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
CustomXmlPart xmlPart = doc.getCustomXmlParts().add("Books", | |
"<books><book><title>Everyday Italian</title><author>Giada De Laurentiis</author></book>" + | |
"<book><title>Harry Potter</title><author>J K. Rowling</author></book>" + | |
"<book><title>Learning XML</title><author>Erik T. Ray</author></book></books>"); | |
Table table = builder.startTable(); | |
builder.insertCell(); | |
builder.write("Title"); | |
builder.insertCell(); | |
builder.write("Author"); | |
builder.endRow(); | |
builder.endTable(); | |
StructuredDocumentTag repeatingSectionSdt = | |
new StructuredDocumentTag(doc, SdtType.REPEATING_SECTION, MarkupLevel.ROW); | |
repeatingSectionSdt.getXmlMapping().setMapping(xmlPart, "/books[1]/book", ""); | |
table.appendChild(repeatingSectionSdt); | |
StructuredDocumentTag repeatingSectionItemSdt = | |
new StructuredDocumentTag(doc, SdtType.REPEATING_SECTION_ITEM, MarkupLevel.ROW); | |
repeatingSectionSdt.appendChild(repeatingSectionItemSdt); | |
Row row = new Row(doc); | |
repeatingSectionItemSdt.appendChild(row); | |
StructuredDocumentTag titleSdt = | |
new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.CELL); | |
titleSdt.getXmlMapping().setMapping(xmlPart, "/books[1]/book[1]/title[1]", ""); | |
row.appendChild(titleSdt); | |
StructuredDocumentTag authorSdt = | |
new StructuredDocumentTag(doc, SdtType.PLAIN_TEXT, MarkupLevel.CELL); | |
authorSdt.getXmlMapping().setMapping(xmlPart, "/books[1]/book[1]/author[1]", ""); | |
row.appendChild(authorSdt); | |
doc.save(dataDir + "Document.docx"); |