SdtListItem
Namespace:
Aspose.Words
We found 10 examples in language CSharp for this search.
You will see 51 fragments of code.
Other methods
Other methods
Project:PersonalSpire
File:SdtListItem.cs
Examples:1
internal SdtListItem method_0()
{
return (SdtListItem) base.MemberwiseClone();
}
Project:PersonalSpire
File:SdtListItemCollection.cs
Examples:2
public void Add(SdtListItem item)
{
this.list_0.Add(item);
}
internal SdtListItemCollection method_0()
{
SdtListItemCollection items = (SdtListItemCollection) base.MemberwiseClone();
items.list_0 = new List<SdtListItem>(this.list_0.Count);
for (int i = 0; i < this.list_0.Count; i++)
{
items.Add(this[i].method_0());
}
return items;
}
Project:Aspose.Word
File:ExStructuredDocumentTag.cs
Examples:6
[Test]
public void RepeatingSection()
{
//ExStart
//ExFor:StructuredDocumentTag.SdtType
//ExSummary:Shows how to get type of structured document tag.
Document doc = new Document(MyDir + "Structured document tags.docx");
List<StructuredDocumentTag> sdTags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().ToList();
Assert.AreEqual(SdtType.RepeatingSection, sdTags[0].SdtType);
Assert.AreEqual(SdtType.RepeatingSectionItem, sdTags[1].SdtType);
Assert.AreEqual(SdtType.RichText, sdTags[2].SdtType);
//ExEnd
}
[Test]
public void SetSpecificStyleToSdt()
{
//ExStart
//ExFor:StructuredDocumentTag
//ExFor:StructuredDocumentTag.NodeType
//ExFor:StructuredDocumentTag.Style
//ExFor:StructuredDocumentTag.StyleName
//ExFor:MarkupLevel
//ExFor:SdtType
//ExSummary:Shows how to work with styles for content control elements.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Get specific style from the document to apply it to an SDT
Style quoteStyle = doc.Styles[StyleIdentifier.Quote];
StructuredDocumentTag sdtPlainText = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
sdtPlainText.Style = quoteStyle;
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
// Second method to apply specific style to an SDT control
sdtRichText.StyleName = "Quote";
// Insert content controls into the document
builder.InsertNode(sdtPlainText);
builder.InsertNode(sdtRichText);
// We can get a collection of StructuredDocumentTags by looking for the document's child nodes of this NodeType
Assert.AreEqual(NodeType.StructuredDocumentTag, sdtPlainText.NodeType);
NodeCollection tags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);
foreach (Node node in tags)
{
StructuredDocumentTag sdt = (StructuredDocumentTag)node;
// If style was not defined before, style should be "Default Paragraph Font"
Assert.AreEqual(StyleIdentifier.Quote, sdt.Style.StyleIdentifier);
Assert.AreEqual("Quote", sdt.StyleName);
}
//ExEnd
}
[Test]
public void CheckBox()
{
//ExStart
//ExFor:StructuredDocumentTag.#ctor(DocumentBase, SdtType, MarkupLevel)
//ExFor:StructuredDocumentTag.Checked
//ExSummary:Show how to create and insert checkbox structured document tag.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
sdtCheckBox.Checked = true;
// Insert content control into the document
builder.InsertNode(sdtCheckBox);
//ExEnd
doc = DocumentHelper.SaveOpen(doc);
NodeCollection sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);
StructuredDocumentTag sdt = (StructuredDocumentTag) sdts[0];
Assert.AreEqual(true, sdt.Checked);
Assert.That(sdt.XmlMapping.StoreItemId, Is.Empty); //Assert that this sdt has no StoreItemId
}
#if NET462 || NETCOREAPP2_1 || JAVA // because of xamarin bug with CultureInfo (https://xamarin.github.io/bugzilla-archives/59/59077/bug.html)
[Test]
public void Date()
{
//ExStart
//ExFor:StructuredDocumentTag.CalendarType
//ExFor:StructuredDocumentTag.DateDisplayFormat
//ExFor:StructuredDocumentTag.DateDisplayLocale
//ExFor:StructuredDocumentTag.DateStorageFormat
//ExFor:StructuredDocumentTag.FullDate
//ExSummary:Shows how to prompt the user to enter a date with a StructuredDocumentTag.
// Create a new document
Document doc = new Document();
// Insert a StructuredDocumentTag that prompts the user to enter a date
// In Microsoft Word, this element is known as a "Date picker content control"
// When we click on the arrow on the right end of this tag in Microsoft Word,
// we will see a pop up in the form of a clickable calendar
// We can use that popup to select a date that will be displayed by the tag
StructuredDocumentTag sdtDate = new StructuredDocumentTag(doc, SdtType.Date, MarkupLevel.Inline);
// This attribute sets the language that the calendar will be displayed in,
// which in this case will be Saudi Arabian Arabic
sdtDate.DateDisplayLocale = CultureInfo.GetCultureInfo("ar-SA").LCID;
// We can set the format with which to display the date like this
// The locale we set above will be carried over to the displayed date
sdtDate.DateDisplayFormat = "dd MMMM, yyyy";
// Select how the data will be stored in the document
sdtDate.DateStorageFormat = SdtDateStorageFormat.DateTime;
// Set the calendar type that will be used to select and display the date
sdtDate.CalendarType = SdtCalendarType.Hijri;
// Before a date is chosen, the tag will display the text "Click here to enter a date."
// We can set a default date to display by setting this variable
// We must convert the date to the appropriate calendar ourselves
sdtDate.FullDate = new DateTime(1440, 10, 20);
// Insert the StructuredDocumentTag into the document with a DocumentBuilder and save the document
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(sdtDate);
doc.Save(ArtifactsDir + "StructuredDocumentTag.Date.docx");
//ExEnd
}
#endif
[Test]
public void PlainText()
{
//ExStart
//ExFor:StructuredDocumentTag.Color
//ExFor:StructuredDocumentTag.ContentsFont
//ExFor:StructuredDocumentTag.EndCharacterFont
//ExFor:StructuredDocumentTag.Id
//ExFor:StructuredDocumentTag.Level
//ExFor:StructuredDocumentTag.Multiline
//ExFor:StructuredDocumentTag.Tag
//ExFor:StructuredDocumentTag.Title
//ExFor:StructuredDocumentTag.RemoveSelfOnly
//ExSummary:Shows how to create a StructuredDocumentTag in the form of a plain text box and modify its appearance.
// Create a new document
Document doc = new Document();
// Create a StructuredDocumentTag that will contain plain text
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// Set the title and color of the frame that appears when you mouse over it
tag.Title = "My plain text";
tag.Color = Color.Magenta;
// Set a programmatic tag for this StructuredDocumentTag
// Unlike the title, this value will not be visible in the document but will be programmatically obtainable
// as an XML element named "tag", with the string below in its "@val" attribute
tag.Tag = "MyPlainTextSDT";
// Every StructuredDocumentTag gets a random unique ID
Assert.That(tag.Id, Is.Positive);
// Set the font for the text inside the StructuredDocumentTag
tag.ContentsFont.Name = "Arial";
// Set the font for the text at the end of the StructuredDocumentTag
// Any text that's typed in the document body after moving out of the tag with arrow keys will keep this font
tag.EndCharacterFont.Name = "Arial Black";
// By default, this is false and pressing enter while inside a StructuredDocumentTag does nothing
// When set to true, our StructuredDocumentTag can have multiple lines
tag.Multiline = true;
// Insert the StructuredDocumentTag into the document with a DocumentBuilder and save the document to a file
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
// Insert a clone of our StructuredDocumentTag in a new paragraph
StructuredDocumentTag tagClone = (StructuredDocumentTag)tag.Clone(true);
builder.InsertParagraph();
builder.InsertNode(tagClone);
// We can remove the tag while keeping its contents where they were in the Paragraph by calling RemoveSelfOnly()
tagClone.RemoveSelfOnly();
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
Assert.AreEqual("My plain text", tag.Title);
Assert.AreEqual(Color.Magenta.ToArgb(), tag.Color.ToArgb());
Assert.AreEqual("MyPlainTextSDT", tag.Tag);
Assert.That(tag.Id, Is.Positive);
Assert.AreEqual("Arial", tag.ContentsFont.Name);
Assert.AreEqual("Arial Black", tag.EndCharacterFont.Name);
Assert.True(tag.Multiline);
}
[Test]
public void IsTemporary()
{
//ExStart
//ExFor:StructuredDocumentTag.IsTemporary
//ExSummary:Demonstrates the effects of making a StructuredDocumentTag temporary.
Document doc = new Document();
// Insert a plain text StructuredDocumentTag, which will prompt the user to enter text
// and allow them to edit it like a text box
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// If we set its Temporary attribute to true, as soon as we start typing,
// the tag will disappear and its contents will be assimilated into the parent Paragraph
tag.IsTemporary = true;
// Insert the StructuredDocumentTag with a DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Temporary text box: ");
builder.InsertNode(tag);
// A StructuredDocumentTag in the form of a check box will let the user a square to check and uncheck
// Setting it to temporary will freeze its value after the first time it is clicked
tag = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
tag.IsTemporary = true;
builder.Write("\nTemporary checkbox: ");
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
Assert.AreEqual(2, doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Count(sdt => ((StructuredDocumentTag)sdt).IsTemporary));
}
[Test]
public void PlaceholderBuildingBlock()
{
//ExStart
//ExFor:StructuredDocumentTag.IsShowingPlaceholderText
//ExFor:StructuredDocumentTag.Placeholder
//ExFor:StructuredDocumentTag.PlaceholderName
//ExSummary:Shows how to use the contents of a BuildingBlock as a custom placeholder text for a StructuredDocumentTag.
Document doc = new Document();
// Insert a plain text StructuredDocumentTag of the PlainText type, which will function like a text box
// It contains a default "Click here to enter text." prompt, which we can click and replace with our own text
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// We can substitute that default placeholder with a custom phrase, which will be drawn from a BuildingBlock
// First we will need to create the BuildingBlock, give it content and add it to the GlossaryDocument
GlossaryDocument glossaryDoc = doc.GlossaryDocument;
BuildingBlock substituteBlock = new BuildingBlock(glossaryDoc);
substituteBlock.Name = "Custom Placeholder";
substituteBlock.AppendChild(new Section(glossaryDoc));
substituteBlock.FirstSection.AppendChild(new Body(glossaryDoc));
substituteBlock.FirstSection.Body.AppendParagraph("Custom placeholder text.");
glossaryDoc.AppendChild(substituteBlock);
// The substitute BuildingBlock we made can be referenced by name
tag.PlaceholderName = "Custom Placeholder";
// If PlaceholderName refers to an existing block in the parent document's GlossaryDocument,
// the BuildingBlock will be automatically found and assigned to the Placeholder attribute
Assert.AreEqual(substituteBlock, tag.Placeholder);
// Setting this to true will register the text inside the StructuredDocumentTag as placeholder text
// This means that, in Microsoft Word, all the text contents of the StructuredDocumentTag will be highlighted with one click,
// so we can immediately replace the entire substitute text by typing
// If this is false, the text will behave like an ordinary Paragraph and a cursor will be placed with nothing highlighted
tag.IsShowingPlaceholderText = true;
// Insert the StructuredDocumentTag into the document using a DocumentBuilder and save the document to a file
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
substituteBlock = (BuildingBlock)doc.GlossaryDocument.GetChild(NodeType.BuildingBlock, 0, true);
Assert.AreEqual("Custom Placeholder", substituteBlock.Name);
Assert.True(tag.IsShowingPlaceholderText);
Assert.AreEqual(substituteBlock, tag.Placeholder);
Assert.AreEqual(substituteBlock.Name, tag.PlaceholderName);
}
Project:WEB_DEV_COLLABORATIVE
File:Working with SDT.cs
Examples:6
[Test]
public void CheckBoxTypeContentControl()
{
//ExStart:CheckBoxTypeContentControl
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
builder.InsertNode(sdtCheckBox);
doc.Save(ArtifactsDir + "WorkingWithSdt.CheckBoxTypeContentControl.docx", SaveFormat.Docx);
//ExEnd:CheckBoxTypeContentControl
}
[Test]
public void CurrentStateOfCheckBox()
{
//ExStart:SetCurrentStateOfCheckBox
Document doc = new Document(MyDir + "Structured document tags.docx");
// Get the first content control from the document.
StructuredDocumentTag sdtCheckBox =
(StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
if (sdtCheckBox.SdtType == SdtType.Checkbox)
sdtCheckBox.Checked = true;
doc.Save(ArtifactsDir + "WorkingWithSdt.CurrentStateOfCheckBox.docx");
//ExEnd:SetCurrentStateOfCheckBox
}
[Test]
public void ModifyContentControls()
{
//ExStart:ModifyContentControls
Document doc = new Document(MyDir + "Structured document tags.docx");
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
switch (sdt.SdtType)
{
case 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);
break;
}
case SdtType.DropDownList:
{
SdtListItem secondItem = sdt.ListItems[2];
sdt.ListItems.SelectedValue = secondItem;
break;
}
case SdtType.Picture:
{
Shape shape = (Shape) sdt.GetChild(NodeType.Shape, 0, true);
if (shape.HasImage)
{
shape.ImageData.SetImage(ImagesDir + "Watermark.png");
}
break;
}
}
}
doc.Save(ArtifactsDir + "WorkingWithSdt.ModifyContentControls.docx");
//ExEnd:ModifyContentControls
}
[Test]
public void ComboBoxContentControl()
{
//ExStart:ComboBoxContentControl
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);
doc.Save(ArtifactsDir + "WorkingWithSdt.ComboBoxContentControl.docx");
//ExEnd:ComboBoxContentControl
}
[Test]
public void RichTextBoxContentControl()
{
//ExStart:RichTextBoxContentControl
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);
doc.Save(ArtifactsDir + "WorkingWithSdt.RichTextBoxContentControl.docx");
//ExEnd:RichTextBoxContentControl
}
[Test]
public void SetContentControlColor()
{
//ExStart:SetContentControlColor
Document doc = new Document(MyDir + "Structured document tags.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
sdt.Color = Color.Red;
doc.Save(ArtifactsDir + "WorkingWithSdt.SetContentControlColor.docx");
//ExEnd:SetContentControlColor
}
Project:WEB_DEV_COLLABORATIVE
File:ExStructuredDocumentTag.cs
Examples:6
[Test]
public void RepeatingSection()
{
//ExStart
//ExFor:StructuredDocumentTag.SdtType
//ExSummary:Shows how to get the type of a structured document tag.
Document doc = new Document(MyDir + "Structured document tags.docx");
List<StructuredDocumentTag> sdTags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().ToList();
Assert.AreEqual(SdtType.RepeatingSection, sdTags[0].SdtType);
Assert.AreEqual(SdtType.RepeatingSectionItem, sdTags[1].SdtType);
Assert.AreEqual(SdtType.RichText, sdTags[2].SdtType);
//ExEnd
}
[Test]
public void ApplyStyle()
{
//ExStart
//ExFor:StructuredDocumentTag
//ExFor:StructuredDocumentTag.NodeType
//ExFor:StructuredDocumentTag.Style
//ExFor:StructuredDocumentTag.StyleName
//ExFor:MarkupLevel
//ExFor:SdtType
//ExSummary:Shows how to work with styles for content control elements.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are two ways to apply a style from the document to a structured document tag.
// 1 - Apply a style object from the document's style collection:
Style quoteStyle = doc.Styles[StyleIdentifier.Quote];
StructuredDocumentTag sdtPlainText = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
sdtPlainText.Style = quoteStyle;
// 2 - Reference a style in the document by name:
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
sdtRichText.StyleName = "Quote";
builder.InsertNode(sdtPlainText);
builder.InsertNode(sdtRichText);
Assert.AreEqual(NodeType.StructuredDocumentTag, sdtPlainText.NodeType);
NodeCollection tags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);
foreach (Node node in tags)
{
StructuredDocumentTag sdt = (StructuredDocumentTag)node;
Assert.AreEqual(StyleIdentifier.Quote, sdt.Style.StyleIdentifier);
Assert.AreEqual("Quote", sdt.StyleName);
}
//ExEnd
}
[Test]
public void CheckBox()
{
//ExStart
//ExFor:StructuredDocumentTag.#ctor(DocumentBase, SdtType, MarkupLevel)
//ExFor:StructuredDocumentTag.Checked
//ExSummary:Show how to create a structured document tag in the form of a check box.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
sdtCheckBox.Checked = true;
builder.InsertNode(sdtCheckBox);
doc.Save(ArtifactsDir + "StructuredDocumentTag.CheckBox.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.CheckBox.docx");
StructuredDocumentTag[] sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().ToArray();
Assert.AreEqual(true, sdts[0].Checked);
Assert.That(sdts[0].XmlMapping.StoreItemId, Is.Empty);
}
#if NET462 || NETCOREAPP2_1 || JAVA // because of a Xamarin bug with CultureInfo (https://xamarin.github.io/bugzilla-archives/59/59077/bug.html)
[Test, Category("SkipMono")]
public void Date()
{
//ExStart
//ExFor:StructuredDocumentTag.CalendarType
//ExFor:StructuredDocumentTag.DateDisplayFormat
//ExFor:StructuredDocumentTag.DateDisplayLocale
//ExFor:StructuredDocumentTag.DateStorageFormat
//ExFor:StructuredDocumentTag.FullDate
//ExSummary:Shows how to prompt the user to enter a date with a structured document tag.
Document doc = new Document();
// Insert a structured document tag that prompts the user to enter a date.
// In Microsoft Word, this element is known as a "Date picker content control".
// When we click on the arrow on the right end of this tag in Microsoft Word,
// we will see a pop up in the form of a clickable calendar.
// We can use that popup to select a date that the tag will display.
StructuredDocumentTag sdtDate = new StructuredDocumentTag(doc, SdtType.Date, MarkupLevel.Inline);
// Display the date, according to the Saudi Arabian Arabic locale.
sdtDate.DateDisplayLocale = CultureInfo.GetCultureInfo("ar-SA").LCID;
// Set the format with which to display the date.
sdtDate.DateDisplayFormat = "dd MMMM, yyyy";
sdtDate.DateStorageFormat = SdtDateStorageFormat.DateTime;
// Display the date according to the Hijri calendar.
sdtDate.CalendarType = SdtCalendarType.Hijri;
// Before the user chooses a date in Microsoft Word, the tag will display the text "Click here to enter a date.".
// According to the tag's calendar, set the "FullDate" property to get the tag to display a default date.
sdtDate.FullDate = new DateTime(1440, 10, 20);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(sdtDate);
doc.Save(ArtifactsDir + "StructuredDocumentTag.Date.docx");
//ExEnd
}
#endif
[Test]
public void PlainText()
{
//ExStart
//ExFor:StructuredDocumentTag.Color
//ExFor:StructuredDocumentTag.ContentsFont
//ExFor:StructuredDocumentTag.EndCharacterFont
//ExFor:StructuredDocumentTag.Id
//ExFor:StructuredDocumentTag.Level
//ExFor:StructuredDocumentTag.Multiline
//ExFor:StructuredDocumentTag.Tag
//ExFor:StructuredDocumentTag.Title
//ExFor:StructuredDocumentTag.RemoveSelfOnly
//ExSummary:Shows how to create a structured document tag in a plain text box and modify its appearance.
Document doc = new Document();
// Create a structured document tag that will contain plain text.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// Set the title and color of the frame that appears when you mouse over the structured document tag in Microsoft Word.
tag.Title = "My plain text";
tag.Color = Color.Magenta;
// Set a tag for this structured document tag, which is obtainable
// as an XML element named "tag", with the string below in its "@val" attribute.
tag.Tag = "MyPlainTextSDT";
// Every structured document tag has a random unique ID.
Assert.That(tag.Id, Is.Positive);
// Set the font for the text inside the structured document tag.
tag.ContentsFont.Name = "Arial";
// Set the font for the text at the end of the structured document tag.
// Any text that we type in the document body after moving out of the tag with arrow keys will use this font.
tag.EndCharacterFont.Name = "Arial Black";
// By default, this is false and pressing enter while inside a structured document tag does nothing.
// When set to true, our structured document tag can have multiple lines.
// Set the "Multiline" property to "false" to only allow the contents
// of this structured document tag to span a single line.
// Set the "Multiline" property to "true" to allow the tag to contain multiple lines of content.
tag.Multiline = true;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
// Insert a clone of our structured document tag in a new paragraph.
StructuredDocumentTag tagClone = (StructuredDocumentTag)tag.Clone(true);
builder.InsertParagraph();
builder.InsertNode(tagClone);
// Use the "RemoveSelfOnly" method to remove a structured document tag, while keeping its contents in the document.
tagClone.RemoveSelfOnly();
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
Assert.AreEqual("My plain text", tag.Title);
Assert.AreEqual(Color.Magenta.ToArgb(), tag.Color.ToArgb());
Assert.AreEqual("MyPlainTextSDT", tag.Tag);
Assert.That(tag.Id, Is.Positive);
Assert.AreEqual("Arial", tag.ContentsFont.Name);
Assert.AreEqual("Arial Black", tag.EndCharacterFont.Name);
Assert.True(tag.Multiline);
}
[TestCase(false)]
[TestCase(true)]
public void IsTemporary(bool isTemporary)
{
//ExStart
//ExFor:StructuredDocumentTag.IsTemporary
//ExSummary:Shows how to make single-use controls.
Document doc = new Document();
// Insert a plain text structured document tag,
// which will act as a plain text form that the user may enter text into.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// Set the "IsTemporary" property to "true" to make the structured document tag disappear and
// assimilate its contents into the document after the user edits it once in Microsoft Word.
// Set the "IsTemporary" property to "false" to allow the user to edit the contents
// of the structured document tag any number of times.
tag.IsTemporary = isTemporary;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Please enter text: ");
builder.InsertNode(tag);
// Insert another structured document tag in the form of a check box and set its default state to "checked".
tag = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
tag.Checked = true;
// Set the "IsTemporary" property to "true" to make the check box become a symbol
// once the user clicks on it in Microsoft Word.
// Set the "IsTemporary" property to "false" to allow the user to click on the check box any number of times.
tag.IsTemporary = isTemporary;
builder.Write("\nPlease click the check box: ");
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
Assert.AreEqual(2,
doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Count(sdt => ((StructuredDocumentTag)sdt).IsTemporary == isTemporary));
}
[TestCase(false)]
[TestCase(true)]
public void PlaceholderBuildingBlock(bool isShowingPlaceholderText)
{
//ExStart
//ExFor:StructuredDocumentTag.IsShowingPlaceholderText
//ExFor:StructuredDocumentTag.Placeholder
//ExFor:StructuredDocumentTag.PlaceholderName
//ExSummary:Shows how to use a building block's contents as a custom placeholder text for a structured document tag.
Document doc = new Document();
// Insert a plain text structured document tag of the "PlainText" type, which will function as a text box.
// The contents that it will display by default are a "Click here to enter text." prompt.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// We can get the tag to display the contents of a building block instead of the default text.
// First, add a building block with contents to the glossary document.
GlossaryDocument glossaryDoc = doc.GlossaryDocument;
BuildingBlock substituteBlock = new BuildingBlock(glossaryDoc);
substituteBlock.Name = "Custom Placeholder";
substituteBlock.AppendChild(new Section(glossaryDoc));
substituteBlock.FirstSection.AppendChild(new Body(glossaryDoc));
substituteBlock.FirstSection.Body.AppendParagraph("Custom placeholder text.");
glossaryDoc.AppendChild(substituteBlock);
// Then, use the structured document tag's "PlaceholderName" property to reference that building block by name.
tag.PlaceholderName = "Custom Placeholder";
// If "PlaceholderName" refers to an existing block in the parent document's glossary document,
// we will be able to verify the building block via the "Placeholder" property.
Assert.AreEqual(substituteBlock, tag.Placeholder);
// Set the "IsShowingPlaceholderText" property to "true" to treat the
// structured document tag's current contents as placeholder text.
// This means that clicking on the text box in Microsoft Word will immediately highlight all the tag's contents.
// Set the "IsShowingPlaceholderText" property to "false" to get the
// structured document tag to treat its contents as text that a user has already entered.
// Clicking on this text in Microsoft Word will place the blinking cursor at the clicked location.
tag.IsShowingPlaceholderText = isShowingPlaceholderText;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
substituteBlock = (BuildingBlock)doc.GlossaryDocument.GetChild(NodeType.BuildingBlock, 0, true);
Assert.AreEqual("Custom Placeholder", substituteBlock.Name);
Assert.AreEqual(isShowingPlaceholderText, tag.IsShowingPlaceholderText);
Assert.AreEqual(substituteBlock, tag.Placeholder);
Assert.AreEqual(substituteBlock.Name, tag.PlaceholderName);
}
Project:WEB_DEV_COLLABORATIVE
File:ExStructuredDocumentTag.cs
Examples:6
[Test]
public void RepeatingSection()
{
//ExStart
//ExFor:StructuredDocumentTag.SdtType
//ExSummary:Shows how to get the type of a structured document tag.
Document doc = new Document(MyDir + "Structured document tags.docx");
List<StructuredDocumentTag> sdTags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().ToList();
Assert.AreEqual(SdtType.RepeatingSection, sdTags[0].SdtType);
Assert.AreEqual(SdtType.RepeatingSectionItem, sdTags[1].SdtType);
Assert.AreEqual(SdtType.RichText, sdTags[2].SdtType);
//ExEnd
}
[Test]
public void ApplyStyle()
{
//ExStart
//ExFor:StructuredDocumentTag
//ExFor:StructuredDocumentTag.NodeType
//ExFor:StructuredDocumentTag.Style
//ExFor:StructuredDocumentTag.StyleName
//ExFor:MarkupLevel
//ExFor:SdtType
//ExSummary:Shows how to work with styles for content control elements.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are two ways to apply a style from the document to a structured document tag.
// 1 - Apply a style object from the document's style collection:
Style quoteStyle = doc.Styles[StyleIdentifier.Quote];
StructuredDocumentTag sdtPlainText = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
sdtPlainText.Style = quoteStyle;
// 2 - Reference a style in the document by name:
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
sdtRichText.StyleName = "Quote";
builder.InsertNode(sdtPlainText);
builder.InsertNode(sdtRichText);
Assert.AreEqual(NodeType.StructuredDocumentTag, sdtPlainText.NodeType);
NodeCollection tags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);
foreach (Node node in tags)
{
StructuredDocumentTag sdt = (StructuredDocumentTag)node;
Assert.AreEqual(StyleIdentifier.Quote, sdt.Style.StyleIdentifier);
Assert.AreEqual("Quote", sdt.StyleName);
}
//ExEnd
}
[Test]
public void CheckBox()
{
//ExStart
//ExFor:StructuredDocumentTag.#ctor(DocumentBase, SdtType, MarkupLevel)
//ExFor:StructuredDocumentTag.Checked
//ExSummary:Show how to create a structured document tag in the form of a check box.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
sdtCheckBox.Checked = true;
builder.InsertNode(sdtCheckBox);
doc.Save(ArtifactsDir + "StructuredDocumentTag.CheckBox.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.CheckBox.docx");
StructuredDocumentTag[] sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().ToArray();
Assert.AreEqual(true, sdts[0].Checked);
Assert.That(sdts[0].XmlMapping.StoreItemId, Is.Empty);
}
#if NET462 || NETCOREAPP2_1 || JAVA // because of a Xamarin bug with CultureInfo (https://xamarin.github.io/bugzilla-archives/59/59077/bug.html)
[Test, Category("SkipMono")]
public void Date()
{
//ExStart
//ExFor:StructuredDocumentTag.CalendarType
//ExFor:StructuredDocumentTag.DateDisplayFormat
//ExFor:StructuredDocumentTag.DateDisplayLocale
//ExFor:StructuredDocumentTag.DateStorageFormat
//ExFor:StructuredDocumentTag.FullDate
//ExSummary:Shows how to prompt the user to enter a date with a structured document tag.
Document doc = new Document();
// Insert a structured document tag that prompts the user to enter a date.
// In Microsoft Word, this element is known as a "Date picker content control".
// When we click on the arrow on the right end of this tag in Microsoft Word,
// we will see a pop up in the form of a clickable calendar.
// We can use that popup to select a date that the tag will display.
StructuredDocumentTag sdtDate = new StructuredDocumentTag(doc, SdtType.Date, MarkupLevel.Inline);
// Display the date, according to the Saudi Arabian Arabic locale.
sdtDate.DateDisplayLocale = CultureInfo.GetCultureInfo("ar-SA").LCID;
// Set the format with which to display the date.
sdtDate.DateDisplayFormat = "dd MMMM, yyyy";
sdtDate.DateStorageFormat = SdtDateStorageFormat.DateTime;
// Display the date according to the Hijri calendar.
sdtDate.CalendarType = SdtCalendarType.Hijri;
// Before the user chooses a date in Microsoft Word, the tag will display the text "Click here to enter a date.".
// According to the tag's calendar, set the "FullDate" property to get the tag to display a default date.
sdtDate.FullDate = new DateTime(1440, 10, 20);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(sdtDate);
doc.Save(ArtifactsDir + "StructuredDocumentTag.Date.docx");
//ExEnd
}
#endif
[Test]
public void PlainText()
{
//ExStart
//ExFor:StructuredDocumentTag.Color
//ExFor:StructuredDocumentTag.ContentsFont
//ExFor:StructuredDocumentTag.EndCharacterFont
//ExFor:StructuredDocumentTag.Id
//ExFor:StructuredDocumentTag.Level
//ExFor:StructuredDocumentTag.Multiline
//ExFor:StructuredDocumentTag.Tag
//ExFor:StructuredDocumentTag.Title
//ExFor:StructuredDocumentTag.RemoveSelfOnly
//ExSummary:Shows how to create a structured document tag in a plain text box and modify its appearance.
Document doc = new Document();
// Create a structured document tag that will contain plain text.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// Set the title and color of the frame that appears when you mouse over the structured document tag in Microsoft Word.
tag.Title = "My plain text";
tag.Color = Color.Magenta;
// Set a tag for this structured document tag, which is obtainable
// as an XML element named "tag", with the string below in its "@val" attribute.
tag.Tag = "MyPlainTextSDT";
// Every structured document tag has a random unique ID.
Assert.That(tag.Id, Is.Positive);
// Set the font for the text inside the structured document tag.
tag.ContentsFont.Name = "Arial";
// Set the font for the text at the end of the structured document tag.
// Any text that we type in the document body after moving out of the tag with arrow keys will use this font.
tag.EndCharacterFont.Name = "Arial Black";
// By default, this is false and pressing enter while inside a structured document tag does nothing.
// When set to true, our structured document tag can have multiple lines.
// Set the "Multiline" property to "false" to only allow the contents
// of this structured document tag to span a single line.
// Set the "Multiline" property to "true" to allow the tag to contain multiple lines of content.
tag.Multiline = true;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
// Insert a clone of our structured document tag in a new paragraph.
StructuredDocumentTag tagClone = (StructuredDocumentTag)tag.Clone(true);
builder.InsertParagraph();
builder.InsertNode(tagClone);
// Use the "RemoveSelfOnly" method to remove a structured document tag, while keeping its contents in the document.
tagClone.RemoveSelfOnly();
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
Assert.AreEqual("My plain text", tag.Title);
Assert.AreEqual(Color.Magenta.ToArgb(), tag.Color.ToArgb());
Assert.AreEqual("MyPlainTextSDT", tag.Tag);
Assert.That(tag.Id, Is.Positive);
Assert.AreEqual("Arial", tag.ContentsFont.Name);
Assert.AreEqual("Arial Black", tag.EndCharacterFont.Name);
Assert.True(tag.Multiline);
}
[TestCase(false)]
[TestCase(true)]
public void IsTemporary(bool isTemporary)
{
//ExStart
//ExFor:StructuredDocumentTag.IsTemporary
//ExSummary:Shows how to make single-use controls.
Document doc = new Document();
// Insert a plain text structured document tag,
// which will act as a plain text form that the user may enter text into.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// Set the "IsTemporary" property to "true" to make the structured document tag disappear and
// assimilate its contents into the document after the user edits it once in Microsoft Word.
// Set the "IsTemporary" property to "false" to allow the user to edit the contents
// of the structured document tag any number of times.
tag.IsTemporary = isTemporary;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Please enter text: ");
builder.InsertNode(tag);
// Insert another structured document tag in the form of a check box and set its default state to "checked".
tag = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
tag.Checked = true;
// Set the "IsTemporary" property to "true" to make the check box become a symbol
// once the user clicks on it in Microsoft Word.
// Set the "IsTemporary" property to "false" to allow the user to click on the check box any number of times.
tag.IsTemporary = isTemporary;
builder.Write("\nPlease click the check box: ");
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
Assert.AreEqual(2,
doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Count(sdt => ((StructuredDocumentTag)sdt).IsTemporary == isTemporary));
}
[TestCase(false)]
[TestCase(true)]
public void PlaceholderBuildingBlock(bool isShowingPlaceholderText)
{
//ExStart
//ExFor:StructuredDocumentTag.IsShowingPlaceholderText
//ExFor:StructuredDocumentTag.Placeholder
//ExFor:StructuredDocumentTag.PlaceholderName
//ExSummary:Shows how to use a building block's contents as a custom placeholder text for a structured document tag.
Document doc = new Document();
// Insert a plain text structured document tag of the "PlainText" type, which will function as a text box.
// The contents that it will display by default are a "Click here to enter text." prompt.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// We can get the tag to display the contents of a building block instead of the default text.
// First, add a building block with contents to the glossary document.
GlossaryDocument glossaryDoc = doc.GlossaryDocument;
BuildingBlock substituteBlock = new BuildingBlock(glossaryDoc);
substituteBlock.Name = "Custom Placeholder";
substituteBlock.AppendChild(new Section(glossaryDoc));
substituteBlock.FirstSection.AppendChild(new Body(glossaryDoc));
substituteBlock.FirstSection.Body.AppendParagraph("Custom placeholder text.");
glossaryDoc.AppendChild(substituteBlock);
// Then, use the structured document tag's "PlaceholderName" property to reference that building block by name.
tag.PlaceholderName = "Custom Placeholder";
// If "PlaceholderName" refers to an existing block in the parent document's glossary document,
// we will be able to verify the building block via the "Placeholder" property.
Assert.AreEqual(substituteBlock, tag.Placeholder);
// Set the "IsShowingPlaceholderText" property to "true" to treat the
// structured document tag's current contents as placeholder text.
// This means that clicking on the text box in Microsoft Word will immediately highlight all the tag's contents.
// Set the "IsShowingPlaceholderText" property to "false" to get the
// structured document tag to treat its contents as text that a user has already entered.
// Clicking on this text in Microsoft Word will place the blinking cursor at the clicked location.
tag.IsShowingPlaceholderText = isShowingPlaceholderText;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
substituteBlock = (BuildingBlock)doc.GlossaryDocument.GetChild(NodeType.BuildingBlock, 0, true);
Assert.AreEqual("Custom Placeholder", substituteBlock.Name);
Assert.AreEqual(isShowingPlaceholderText, tag.IsShowingPlaceholderText);
Assert.AreEqual(substituteBlock, tag.Placeholder);
Assert.AreEqual(substituteBlock.Name, tag.PlaceholderName);
}
Project:starred-repos
File:Working with SDT.cs
Examples:6
[Test]
public void CheckBoxTypeContentControl()
{
//ExStart:CheckBoxTypeContentControl
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
builder.InsertNode(sdtCheckBox);
doc.Save(ArtifactsDir + "WorkingWithSdt.CheckBoxTypeContentControl.docx", SaveFormat.Docx);
//ExEnd:CheckBoxTypeContentControl
}
[Test]
public void CurrentStateOfCheckBox()
{
//ExStart:SetCurrentStateOfCheckBox
Document doc = new Document(MyDir + "Structured document tags.docx");
// Get the first content control from the document.
StructuredDocumentTag sdtCheckBox =
(StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
if (sdtCheckBox.SdtType == SdtType.Checkbox)
sdtCheckBox.Checked = true;
doc.Save(ArtifactsDir + "WorkingWithSdt.CurrentStateOfCheckBox.docx");
//ExEnd:SetCurrentStateOfCheckBox
}
[Test]
public void ModifyContentControls()
{
//ExStart:ModifyContentControls
Document doc = new Document(MyDir + "Structured document tags.docx");
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
switch (sdt.SdtType)
{
case 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);
break;
}
case SdtType.DropDownList:
{
SdtListItem secondItem = sdt.ListItems[2];
sdt.ListItems.SelectedValue = secondItem;
break;
}
case SdtType.Picture:
{
Shape shape = (Shape) sdt.GetChild(NodeType.Shape, 0, true);
if (shape.HasImage)
{
shape.ImageData.SetImage(ImagesDir + "Watermark.png");
}
break;
}
}
}
doc.Save(ArtifactsDir + "WorkingWithSdt.ModifyContentControls.docx");
//ExEnd:ModifyContentControls
}
[Test]
public void ComboBoxContentControl()
{
//ExStart:ComboBoxContentControl
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);
doc.Save(ArtifactsDir + "WorkingWithSdt.ComboBoxContentControl.docx");
//ExEnd:ComboBoxContentControl
}
[Test]
public void RichTextBoxContentControl()
{
//ExStart:RichTextBoxContentControl
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);
doc.Save(ArtifactsDir + "WorkingWithSdt.RichTextBoxContentControl.docx");
//ExEnd:RichTextBoxContentControl
}
[Test]
public void SetContentControlColor()
{
//ExStart:SetContentControlColor
Document doc = new Document(MyDir + "Structured document tags.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
sdt.Color = Color.Red;
doc.Save(ArtifactsDir + "WorkingWithSdt.SetContentControlColor.docx");
//ExEnd:SetContentControlColor
}
Project:starred-repos
File:Working with SDT.cs
Examples:6
[Test]
public void CheckBoxTypeContentControl()
{
//ExStart:CheckBoxTypeContentControl
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
builder.InsertNode(sdtCheckBox);
doc.Save(ArtifactsDir + "WorkingWithSdt.CheckBoxTypeContentControl.docx", SaveFormat.Docx);
//ExEnd:CheckBoxTypeContentControl
}
[Test]
public void CurrentStateOfCheckBox()
{
//ExStart:SetCurrentStateOfCheckBox
Document doc = new Document(MyDir + "Structured document tags.docx");
// Get the first content control from the document.
StructuredDocumentTag sdtCheckBox =
(StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
if (sdtCheckBox.SdtType == SdtType.Checkbox)
sdtCheckBox.Checked = true;
doc.Save(ArtifactsDir + "WorkingWithSdt.CurrentStateOfCheckBox.docx");
//ExEnd:SetCurrentStateOfCheckBox
}
[Test]
public void ModifyContentControls()
{
//ExStart:ModifyContentControls
Document doc = new Document(MyDir + "Structured document tags.docx");
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
switch (sdt.SdtType)
{
case 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);
break;
}
case SdtType.DropDownList:
{
SdtListItem secondItem = sdt.ListItems[2];
sdt.ListItems.SelectedValue = secondItem;
break;
}
case SdtType.Picture:
{
Shape shape = (Shape) sdt.GetChild(NodeType.Shape, 0, true);
if (shape.HasImage)
{
shape.ImageData.SetImage(ImagesDir + "Watermark.png");
}
break;
}
}
}
doc.Save(ArtifactsDir + "WorkingWithSdt.ModifyContentControls.docx");
//ExEnd:ModifyContentControls
}
[Test]
public void ComboBoxContentControl()
{
//ExStart:ComboBoxContentControl
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);
doc.Save(ArtifactsDir + "WorkingWithSdt.ComboBoxContentControl.docx");
//ExEnd:ComboBoxContentControl
}
[Test]
public void RichTextBoxContentControl()
{
//ExStart:RichTextBoxContentControl
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);
doc.Save(ArtifactsDir + "WorkingWithSdt.RichTextBoxContentControl.docx");
//ExEnd:RichTextBoxContentControl
}
[Test]
public void SetContentControlColor()
{
//ExStart:SetContentControlColor
Document doc = new Document(MyDir + "Structured document tags.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
sdt.Color = Color.Red;
doc.Save(ArtifactsDir + "WorkingWithSdt.SetContentControlColor.docx");
//ExEnd:SetContentControlColor
}
Project:starred-repos
File:ExStructuredDocumentTag.cs
Examples:6
[Test]
public void RepeatingSection()
{
//ExStart
//ExFor:StructuredDocumentTag.SdtType
//ExSummary:Shows how to get the type of a structured document tag.
Document doc = new Document(MyDir + "Structured document tags.docx");
List<StructuredDocumentTag> sdTags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().ToList();
Assert.AreEqual(SdtType.RepeatingSection, sdTags[0].SdtType);
Assert.AreEqual(SdtType.RepeatingSectionItem, sdTags[1].SdtType);
Assert.AreEqual(SdtType.RichText, sdTags[2].SdtType);
//ExEnd
}
[Test]
public void ApplyStyle()
{
//ExStart
//ExFor:StructuredDocumentTag
//ExFor:StructuredDocumentTag.NodeType
//ExFor:StructuredDocumentTag.Style
//ExFor:StructuredDocumentTag.StyleName
//ExFor:MarkupLevel
//ExFor:SdtType
//ExSummary:Shows how to work with styles for content control elements.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are two ways to apply a style from the document to a structured document tag.
// 1 - Apply a style object from the document's style collection:
Style quoteStyle = doc.Styles[StyleIdentifier.Quote];
StructuredDocumentTag sdtPlainText = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
sdtPlainText.Style = quoteStyle;
// 2 - Reference a style in the document by name:
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
sdtRichText.StyleName = "Quote";
builder.InsertNode(sdtPlainText);
builder.InsertNode(sdtRichText);
Assert.AreEqual(NodeType.StructuredDocumentTag, sdtPlainText.NodeType);
NodeCollection tags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);
foreach (Node node in tags)
{
StructuredDocumentTag sdt = (StructuredDocumentTag)node;
Assert.AreEqual(StyleIdentifier.Quote, sdt.Style.StyleIdentifier);
Assert.AreEqual("Quote", sdt.StyleName);
}
//ExEnd
}
[Test]
public void CheckBox()
{
//ExStart
//ExFor:StructuredDocumentTag.#ctor(DocumentBase, SdtType, MarkupLevel)
//ExFor:StructuredDocumentTag.Checked
//ExSummary:Show how to create a structured document tag in the form of a check box.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
sdtCheckBox.Checked = true;
builder.InsertNode(sdtCheckBox);
doc.Save(ArtifactsDir + "StructuredDocumentTag.CheckBox.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.CheckBox.docx");
StructuredDocumentTag[] sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().ToArray();
Assert.AreEqual(true, sdts[0].Checked);
Assert.That(sdts[0].XmlMapping.StoreItemId, Is.Empty);
}
#if NET462 || NETCOREAPP2_1 || JAVA // because of a Xamarin bug with CultureInfo (https://xamarin.github.io/bugzilla-archives/59/59077/bug.html)
[Test, Category("SkipMono")]
public void Date()
{
//ExStart
//ExFor:StructuredDocumentTag.CalendarType
//ExFor:StructuredDocumentTag.DateDisplayFormat
//ExFor:StructuredDocumentTag.DateDisplayLocale
//ExFor:StructuredDocumentTag.DateStorageFormat
//ExFor:StructuredDocumentTag.FullDate
//ExSummary:Shows how to prompt the user to enter a date with a structured document tag.
Document doc = new Document();
// Insert a structured document tag that prompts the user to enter a date.
// In Microsoft Word, this element is known as a "Date picker content control".
// When we click on the arrow on the right end of this tag in Microsoft Word,
// we will see a pop up in the form of a clickable calendar.
// We can use that popup to select a date that the tag will display.
StructuredDocumentTag sdtDate = new StructuredDocumentTag(doc, SdtType.Date, MarkupLevel.Inline);
// Display the date, according to the Saudi Arabian Arabic locale.
sdtDate.DateDisplayLocale = CultureInfo.GetCultureInfo("ar-SA").LCID;
// Set the format with which to display the date.
sdtDate.DateDisplayFormat = "dd MMMM, yyyy";
sdtDate.DateStorageFormat = SdtDateStorageFormat.DateTime;
// Display the date according to the Hijri calendar.
sdtDate.CalendarType = SdtCalendarType.Hijri;
// Before the user chooses a date in Microsoft Word, the tag will display the text "Click here to enter a date.".
// According to the tag's calendar, set the "FullDate" property to get the tag to display a default date.
sdtDate.FullDate = new DateTime(1440, 10, 20);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(sdtDate);
doc.Save(ArtifactsDir + "StructuredDocumentTag.Date.docx");
//ExEnd
}
#endif
[Test]
public void PlainText()
{
//ExStart
//ExFor:StructuredDocumentTag.Color
//ExFor:StructuredDocumentTag.ContentsFont
//ExFor:StructuredDocumentTag.EndCharacterFont
//ExFor:StructuredDocumentTag.Id
//ExFor:StructuredDocumentTag.Level
//ExFor:StructuredDocumentTag.Multiline
//ExFor:StructuredDocumentTag.Tag
//ExFor:StructuredDocumentTag.Title
//ExFor:StructuredDocumentTag.RemoveSelfOnly
//ExSummary:Shows how to create a structured document tag in a plain text box and modify its appearance.
Document doc = new Document();
// Create a structured document tag that will contain plain text.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// Set the title and color of the frame that appears when you mouse over the structured document tag in Microsoft Word.
tag.Title = "My plain text";
tag.Color = Color.Magenta;
// Set a tag for this structured document tag, which is obtainable
// as an XML element named "tag", with the string below in its "@val" attribute.
tag.Tag = "MyPlainTextSDT";
// Every structured document tag has a random unique ID.
Assert.That(tag.Id, Is.Positive);
// Set the font for the text inside the structured document tag.
tag.ContentsFont.Name = "Arial";
// Set the font for the text at the end of the structured document tag.
// Any text that we type in the document body after moving out of the tag with arrow keys will use this font.
tag.EndCharacterFont.Name = "Arial Black";
// By default, this is false and pressing enter while inside a structured document tag does nothing.
// When set to true, our structured document tag can have multiple lines.
// Set the "Multiline" property to "false" to only allow the contents
// of this structured document tag to span a single line.
// Set the "Multiline" property to "true" to allow the tag to contain multiple lines of content.
tag.Multiline = true;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
// Insert a clone of our structured document tag in a new paragraph.
StructuredDocumentTag tagClone = (StructuredDocumentTag)tag.Clone(true);
builder.InsertParagraph();
builder.InsertNode(tagClone);
// Use the "RemoveSelfOnly" method to remove a structured document tag, while keeping its contents in the document.
tagClone.RemoveSelfOnly();
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
Assert.AreEqual("My plain text", tag.Title);
Assert.AreEqual(Color.Magenta.ToArgb(), tag.Color.ToArgb());
Assert.AreEqual("MyPlainTextSDT", tag.Tag);
Assert.That(tag.Id, Is.Positive);
Assert.AreEqual("Arial", tag.ContentsFont.Name);
Assert.AreEqual("Arial Black", tag.EndCharacterFont.Name);
Assert.True(tag.Multiline);
}
[TestCase(false)]
[TestCase(true)]
public void IsTemporary(bool isTemporary)
{
//ExStart
//ExFor:StructuredDocumentTag.IsTemporary
//ExSummary:Shows how to make single-use controls.
Document doc = new Document();
// Insert a plain text structured document tag,
// which will act as a plain text form that the user may enter text into.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// Set the "IsTemporary" property to "true" to make the structured document tag disappear and
// assimilate its contents into the document after the user edits it once in Microsoft Word.
// Set the "IsTemporary" property to "false" to allow the user to edit the contents
// of the structured document tag any number of times.
tag.IsTemporary = isTemporary;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Please enter text: ");
builder.InsertNode(tag);
// Insert another structured document tag in the form of a check box and set its default state to "checked".
tag = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
tag.Checked = true;
// Set the "IsTemporary" property to "true" to make the check box become a symbol
// once the user clicks on it in Microsoft Word.
// Set the "IsTemporary" property to "false" to allow the user to click on the check box any number of times.
tag.IsTemporary = isTemporary;
builder.Write("\nPlease click the check box: ");
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
Assert.AreEqual(2,
doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Count(sdt => ((StructuredDocumentTag)sdt).IsTemporary == isTemporary));
}
[TestCase(false)]
[TestCase(true)]
public void PlaceholderBuildingBlock(bool isShowingPlaceholderText)
{
//ExStart
//ExFor:StructuredDocumentTag.IsShowingPlaceholderText
//ExFor:StructuredDocumentTag.Placeholder
//ExFor:StructuredDocumentTag.PlaceholderName
//ExSummary:Shows how to use a building block's contents as a custom placeholder text for a structured document tag.
Document doc = new Document();
// Insert a plain text structured document tag of the "PlainText" type, which will function as a text box.
// The contents that it will display by default are a "Click here to enter text." prompt.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// We can get the tag to display the contents of a building block instead of the default text.
// First, add a building block with contents to the glossary document.
GlossaryDocument glossaryDoc = doc.GlossaryDocument;
BuildingBlock substituteBlock = new BuildingBlock(glossaryDoc);
substituteBlock.Name = "Custom Placeholder";
substituteBlock.AppendChild(new Section(glossaryDoc));
substituteBlock.FirstSection.AppendChild(new Body(glossaryDoc));
substituteBlock.FirstSection.Body.AppendParagraph("Custom placeholder text.");
glossaryDoc.AppendChild(substituteBlock);
// Then, use the structured document tag's "PlaceholderName" property to reference that building block by name.
tag.PlaceholderName = "Custom Placeholder";
// If "PlaceholderName" refers to an existing block in the parent document's glossary document,
// we will be able to verify the building block via the "Placeholder" property.
Assert.AreEqual(substituteBlock, tag.Placeholder);
// Set the "IsShowingPlaceholderText" property to "true" to treat the
// structured document tag's current contents as placeholder text.
// This means that clicking on the text box in Microsoft Word will immediately highlight all the tag's contents.
// Set the "IsShowingPlaceholderText" property to "false" to get the
// structured document tag to treat its contents as text that a user has already entered.
// Clicking on this text in Microsoft Word will place the blinking cursor at the clicked location.
tag.IsShowingPlaceholderText = isShowingPlaceholderText;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
substituteBlock = (BuildingBlock)doc.GlossaryDocument.GetChild(NodeType.BuildingBlock, 0, true);
Assert.AreEqual("Custom Placeholder", substituteBlock.Name);
Assert.AreEqual(isShowingPlaceholderText, tag.IsShowingPlaceholderText);
Assert.AreEqual(substituteBlock, tag.Placeholder);
Assert.AreEqual(substituteBlock.Name, tag.PlaceholderName);
}
Project:starred-repos
File:ExStructuredDocumentTag.cs
Examples:6
[Test]
public void RepeatingSection()
{
//ExStart
//ExFor:StructuredDocumentTag.SdtType
//ExSummary:Shows how to get the type of a structured document tag.
Document doc = new Document(MyDir + "Structured document tags.docx");
List<StructuredDocumentTag> sdTags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().ToList();
Assert.AreEqual(SdtType.RepeatingSection, sdTags[0].SdtType);
Assert.AreEqual(SdtType.RepeatingSectionItem, sdTags[1].SdtType);
Assert.AreEqual(SdtType.RichText, sdTags[2].SdtType);
//ExEnd
}
[Test]
public void ApplyStyle()
{
//ExStart
//ExFor:StructuredDocumentTag
//ExFor:StructuredDocumentTag.NodeType
//ExFor:StructuredDocumentTag.Style
//ExFor:StructuredDocumentTag.StyleName
//ExFor:MarkupLevel
//ExFor:SdtType
//ExSummary:Shows how to work with styles for content control elements.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Below are two ways to apply a style from the document to a structured document tag.
// 1 - Apply a style object from the document's style collection:
Style quoteStyle = doc.Styles[StyleIdentifier.Quote];
StructuredDocumentTag sdtPlainText = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
sdtPlainText.Style = quoteStyle;
// 2 - Reference a style in the document by name:
StructuredDocumentTag sdtRichText = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
sdtRichText.StyleName = "Quote";
builder.InsertNode(sdtPlainText);
builder.InsertNode(sdtRichText);
Assert.AreEqual(NodeType.StructuredDocumentTag, sdtPlainText.NodeType);
NodeCollection tags = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);
foreach (Node node in tags)
{
StructuredDocumentTag sdt = (StructuredDocumentTag)node;
Assert.AreEqual(StyleIdentifier.Quote, sdt.Style.StyleIdentifier);
Assert.AreEqual("Quote", sdt.StyleName);
}
//ExEnd
}
[Test]
public void CheckBox()
{
//ExStart
//ExFor:StructuredDocumentTag.#ctor(DocumentBase, SdtType, MarkupLevel)
//ExFor:StructuredDocumentTag.Checked
//ExSummary:Show how to create a structured document tag in the form of a check box.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdtCheckBox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
sdtCheckBox.Checked = true;
builder.InsertNode(sdtCheckBox);
doc.Save(ArtifactsDir + "StructuredDocumentTag.CheckBox.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.CheckBox.docx");
StructuredDocumentTag[] sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).OfType<StructuredDocumentTag>().ToArray();
Assert.AreEqual(true, sdts[0].Checked);
Assert.That(sdts[0].XmlMapping.StoreItemId, Is.Empty);
}
#if NET462 || NETCOREAPP2_1 || JAVA // because of a Xamarin bug with CultureInfo (https://xamarin.github.io/bugzilla-archives/59/59077/bug.html)
[Test, Category("SkipMono")]
public void Date()
{
//ExStart
//ExFor:StructuredDocumentTag.CalendarType
//ExFor:StructuredDocumentTag.DateDisplayFormat
//ExFor:StructuredDocumentTag.DateDisplayLocale
//ExFor:StructuredDocumentTag.DateStorageFormat
//ExFor:StructuredDocumentTag.FullDate
//ExSummary:Shows how to prompt the user to enter a date with a structured document tag.
Document doc = new Document();
// Insert a structured document tag that prompts the user to enter a date.
// In Microsoft Word, this element is known as a "Date picker content control".
// When we click on the arrow on the right end of this tag in Microsoft Word,
// we will see a pop up in the form of a clickable calendar.
// We can use that popup to select a date that the tag will display.
StructuredDocumentTag sdtDate = new StructuredDocumentTag(doc, SdtType.Date, MarkupLevel.Inline);
// Display the date, according to the Saudi Arabian Arabic locale.
sdtDate.DateDisplayLocale = CultureInfo.GetCultureInfo("ar-SA").LCID;
// Set the format with which to display the date.
sdtDate.DateDisplayFormat = "dd MMMM, yyyy";
sdtDate.DateStorageFormat = SdtDateStorageFormat.DateTime;
// Display the date according to the Hijri calendar.
sdtDate.CalendarType = SdtCalendarType.Hijri;
// Before the user chooses a date in Microsoft Word, the tag will display the text "Click here to enter a date.".
// According to the tag's calendar, set the "FullDate" property to get the tag to display a default date.
sdtDate.FullDate = new DateTime(1440, 10, 20);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(sdtDate);
doc.Save(ArtifactsDir + "StructuredDocumentTag.Date.docx");
//ExEnd
}
#endif
[Test]
public void PlainText()
{
//ExStart
//ExFor:StructuredDocumentTag.Color
//ExFor:StructuredDocumentTag.ContentsFont
//ExFor:StructuredDocumentTag.EndCharacterFont
//ExFor:StructuredDocumentTag.Id
//ExFor:StructuredDocumentTag.Level
//ExFor:StructuredDocumentTag.Multiline
//ExFor:StructuredDocumentTag.Tag
//ExFor:StructuredDocumentTag.Title
//ExFor:StructuredDocumentTag.RemoveSelfOnly
//ExSummary:Shows how to create a structured document tag in a plain text box and modify its appearance.
Document doc = new Document();
// Create a structured document tag that will contain plain text.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// Set the title and color of the frame that appears when you mouse over the structured document tag in Microsoft Word.
tag.Title = "My plain text";
tag.Color = Color.Magenta;
// Set a tag for this structured document tag, which is obtainable
// as an XML element named "tag", with the string below in its "@val" attribute.
tag.Tag = "MyPlainTextSDT";
// Every structured document tag has a random unique ID.
Assert.That(tag.Id, Is.Positive);
// Set the font for the text inside the structured document tag.
tag.ContentsFont.Name = "Arial";
// Set the font for the text at the end of the structured document tag.
// Any text that we type in the document body after moving out of the tag with arrow keys will use this font.
tag.EndCharacterFont.Name = "Arial Black";
// By default, this is false and pressing enter while inside a structured document tag does nothing.
// When set to true, our structured document tag can have multiple lines.
// Set the "Multiline" property to "false" to only allow the contents
// of this structured document tag to span a single line.
// Set the "Multiline" property to "true" to allow the tag to contain multiple lines of content.
tag.Multiline = true;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
// Insert a clone of our structured document tag in a new paragraph.
StructuredDocumentTag tagClone = (StructuredDocumentTag)tag.Clone(true);
builder.InsertParagraph();
builder.InsertNode(tagClone);
// Use the "RemoveSelfOnly" method to remove a structured document tag, while keeping its contents in the document.
tagClone.RemoveSelfOnly();
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlainText.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
Assert.AreEqual("My plain text", tag.Title);
Assert.AreEqual(Color.Magenta.ToArgb(), tag.Color.ToArgb());
Assert.AreEqual("MyPlainTextSDT", tag.Tag);
Assert.That(tag.Id, Is.Positive);
Assert.AreEqual("Arial", tag.ContentsFont.Name);
Assert.AreEqual("Arial Black", tag.EndCharacterFont.Name);
Assert.True(tag.Multiline);
}
[TestCase(false)]
[TestCase(true)]
public void IsTemporary(bool isTemporary)
{
//ExStart
//ExFor:StructuredDocumentTag.IsTemporary
//ExSummary:Shows how to make single-use controls.
Document doc = new Document();
// Insert a plain text structured document tag,
// which will act as a plain text form that the user may enter text into.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// Set the "IsTemporary" property to "true" to make the structured document tag disappear and
// assimilate its contents into the document after the user edits it once in Microsoft Word.
// Set the "IsTemporary" property to "false" to allow the user to edit the contents
// of the structured document tag any number of times.
tag.IsTemporary = isTemporary;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Please enter text: ");
builder.InsertNode(tag);
// Insert another structured document tag in the form of a check box and set its default state to "checked".
tag = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
tag.Checked = true;
// Set the "IsTemporary" property to "true" to make the check box become a symbol
// once the user clicks on it in Microsoft Word.
// Set the "IsTemporary" property to "false" to allow the user to click on the check box any number of times.
tag.IsTemporary = isTemporary;
builder.Write("\nPlease click the check box: ");
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.IsTemporary.docx");
Assert.AreEqual(2,
doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Count(sdt => ((StructuredDocumentTag)sdt).IsTemporary == isTemporary));
}
[TestCase(false)]
[TestCase(true)]
public void PlaceholderBuildingBlock(bool isShowingPlaceholderText)
{
//ExStart
//ExFor:StructuredDocumentTag.IsShowingPlaceholderText
//ExFor:StructuredDocumentTag.Placeholder
//ExFor:StructuredDocumentTag.PlaceholderName
//ExSummary:Shows how to use a building block's contents as a custom placeholder text for a structured document tag.
Document doc = new Document();
// Insert a plain text structured document tag of the "PlainText" type, which will function as a text box.
// The contents that it will display by default are a "Click here to enter text." prompt.
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Inline);
// We can get the tag to display the contents of a building block instead of the default text.
// First, add a building block with contents to the glossary document.
GlossaryDocument glossaryDoc = doc.GlossaryDocument;
BuildingBlock substituteBlock = new BuildingBlock(glossaryDoc);
substituteBlock.Name = "Custom Placeholder";
substituteBlock.AppendChild(new Section(glossaryDoc));
substituteBlock.FirstSection.AppendChild(new Body(glossaryDoc));
substituteBlock.FirstSection.Body.AppendParagraph("Custom placeholder text.");
glossaryDoc.AppendChild(substituteBlock);
// Then, use the structured document tag's "PlaceholderName" property to reference that building block by name.
tag.PlaceholderName = "Custom Placeholder";
// If "PlaceholderName" refers to an existing block in the parent document's glossary document,
// we will be able to verify the building block via the "Placeholder" property.
Assert.AreEqual(substituteBlock, tag.Placeholder);
// Set the "IsShowingPlaceholderText" property to "true" to treat the
// structured document tag's current contents as placeholder text.
// This means that clicking on the text box in Microsoft Word will immediately highlight all the tag's contents.
// Set the "IsShowingPlaceholderText" property to "false" to get the
// structured document tag to treat its contents as text that a user has already entered.
// Clicking on this text in Microsoft Word will place the blinking cursor at the clicked location.
tag.IsShowingPlaceholderText = isShowingPlaceholderText;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertNode(tag);
doc.Save(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
//ExEnd
doc = new Document(ArtifactsDir + "StructuredDocumentTag.PlaceholderBuildingBlock.docx");
tag = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
substituteBlock = (BuildingBlock)doc.GlossaryDocument.GetChild(NodeType.BuildingBlock, 0, true);
Assert.AreEqual("Custom Placeholder", substituteBlock.Name);
Assert.AreEqual(isShowingPlaceholderText, tag.IsShowingPlaceholderText);
Assert.AreEqual(substituteBlock, tag.Placeholder);
Assert.AreEqual(substituteBlock.Name, tag.PlaceholderName);
}
Aspose.Words.Markup.SdtListItem : Object
Constructors :
public SdtListItem(String displayText = , String value = )public SdtListItem(String value = )
Methods :
public String get_DisplayText()public String get_Value()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()