Arbeiten mit Inhaltskontrolle SDT
In Microsoft Word können Sie ein Formular erstellen, indem Sie mit einer Vorlage beginnen und Inhaltssteuerelemente hinzufügen, einschließlich checkboxes, Textfelder, Datumsauswahl und Dropdown-Listen. In Aspose.Words wird ein strukturiertes Dokument-Tag oder ein Inhaltssteuerelement aus einem beliebigen in Aspose.Words geladenen Dokument als StructuredDocumentTag-Knoten importiert. Strukturierte Dokument-Tags (SDT oder Inhaltskontrolle) ermöglichen die Einbettung von kundendefinierter Semantik sowie deren Verhalten und Erscheinungsbild in ein Dokument.
StructuredDocumentTag kann in einem Dokument an folgenden Stellen vorkommen:
- Blockebene - Zwischen Absätzen und Tabellen, als Kind eines Körpers, HeaderFooter, Kommentars, einer Fußnote oder eines Formknotens.
- Zeilenebene - Unter Zeilen in einer Tabelle als untergeordnetes Element eines Tabellenknotens.
- Zellebene - Zwischen Zellen in einer Tabellenzeile als untergeordnetes Element eines Zeilenknotens.
- Inline-Ebene - Unter Inline-Inhalten im Inneren als untergeordnetes Element eines Absatzes.
- In einem anderen StructuredDocumentTag verschachtelt.
Einfügen von Inhaltssteuerelementen in ein Dokument
In dieser Version von Aspose.Words können die folgenden Arten von SDT- oder Inhaltssteuerelementen erstellt werden:
- Checkbox
- DropDownList
- ComboBox
- Datum
- BuildingBlockGallery
- Gruppe
Picture
- RichText
- PlainText
Das folgende Codebeispiel zeigt, wie Sie ein Inhaltssteuerelement vom Typ checkbox erstellen:
// 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"); |
Das folgende Codebeispiel zeigt, wie Sie ein Inhaltssteuerelement vom Typ Rich-Text-Feld erstellen:
// 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"); |
Das folgende Codebeispiel zeigt, wie Sie ein Inhaltssteuerelement vom Typ Kombinationsfeld erstellen:
// 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"); |
So aktualisieren Sie Inhaltssteuerelemente
In diesem Abschnitt wird erläutert, wie Sie die Werte von SDT oder Content Control programmgesteuert aktualisieren.
Das folgende Codebeispiel zeigt, wie der aktuelle Status von checkbox festgelegt wird:
// 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"); |
Das folgende Codebeispiel zeigt, wie Inhaltssteuerelemente vom Typ Nur-Text-Feld, Dropdown-Liste und Bild geändert werden:
// 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"); |
Binden der Inhaltssteuerung an benutzerdefinierte XML -Teile
Sie können Inhaltssteuerelemente mit XML-Daten (custom XML part) in Word-Dokumenten binden.
Das folgende Codebeispiel zeigt, wie Inhaltssteuerelemente an benutzerdefinierte XML -Teile gebunden werden:
// 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); |
Inhalte eines Inhaltssteuerelements löschen
Sie können den Inhalt eines Inhaltssteuerelements löschen, indem Sie einen Platzhalter anzeigen. Die StructuredDocumentTag.clear() -Methode löscht den Inhalt dieses strukturierten Dokument-Tags und zeigt einen Platzhalter an, wenn er definiert ist. Es ist jedoch nicht möglich, den Inhalt eines Inhaltssteuerelements zu löschen, wenn es Überarbeitungen enthält. Wenn ein Inhaltssteuerelement keinen Platzhalter enthält, werden fünf Leerzeichen wie in MS Word eingefügt (außer wiederholten Abschnitten, wiederholten Abschnittselementen, Gruppen, Kontrollkästchen, Zitaten). Wenn ein Inhaltssteuerelement benutzerdefiniert XML zugeordnet ist, wird der referenzierte XML-Knoten gelöscht.
Das folgende Codebeispiel zeigt, wie der Inhalt der Inhaltskontrolle gelöscht wird:
// 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); |
Hintergrund- und Rahmenfarben der Inhaltssteuerung ändern
Mit der Eigenschaft StructuredDocumentTag.Color
können Sie die Farbe des Inhaltssteuerelements abrufen oder festlegen. Die Farbe wirkt sich in zwei Situationen auf die Inhaltskontrolle aus:
- MS Word hebt den Hintergrund des Inhaltssteuerelements hervor, wenn sich die Maus über das Inhaltssteuerelement bewegt. Dies hilft, die Inhaltskontrolle zu identifizieren. Die Farbe der Hervorhebung ist etwas “weicher” als die Color. Zum Beispiel hebt MS Wort den Hintergrund mit der rosa Farbe hervor, wenn Color Rot ist.
- Wenn Sie mit dem Inhaltssteuerelement interagieren (Bearbeiten, Auswählen usw.), wird der Rand des Inhaltssteuerelements mit Color eingefärbt.
Das folgende Codebeispiel zeigt, wie Sie die Farbe des Inhaltssteuerelements ändern:
// 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); |
So legen Sie den Stil fest, um Text zu formatieren, der in das Inhaltssteuerelement eingegeben wurde
Wenn Sie den Stil der Inhaltssteuerung festlegen möchten, können Sie die Eigenschaften StructuredDocumentTag.Style
oder StructuredDocumentTag.StyleName
verwenden. Wenn Sie den Text in das Inhaltssteuerelement im Ausgabedokument eingeben, hat der eingegebene Text den Stil “Zitat”.
Das folgende Codebeispiel zeigt, wie Sie den Stil der Inhaltssteuerung festlegen:
// 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); |
Arbeiten mit der Inhaltssteuerung für wiederholte Abschnitte
Die Inhaltssteuerung für wiederholte Abschnitte ermöglicht das Wiederholen des darin enthaltenen Inhalts. Mit Aspose.Words können die strukturierten Dokument-Tag-Knoten des wiederholten Abschnitts und der wiederholten Abschnittselementtypen erstellt werden, und zu diesem Zweck stellt SdtType Aufzählungstyp REPEATING_SECTION_ITEM Mitglied bereit.
Das folgende Codebeispiel zeigt, wie Sie ein Inhaltssteuerelement für wiederholte Abschnitte an eine Tabelle binden:
// 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"); |