Add checkbox and picture content control to word document in C#

2019-05-29 07:31:42 Written by  support iceblue
Rate this item
(0 votes)

Besides the Combo Box, Text, Date Picker and Drop-Down List content controls, Checkbox and picture content control also are the mostly used content control in word document. Spire.Doc supports to add many kinds of content controls to the word document. This article will show you how to add checkbox and picture content control to word document by Spire.Doc for .NET.

Code snippets of how to add checkbox and picture content control:

using System;
using System.Drawing;
namespace AddCheckbox
{

    class Program
    {

        static void Main(string[] args)
        {
            //Create a new word document
            Document document = new Document();

            //Add a section to the document
            Section section = document.AddSection();

            //Add a document to the section
            Paragraph paragraph = section.AddParagraph();

            //Add checkbox content control
            StructureDocumentTagInline sdt = new StructureDocumentTagInline(document);
            paragraph = section.AddParagraph();
            sdt = new StructureDocumentTagInline(document);
            sdt.CharacterFormat.FontSize = 20;
            paragraph.ChildObjects.Add(sdt);
            sdt.SDTProperties.SDTType = SdtType.CheckBox;
            SdtCheckBox scb = new SdtCheckBox();
            sdt.SDTProperties.ControlProperties = scb;
            TextRange tr = new TextRange(document);
            tr.CharacterFormat.FontName = "MS Gothic";
            tr.CharacterFormat.FontSize = 20;
            sdt.ChildObjects.Add(tr);
            scb.Checked = true;

            sdt.SDTProperties.Alias = "CheckoBox";
            sdt.SDTProperties.Tag = "Checkbox";

            //Add picture content control
            paragraph = section.AddParagraph();
            sdt = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sdt);
            sdt.SDTProperties.ControlProperties = new SdtPicture();

            sdt.SDTProperties.Alias = "Picture";
            sdt.SDTProperties.Tag = "Picture";

            DocPicture pic = new DocPicture(document) { Width = 10, Height = 10 };
            pic.LoadImage(Image.FromFile("Logo.jpg"));
            sdt.SDTContent.ChildObjects.Add(pic);

            document.SaveToFile("Sample.docx", FileFormat.Docx2013);

        }
    }
}

Effective screenshot after adding checkbox and picture content control to word document:

Add checkbox and picture content control to word document in C#

Additional Info

  • tutorial_title:
Last modified on Tuesday, 18 June 2024 08:27