Content Controls / Structured Document Tags

Content Controls (also called Structured Document Tags) allow you to customize a document. GemBox.Document supports two kinds of Content Controls – BlockContentControl is used as a block element and InlineContentControl is used as an inline element.

The ContentControlType enumeration contains a list of Content Control types that are supported through GemBox.Document's API. Each Content Control element can be customized using the ContentControlProperties class.

The following example demonstrates how to create various Content Controls and change their properties.

Screenshot of Word file with Content Controls
Word document with Content Controls
using GemBox.Document;
using GemBox.Document.CustomMarkups;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        var section = new Section(document);
        document.Sections.Add(section);

        var richText = new BlockContentControl(document, ContentControlType.RichText);
        richText.Properties.Title = "Rich Text";
        richText.Properties.LockEditing = true;
        richText.Properties.LockDeleting = true;
        richText.Blocks.Add(new Paragraph(document, "This document shows how to use Content Controls with GemBox.Document."));
        richText.Blocks.Add(new Paragraph(document, "This text is inside Rich Text Content Control that cannot be deleted or edited."));

        section.Blocks.Add(richText);

        var plainText1 = new InlineContentControl(document, ContentControlType.PlainText);
        plainText1.Inlines.Add(new Run(document, "Choose GemBox product: "));

        var comboBox = new InlineContentControl(document, ContentControlType.ComboBox);
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Document", "GBD"));
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Spreadsheet", "GBS"));
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Presentation", "GBP"));
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Pdf", "GBA"));
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Email", "GBE"));
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Spreadsheet for Java", "GBS4j"));

        var paragraph1 = new Paragraph(document);
        paragraph1.Inlines.Add(plainText1);
        paragraph1.Inlines.Add(comboBox);
        section.Blocks.Add(paragraph1);

        var plainText2 = new InlineContentControl(document, ContentControlType.PlainText);
        plainText2.Inlines.Add(new Run(document, "Full version: "));

        var checkBox = new InlineContentControl(document, ContentControlType.CheckBox);
        checkBox.Properties.CheckedSymbol.Character = 'Y';
        checkBox.Properties.UncheckedSymbol.Character = 'N';
        checkBox.Inlines.Add(new Run(document, "N"));

        var paragraph2 = new Paragraph(document);
        paragraph2.Inlines.Add(plainText2);
        paragraph2.Inlines.Add(checkBox);
        section.Blocks.Add(paragraph2);

        document.Save("ContentControl.docx");
    }
}
Imports GemBox.Document
Imports GemBox.Document.CustomMarkups

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document as new DocumentModel()

        Dim section as new Section(document)
        document.Sections.Add(section)

        Dim richText as new BlockContentControl(document, ContentControlType.RichText)
        richText.Properties.Title = "Rich Text"
        richText.Properties.LockEditing = true
        richText.Properties.LockDeleting = true
        richText.Blocks.Add(new Paragraph(document, "This document shows how to use Content Controls with GemBox.Document."))
        richText.Blocks.Add(new Paragraph(document, "This text is inside Rich Text Content Control that cannot be deleted or edited."))

        section.Blocks.Add(richText)

        Dim plainText1 as new InlineContentControl(document, ContentControlType.PlainText)
        plainText1.Inlines.Add(new Run(document, "Choose GemBox product: "))

        Dim comboBox as new InlineContentControl(document, ContentControlType.ComboBox)
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Document", "GBD"))
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Spreadsheet", "GBS"))
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Presentation", "GBP"))
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Pdf", "GBA"))
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Email", "GBE"))
        comboBox.Properties.ListItems.Add(new ContentControlListItem("GemBox.Spreadsheet for Java", "GBS4j"))

        Dim paragraph1 as new Paragraph(document)
        paragraph1.Inlines.Add(plainText1)
        paragraph1.Inlines.Add(comboBox)
        section.Blocks.Add(paragraph1)

        Dim plainText2 as new InlineContentControl(document, ContentControlType.PlainText)
        plainText2.Inlines.Add(new Run(document, "Full version: "))

        Dim checkBox as new InlineContentControl(document, ContentControlType.CheckBox)
        checkBox.Properties.CheckedSymbol.Character = "Y"C
        checkBox.Properties.UncheckedSymbol.Character = "N"C
        checkBox.Inlines.Add(new Run(document, "N"))
    
        Dim paragraph2 as new Paragraph(document)
        paragraph2.Inlines.Add(plainText2)
        paragraph2.Inlines.Add(checkBox)
        section.Blocks.Add(paragraph2)

        document.Save("ContentControl.docx")

    End Sub

End Module

Check next example or download examples from GitHub.