コンテンツコントロールSDTの操作

Microsoft Wordでは、テンプレートから始めて、checkboxes、テキストボックス、日付ピッカー、ドロップダウンリストなどのコンテンツコントロールを追加することで、フォームを作 Aspose.Wordsでは、Aspose.Wordsにロードされたドキュメントの構造化ドキュメントタグまたはコンテンツコントロールがStructuredDocumentTagノードとしてインポートされます。 構造化ドキュメントタグ(SDTまたはコンテンツコントロール)を使用すると、顧客定義のセマンティクスとその動作と外観を文書に埋め込むことがで

StructuredDocumentTagは、次の場所の文書で発生する可能性があります:

  • ブロックレベル-段落と表の間で、Body、HeaderFooter、Comment、Footnote、またはShapeノードの子として使用されます。
  • Row-level-テーブルノードの子としてのテーブル内の行の間。
  • Cell-level-テーブル行のセルのうち、行ノードの子として。
  • Inline-level-段落の子として、内部のインラインコンテンツの中で。
  • 別の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のように5つのスペースが挿入されます(繰り返しセクション、繰り返しセクション項目、グループ、チェ コンテンツコントロールがカスタム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プロパティを使用すると、コンテンツコントロールの色を取得または設定できます。 色は、次の2つの状況でコンテンツコントロールに影響します:

  1. MSWordは、マウスがコンテンツコントロールの上に移動すると、コンテンツコントロールの背景を強調表示します。 これは、コンテンツコントロールを識別するのに役立ちます。 強調表示の色はColorよりも少し「柔らかい」です。 たとえば、Colorが赤の場合、MSWordは背景をピンク色で強調表示します。
  2. コンテンツコントロールと対話(編集、ピッキングなど)すると、コンテンツコントロールの境界線は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プロパティを使用できます。 出力ドキュメントのコンテンツコントロールにテキストを入力すると、入力されたテキストのスタイルは"引用"になります。

次のコード例は、コンテンツコントロールのスタイルを設定する方法を示しています:

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