How to insert a check box in word with check/ uncheck in Dot net

Hi Team,

We have a functionality to insert bullet points in word with a check box kind of functionality. As seen below we need to have a similar functionality automated in Aspose. Is it possible to do it in Aspose ? If yes, please share a coding snippet

☐ Option 1 (should be checked)
☐ Option 2 (Unchecked)
☐ Option 3 (Checked)

Thanks,
Karthikeyan

@Karthik_Test_account You can achieve this either using form fields:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// insert checkbox formfield.
builder.InsertCheckBox("Option1", true, 10);
builder.Writeln("Option 1 (should be checked)");

builder.InsertCheckBox("Option2", false, 10);
builder.Writeln("Option 2 (Unchecked)");

builder.InsertCheckBox("Option3", true, 10);
builder.Writeln("Option 3 (Checked)");

doc.Save(@"C:\Temp\out.docx");

or using structured document tags:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// insert checkbox SDT.
StructuredDocumentTag sdt1 = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
sdt1.Checked = true;
builder.InsertNode(sdt1);
builder.Writeln("Option 1 (should be checked)");

StructuredDocumentTag sdt2 = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
sdt2.Checked = false;
builder.InsertNode(sdt2);
builder.Writeln("Option 2 (Unchecked)");

StructuredDocumentTag sdt3 = new StructuredDocumentTag(doc, SdtType.Checkbox, MarkupLevel.Inline);
sdt3.Checked = true;
builder.InsertNode(sdt3);
builder.Writeln("Option 3 (Checked)");

doc.Save(@"C:\Temp\out.docx");

This works Thank you! I also have one additional requirement the thing is we have a prefilled document where the checkbox content will have some content before and after so the same functionality need to be done in the middle of a prefilled document. Is it possible to achieve this in Aspose?

Prefilled document:
some paragraphs content

☐ Option 1 (should be checked)
☐ Option 2 (Unchecked)
☐ Option 3 (Checked)

some paragraphs content

@Karthik_Test_account Sure, you can achieve this. You can move DocumentBuuilder cursor in the document using the appropriate MoveToXXX methods. Please see Aspose.Words documentation to learn more about navigation DocumentBuilder cursor.

@alexey.noskov - Above logic works. But is there a way to change the checkbox style using form fields?
Here is the detailed reason for above question we are currently using Aspose.Word 20.3.0 (Dot Net). There is a method SetCheckedSymbol in StructuredDocumentTag to set check box style but this method (SetCheckedSymbol) is availabe only from version V21.5.0 but our system fails when I try to upgrade version so wanted to know if there is any way to set check box style with Aspose.Word V20.3.0?

@Karthik_Test_account Unfortunately, there is no other way to set checked symbol except StructuredDocumentTag.SetCheckedSymbol. Could you please specify what problems you have encountered upon upgrading to 21.5 version?

@alexey.noskov - We are using checkbox form field to insert check box but the background of that is having dark grey as seen in the screenshot. Please tell us a way to remove the dark background

Code used for Check Box insertion

builder.InsertCheckBox("Option1", true, 10);
builder.Writeln("Option 1 (should be checked)");

Screenshot of checkbox background

@Karthik_Test_account You can disable legacy form field shading using Document.ShadeFormData property.
You can also disable it in Legacy Forms toolbox on the Developer tab in MS Word:

Thank you this works

1 Like