Free Support Forum - aspose.com

Insert checkbox inside an SDT

Hi,


I wanted to insert a checkbox inside an SDT. Below is the code how I’m doing it presently.
DocumentBuilder builder = new DocumentBuilder(doc);

IEnumerable nodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast().Where(sdt => sdt.Tag == “sdt1”);
foreach (StructuredDocumentTag node in nodes)
{
if (node.HasChildNodes)
{
builder.MoveTo(node);
builder.InsertCheckBox(“checkbox”, true, 0);
}
}

But the above code inserts the checkbox before the SDT and not within it. If I try moving the builder to the LastChild of the node and then try to insert the checkbox, it throws an exception ().
I’m attaching the source and destination document for your reference.

Thanks,
Neha
Hi Neha,

Thanks for your inquiry. Your target document contains the content control of type Checkbox. You can insert it into Document using following code example. If you want to insert checkbox (legacy form control) inside StructuredDocumentTag (content control), please move the cursor to the first node of it and insert checkbox (legacy form control).

Please let us know if you have any more queries.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToDocumentEnd();

Paragraph para = builder.InsertParagraph();

StructuredDocumentTag checkbox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);

para.AppendChild(checkbox);

doc.Save(MyDir + "Out v17.2.0.docx");

This is my code snippet that I’m using to insert the checkbox.


public static void InsertCheckboxInsideTag(ref Document doc, string TagName, bool value)
{
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag checkbox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
if(value == true)
checkbox.Checked = true;
Run run = new Run(doc, “”);

IEnumerable nodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast().Where(sdt => sdt.Tag == TagName);
foreach (StructuredDocumentTag node in nodes)
{
if (node.HasChildNodes)
{
builder.MoveTo(node.LastChild);
node.AppendChild(run);
run.ParentNode.AppendChild(checkbox);
}
}
}

This inserts an SDT ( with a checkbox ) inside the SDT ( which is already there in the document ).

Isn’t there any other way to insert only a checkbox inside the SDT ( tagged as “TagName” ) that is already present in the doc.

Thanks,
Neha
Hi Neha,

Thanks for your inquiry. In this case, you need to replace the content control of type checkbox with the existing content control. Please check following code example. Hope this helps you.

Document doc = new Document(MyDir + "Source.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

StructuredDocumentTag checkbox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Block);

checkbox.Checked = true;

IEnumerable<StructuredDocumentTag> nodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().Where(sdt => sdt.Tag == "sdt1");

foreach (StructuredDocumentTag node in nodes)

{

if (node.Level == MarkupLevel.Block)

{

node.ParentNode.InsertAfter(checkbox, node);

node.Remove();

}

}

checkbox.Tag = "sdt1";

doc.Save(MyDir + "Out v17.2.0.docx");

Hi,

Since my new question is related to this thread, I’m attaching the new question here.

The only way to add a checkbox to the document is using the “Checkbox SDT”. Now, what if I want to change the font family, size of the checkbox attached and want to make it bold? Is there a way to do that? Because if I try to do the same manually ( viz. selecting the checkbox in the doc and increase the size, it works ). But I’m not able to do the same from the code behind.

Thanks in advance,
Neha

@nsharma,

Thanks for your inquiry. Please use StructuredDocumentTag.ContentsFont property to set the font formatting to the content of SDT. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag checkbox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Block);
checkbox.Checked = true;

IEnumerable<StructuredDocumentTag> nodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().Where(sdt
=> sdt.Tag == "sdt1");

foreach (StructuredDocumentTag node in nodes)
{
    if (node.Level == MarkupLevel.Block)
    {
        node.ParentNode.InsertAfter(checkbox, node);
        node.Remove();
    }
}

checkbox.Tag = "sdt1";
checkbox.ContentsFont.Bold = true;
checkbox.ContentsFont.Name = "Arial";
doc.Save(MyDir + "17.6.docx");

Best Regards,
Tahir Manzoor

HI,

Thanks for the solution. I tried it but it doesn’t seem to work I guess. But when I open the output document, put the design mode off and then try to uncheck or check the checkbox, then it shows the properties that I set.

I’m attaching my code snippet here.

    public static void InsertCheckboxInsideTag(ref Document doc, string TagName, bool value)
    {
        DocumentBuilder builder = new DocumentBuilder(doc);
        List<StructuredDocumentTag> newCheckboxList = new List<StructuredDocumentTag>();
        IEnumerable<StructuredDocumentTag> nodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().Where(sdt => sdt.Tag == TagName);
        foreach (StructuredDocumentTag node in nodes)
        {
            StructuredDocumentTag checkbox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Block);
            checkbox.Checked = true;
            checkbox.ContentsFont.Name = "Calibri";
            checkbox.ContentsFont.Size = Convert.ToDouble(14);
            checkbox.ContentsFont.Bold = true;
            checkbox.ContentsFont.Shading.BackgroundPatternColor = Color.Silver;
            checkbox.ContentsFont.Shading.ForegroundPatternColor = Color.Black;
            if (value == true)
                checkbox.Checked = true;
            else
                checkbox.Checked = false;
            node.ParentNode.InsertAfter(checkbox, node);
            newCheckboxList.Add(checkbox);
        }
        foreach (StructuredDocumentTag node in nodes)
        {
            node.Remove();
        }
        foreach (StructuredDocumentTag node in newCheckboxList)
        {
            node.Tag = TagName;
        }
    }

Also attaching the image from the output doc for your reference.
after_checked.PNG (366 Bytes)
before.PNG (441 Bytes)
after_unchecked.PNG (192 Bytes)

Thanks,
Neha

@nsharma,

Thanks for your inquiry.

While using the latest version of Aspose.Words for .NET i.e. 17.6, we managed to reproduce this issue on our end by using the following simple code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
 
StructuredDocumentTag checkbox = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
checkbox.Checked = true;  
checkbox.Tag = "sdt1";
checkbox.ContentsFont.Bold = true;
checkbox.ContentsFont.Name = "Arial"; 
checkbox.ContentsFont.Color = Color.Red;             
builder.InsertNode(checkbox);
 
doc.Save(MyDir + "17.6.docx");

We have logged this issue in our bug tracking system. The ID of this issue is WORDSNET-15602. Your thread has also been linked to this issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best regards,
Awais Hafeez

The issues you have found earlier (filed as WORDSNET-15602) have been fixed in this Aspose.Words for .NET 19.3 update and this Aspose.Words for Java 19.3 update.