Skip to content

Commit 40e55a8

Browse files
Merge pull request #437 from telerik/new-kb-change-checkbox-state-radwordsprocessing-dcf640fb13d44a618710b5180343515a
Added new kb article change-checkbox-state-radwordsprocessing
2 parents 1e94819 + 7f82c75 commit 40e55a8

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: Changing Checkbox State in Word Templates with RadWordsProcessing
3+
description: Learn how to programmatically change the state of a checkbox in a Word document template using RadWordsProcessing.
4+
type: how-to
5+
page_title: How to Programmatically Modify Checkbox States in Word Document Templates Using RadWordsProcessing
6+
slug: change-checkbox-state-radwordsprocessing
7+
tags: radwordsprocessing, document processing, checkbox, word, template
8+
res_type: kb
9+
ticketid: 1656247
10+
---
11+
12+
## Environment
13+
14+
|Product Version|Product|Author|
15+
|----|----|----|
16+
|2024.2.426|RadWordsProcessing|[Yoan Karamanov](https://www.telerik.com/blogs/author/yoan-karamanov)|
17+
18+
## Description
19+
20+
When working with Word templates that include checkboxes as content controls, you may need to change the state of a checkbox based on certain conditions. This KB article provides a method to programmatically check or uncheck a checkbox using the RadWordsProcessing library.
21+
22+
This KB article also answers the following questions:
23+
- How can I programmatically check a checkbox in a Word document?
24+
- How do I modify the state of a checkbox in a Word template?
25+
- What is the method to change checkbox states in Word documents using C#?
26+
27+
## Solution
28+
29+
To change the state of a checkbox in a Word document, follow these steps:
30+
31+
1. Identify the content control that represents the checkbox.
32+
2. Change the checkbox's state to either checked or unchecked.
33+
3. Update the visual representation of the checkbox accordingly.
34+
35+
Below is a method that demonstrates how to achieve this:
36+
37+
```csharp
38+
private static void ChangeCheckboxState(SdtRangeStart sdt)
39+
{
40+
if (sdt == null || sdt.SdtProperties.Type != SdtType.CheckBox)
41+
{
42+
return;
43+
}
44+
45+
// Get the paragraph that is parent of the checkbox
46+
Paragraph paragraph = sdt.Paragraph;
47+
48+
// Get the index of the SDT start in the paragraph's child collection
49+
int index = paragraph.Inlines.IndexOf(sdt);
50+
51+
// Get the Run that represents the checkbox' value
52+
Run checkBoxValue = paragraph.Inlines[index + 1] as Run;
53+
CheckBoxProperties properties = (CheckBoxProperties)sdt.SdtProperties;
54+
if (!properties.Checked.HasValue || !properties.Checked.Value)
55+
{
56+
properties.Checked = true;
57+
58+
if (checkBoxValue != null)
59+
{
60+
checkBoxValue.Properties.FontFamily.LocalValue = new ThemableFontFamily(properties.CheckedState.Font);
61+
checkBoxValue.Text = ((char)properties.CheckedState.CharacterCode).ToString();
62+
}
63+
}
64+
else
65+
{
66+
properties.Checked = false;
67+
68+
if (checkBoxValue != null)
69+
{
70+
checkBoxValue.Properties.FontFamily.LocalValue = new ThemableFontFamily(properties.UncheckedState.Font);
71+
checkBoxValue.Text = ((char)properties.UncheckedState.CharacterCode).ToString();
72+
}
73+
}
74+
}
75+
```
76+
77+
To apply this method, iterate through the content controls in your document, and call `ChangeCheckboxState` for each checkbox you wish to modify. Alternatively, get the first SdtRangeStart and update its state:
78+
79+
```csharp
80+
SdtRangeStart stdStart = document.EnumerateChildrenOfType<SdtRangeStart>().First();
81+
ChangeCheckboxState(stdStart);
82+
```
83+
84+
85+
## Notes
86+
87+
- Ensure that you have identified the correct content control by checking its tag or other properties.
88+
- The visual representation of the checkbox is determined by the font family and character code specified in the `CheckedState` and `UncheckedState` of the `CheckBoxProperties`.
89+
90+
## See Also
91+
92+
- [Working with Content Controls]({%slug wordsprocessing-model-working-with-content-controls%})
93+
- [Content Controls (Structured Document Tags)]({%slug wordsprocessing-model-content-controls%})

0 commit comments

Comments
 (0)