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
  • Geneste 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-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
// Open the empty document
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag SdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
// Insert content control into the document
builder.InsertNode(SdtCheckBox);
dataDir = dataDir + "CheckBoxTypeContentControl_out.docx";
doc.Save(dataDir, SaveFormat.Docx);

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-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
Document doc = new Document();
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
Paragraph para = new Paragraph(doc);
Run run = new Run(doc);
run.Text = "Hello World";
run.Font.Color = Color.Green;
para.Runs.Add(run);
sdtRichText.ChildNodes.Add(para);
doc.FirstSection.Body.AppendChild(sdtRichText);
dataDir = dataDir + "RichTextBoxContentControl_out.docx";
doc.Save(dataDir);

Die volgende kode voorbeeld toon hoe om inhoud beheer van die tipe combo boks te skep:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithDocument();
Document doc = new Document();
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.ComboBox, MarkupLevel.Block);
sdt.ListItems.Add(new SdtListItem("Choose an item", "-1"));
sdt.ListItems.Add(new SdtListItem("Item 1", "1"));
sdt.ListItems.Add(new SdtListItem("Item 2", "2"));
doc.FirstSection.Body.AppendChild(sdt);
dataDir = dataDir + "ComboBoxContentControl_out.docx";
doc.Save(dataDir);

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-.NET
// Open an existing document
Document doc = new Document(dataDir + "CheckBoxTypeContentControl.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get the first content control from the document
StructuredDocumentTag SdtCheckBox = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
// StructuredDocumentTag.Checked property gets/sets current state of the Checkbox SDT
if (SdtCheckBox.SdtType == SdtType.Checkbox)
SdtCheckBox.Checked = true;
dataDir = dataDir + "SetCurrentStateOfCheckBox_out.docx";
doc.Save(dataDir);

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-.NET
// Open an existing document
Document doc = new Document(dataDir + "CheckBoxTypeContentControl.docx");
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
if (sdt.SdtType == SdtType.PlainText)
{
sdt.RemoveAllChildren();
Paragraph para = sdt.AppendChild(new Paragraph(doc)) as Paragraph;
Run run = new Run(doc, "new text goes here");
para.AppendChild(run);
}
else if (sdt.SdtType == SdtType.DropDownList)
{
SdtListItem secondItem = sdt.ListItems[2];
sdt.ListItems.SelectedValue = secondItem;
}
else if (sdt.SdtType == SdtType.Picture)
{
Shape shape = (Shape)sdt.GetChild(NodeType.Shape, 0, true);
if (shape.HasImage)
{
shape.ImageData.SetImage(dataDir + "Watermark.png");
}
}
}
dataDir = dataDir + "ModifyContentControls_out.docx";
doc.Save(dataDir);

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-.NET
Document doc = new Document();
CustomXmlPart xmlPart = doc.CustomXmlParts.Add(Guid.NewGuid().ToString("B"), "<root><text>Hello, World!</text></root>");
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block);
doc.FirstSection.Body.AppendChild(sdt);
sdt.XmlMapping.SetMapping(xmlPart, "/root[1]/text[1]", "");
dataDir = dataDir + "BindSDTtoCustomXmlPart_out.doc";
// Save the document to disk.
doc.Save(dataDir);

XMLMapping Van Gestruktureerde Dokument Tag Reeks

Jy kan die kartering van hierdie gestruktureerde dokument tag reeks te XML data in’n persoonlike XML deel van die huidige dokument met behulp van die StructuredDocumentTagRangeStart.XmlMapping property. Die SetMapping metode kan egter gebruik word om’n gestruktureerde dokument tag reeks te karteer om XML data.

Die volgende kode voorbeeld toon hoe om XML kartering stel:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "input.docx");
StructuredDocumentTagRangeStart sdtRangeStart = (StructuredDocumentTagRangeStart)doc.GetChild(NodeType.StructuredDocumentTagRangeStart, 0, true);
sdtRangeStart.XmlMapping.SetMapping(doc.CustomXmlParts[0], "/Root/Element", null);
doc.Save(dataDir + "output.docx");

Maak die inhoud van’n Inhoudsbeheer skoon

U kan die inhoud van’n inhoudsbeheer skoonmaak deur’n plekhouer te vertoon. Die 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 Microsoft Word (behalwe herhalende afdelings, herhalende afdelings, 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-.NET
Document doc = new Document(dataDir + "input.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 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:

  1. 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.
  2. 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-.NET
Document doc = new Document(dataDir + "input.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
sdt.Color = 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-.NET
Document doc = new Document(dataDir + "input.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
Style style = doc.Styles[StyleIdentifier.Quote];
sdt.Style = style;
dataDir = dataDir + "SetContentControlStyle_out.docx";
// Save the document to disk.
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 RepeatingSectionItem eiendom.

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-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
CustomXmlPart xmlPart = doc.CustomXmlParts.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.RepeatingSection, MarkupLevel.Row);
repeatingSectionSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book", "");
table.AppendChild(repeatingSectionSdt);
StructuredDocumentTag repeatingSectionItemSdt =
new StructuredDocumentTag(doc, SdtType.RepeatingSectionItem, MarkupLevel.Row);
repeatingSectionSdt.AppendChild(repeatingSectionItemSdt);
Row row = new Row(doc);
repeatingSectionItemSdt.AppendChild(row);
StructuredDocumentTag titleSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
titleSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/title[1]", "");
row.AppendChild(titleSdt);
StructuredDocumentTag authorSdt =
new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Cell);
authorSdt.XmlMapping.SetMapping(xmlPart, "/books[1]/book[1]/author[1]", "");
row.AppendChild(authorSdt);
doc.Save(dataDir + "Document.docx");