Werk met Inhoud Beheer SDT
In Microsoft Word kan jy’n vorm skep deur met’n sjabloon te begin en inhoudskontroles by te voeg, insluitend checkboxes, tekskassies, datumplukkers en aftreklyste. In Aspose.Words word’n Gestruktureerde Dokumentetiket of inhoudbeheer van enige dokument wat in Aspose.Words gelaai is, ingevoer as’n StructuredDocumentTag - knooppunt. Gestruktureerde dokument etikette (SDT of inhoud beheer) toelaat inbed kliënt-gedefinieerde semantiek sowel as sy gedrag en voorkoms in’n dokument.
StructuredDocumentTag kan in’n dokument voorkom in die volgende plekke:
- Blokvlak-Onder paragrawe en tabelle, As’n Kind van’n Liggaam, HeaderFooter, Kommentaar, Voetnoot of’n Vormknooppunt.
- Ry-vlak-Tussen rye in’n tabel, as’n kind van’n Tabelknooppunt.
- Selvlak-Onder selle in’n tabelry, as’n kind van’n Ryknooppunt.
- Inline-vlak-Onder inline inhoud binne, as’n kind van’n Paragraaf.
- Ingesluit binne’n ander StructuredDocumentTag.
Invoeging Van Inhoudkontroles in’n Dokument
In hierdie weergawe van Aspose.Words, kan die volgende tipes van SDT of inhoud beheer geskep word:
- Checkbox
- DropDownList
- ComboBox
- Datum
- BuildingBlockGallery
- Groep
Picture
- RichText
- PlainText
Die volgende kode voorbeeld toon hoe om inhoud beheer van tipe checkboxte skep:
// 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"); |
Die volgende kode voorbeeld toon hoe om inhoud beheer van tipe ryk teks boks te skep:
// 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"); |
Die volgende kode voorbeeld toon hoe om inhoud beheer van tipe combo boks te skep:
// 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"); |
Hoe Om Inhoudskontroles Op Te Dateer
Hierdie afdeling verduidelik hoe om die waardes van SDT of inhoud beheer programmaties te werk.
Die volgende kode voorbeeld toon hoe om die huidige toestand van die checkboxstel:
// 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"); |
Die volgende kode voorbeeld toon hoe om inhoud kontroles van tipe gewone teks boks, drop-down lys en prentjie te verander:
// 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"); |
Bindende Inhoud Beheer Om Persoonlike XML Dele
Jy kan inhoudsbeheer bind met XML data (custom XML part) in Word-dokumente.
Die volgende kode voorbeeld toon hoe om inhoud beheer bind om persoonlike XML dele:
// 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); |
Maak die inhoud van’n Inhoudsbeheer skoon
U kan die inhoud van’n inhoudsbeheer skoonmaak deur’n plekhouer te vertoon. StructuredDocumentTag.clear() metode maak die inhoud van hierdie gestruktureerde dokument tag skoon en vertoon’n plekhouer as dit gedefinieer is. Dit is egter nie moontlik om die inhoud van’n inhoudskontrole skoon te maak as dit hersienings het nie. As’n inhoudskontrole geen plekhouer het nie, word vyf spasies ingevoeg soos in MS Word (behalwe herhalende afdelings, herhalende afdelingitems, groepe, vinkbokse, aanhalings). As’n inhoud beheer is gekarteer na persoonlike XML, die verwys XML node is skoongemaak.
Die volgende kode voorbeeld toon hoe om die inhoud van inhoud beheer skoon te maak:
// 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); |
Verander Inhoud Beheer Agtergrond En Grens Kleure
Die StructuredDocumentTag.Color
eienskap laat jou toe om die kleur van inhoud beheer te kry of stel. Die kleur beïnvloed inhoud beheer in twee situasies:
- MS Word beklemtoon die agtergrond van die inhoudbeheer wanneer die muis oor die inhoudbeheer beweeg. Dit help om die inhoud beheer te identifiseer. Die kleur van die verligting is’n bietjie" sagter " as die Color. Byvoorbeeld, MS Word beklemtoon die agtergrond met die pienk kleur, wanneer Color Rooi is.
- Wanneer jy interaksie (redigering, pluk ens) met die inhoud beheer, is die grens van inhoud beheer gekleur met die Color.
Die volgende kode voorbeeld toon hoe om die kleur van inhoud beheer te verander:
// 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); |
Hoe Om Styl Te Stel Om Teks Wat In Die Inhoudsbeheer Getik Is, Te Formateer
As jy die styl van inhoudbeheer wil instel, kan jy StructuredDocumentTag.Style
of StructuredDocumentTag.StyleName
eienskappe gebruik. As u die teks in die inhoudsbeheer in die uitvoerdokument tik, sal die getikte teks die styl “Kwotasie"hê.
Die volgende kode voorbeeld toon hoe om die styl van inhoud beheer stel:
// 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); |
Werk Met Herhalende Afdeling Inhoud Beheer
Die herhalende afdeling inhoud beheer laat herhaling van die inhoud wat daarin vervat is. Met behulp van Aspose.Words, kan die gestruktureerde dokument tag nodes van die herhalende afdeling en herhalende afdeling item tipes geskep word en vir hierdie doel, SdtType enumeration type bied REPEATING_SECTION_ITEM lid.
Die volgende kode voorbeeld toon hoe om’n herhalende gedeelte inhoud beheer te bind aan’n tabel:
// 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"); |