Skip to content

Commit c689e9c

Browse files
Merge pull request #447 from telerik/new-kb-dynamic-docx-document-generation-radwordsprocessing-fb9c94ad1b7c42ceb1ff0401650dca7d
Added new kb article dynamic-docx-document-generation-radwordsprocessing
2 parents 420f910 + 927878f commit c689e9c

File tree

5 files changed

+148
-2
lines changed

5 files changed

+148
-2
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: Generating Dynamic DOCX Documents with Tables and CheckBoxes using RadWordsProcessing
3+
description: Learn how to create DOCX documents dynamically, including tables and checkboxes, using RadWordsProcessing for Document Processing.
4+
type: how-to
5+
page_title: Dynamic DOCX Document Generation with RadWordsProcessing
6+
slug: dynamic-docx-document-generation-radwordsprocessing
7+
tags: wordsprocessing, document, processing, docx, generation, dynamic, tables, checkboxes
8+
res_type: kb
9+
ticketid: 1658864
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2024.2.426| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
This article demonstrates how to create tables and checkboxes within a DOCX document programmatically.
21+
22+
![DOCX with CheckBoxes](images/docx-with-checkboxes.png)
23+
24+
## Solution
25+
26+
To generate a DOCX document dynamically, including tables and checkboxes, follow these steps:
27+
28+
1. Create a new [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}) and a [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%}).
29+
2. Use the editor to insert a [table]({%slug radwordsprocessing-model-table%}) into the document.
30+
3. Populate the table with rows and cells. For cells intended to contain checkboxes, add a checkbox using [structured document tags]({%slug wordsprocessing-model-content-controls%}) (SDT).
31+
4. Customize the checkbox appearance and checked state.
32+
5. [Export the document]({%slug radwordsprocessing-formats-and-conversion-docx-docxformatprovider%}) as a DOCX file.
33+
34+
Here is a sample code snippet demonstrating how to accomplish this:
35+
36+
```csharp
37+
static void Main(string[] args)
38+
{
39+
RadFlowDocument document = new RadFlowDocument();
40+
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
41+
Table table = editor.InsertTable();
42+
43+
for (int i = 0; i < 5; i++)
44+
{
45+
TableRow row = table.Rows.AddTableRow();
46+
47+
for (int j = 0; j < 3; j++)
48+
{
49+
TableCell cell = row.Cells.AddTableCell();
50+
if (j == 2)
51+
{
52+
Run r = cell.Blocks.AddParagraph().Inlines.AddRun("Yes/No ");
53+
SdtCheckBoxState checkedBoxState = new SdtCheckBoxState
54+
{
55+
Font = new FontFamily("MS Gothic"),
56+
CharacterCode = 0x2611
57+
};
58+
59+
SdtCheckBoxState uncheckedBoxState = new SdtCheckBoxState
60+
{
61+
Font = new FontFamily("MS Gothic"),
62+
CharacterCode = 0x2610
63+
};
64+
65+
CheckBoxProperties checkBoxProperties = new CheckBoxProperties
66+
{
67+
CheckedState = checkedBoxState,
68+
UncheckedState = uncheckedBoxState,
69+
Checked = i % 2 == 0
70+
};
71+
72+
editor.MoveToInlineEnd(r);
73+
SdtRangeStart sdt = editor.InsertStructuredDocumentTag(checkBoxProperties);
74+
editor.MoveToInlineEnd(sdt);
75+
Run runWithCheckBox = editor.InsertText(((char)(checkBoxProperties.Checked ? checkBoxProperties.CheckedState.CharacterCode : checkBoxProperties.UncheckedState.CharacterCode)).ToString());
76+
runWithCheckBox.Properties.FontFamily.LocalValue = new ThemableFontFamily(checkBoxProperties.CheckedState.Font);
77+
editor.MoveToInlineEnd(sdt.End);
78+
}
79+
else
80+
{
81+
cell.Blocks.AddParagraph().Inlines.AddRun(string.Format("Cell {0}, {1}", i, j));
82+
}
83+
cell.PreferredWidth = new TableWidthUnit(150);
84+
}
85+
}
86+
87+
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
88+
string outputFilePath = "output.docx";
89+
File.Delete(outputFilePath);
90+
using (Stream output = File.OpenWrite(outputFilePath))
91+
{
92+
provider.Export(document, output);
93+
}
94+
95+
Process.Start(new ProcessStartInfo { FileName = outputFilePath, UseShellExecute = true });
96+
}
97+
```
98+
99+
100+
For more examples and details on working with content controls and other features of RadWordsProcessing, refer to the [SDK examples](https://github.com/telerik/document-processing-sdk/blob/master/WordsProcessing/ContentControls/DocumentGenerator.cs).
101+
102+
## See Also
103+
104+
- [RadWordsProcessing Overview]({%slug radwordsprocessing-overview%})
105+
- [Working with Tables in RadWordsProcessing]({%slug radwordsprocessing-model-table%})
106+
- [Content Controls in RadWordsProcessing]({%slug wordsprocessing-model-content-controls%})
107+
9.17 KB
Loading

libraries/radwordsprocessing/model/content-controls/content-controls.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,55 @@ The __CheckBox__ content control exposes two properties __CheckedState__ and __U
7878

7979
SdtCheckBoxState checkedBoxState = new SdtCheckBoxState();
8080
checkedBoxState.Font = new FontFamily("Arial");
81-
checkedBoxState.CharacterCode = 0040;
81+
checkedBoxState.CharacterCode = 0x0040;
8282

8383
SdtCheckBoxState uncheckedBoxState = new SdtCheckBoxState();
8484
uncheckedBoxState.Font = new FontFamily("Arial");
85-
uncheckedBoxState.CharacterCode = 0024;
85+
uncheckedBoxState.CharacterCode = 0x0024;
8686

8787
CheckBoxProperties properties = new CheckBoxProperties();
8888
properties.CheckedState = checkedBoxState;
8989
properties.UncheckedState = uncheckedBoxState;
9090
properties.Checked = true;
9191
{{endregion}}
9292

93+
The toggle states can be visualized with any characters specified in the properties. The following example demonstrates a complete code snippet how to insert toggled/untoggled checkboxes:
94+
95+
```csharp
96+
RadFlowDocument document = new RadFlowDocument();
97+
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
98+
99+
//define the characters which will visually indicate the toggle states
100+
SdtCheckBoxState checkedBoxState = new SdtCheckBoxState();
101+
checkedBoxState.Font = new FontFamily("Arial");
102+
checkedBoxState.CharacterCode = 0x0040;
103+
104+
SdtCheckBoxState uncheckedBoxState = new SdtCheckBoxState();
105+
uncheckedBoxState.Font = new FontFamily("Arial");
106+
uncheckedBoxState.CharacterCode = 0x0024;
107+
108+
CheckBoxProperties checkBoxProperties = new CheckBoxProperties();
109+
checkBoxProperties.CheckedState = checkedBoxState;
110+
checkBoxProperties.UncheckedState = uncheckedBoxState;
111+
checkBoxProperties.Checked = true;
112+
113+
Run checkedRun = editor.InsertText("Checked: ");
114+
editor.MoveToInlineEnd(checkedRun);
115+
SdtRangeStart sdt = editor.InsertStructuredDocumentTag(checkBoxProperties);
116+
editor.MoveToInlineEnd(sdt);
117+
Run runWithCheckBox = editor.InsertText(((char)checkBoxProperties.CheckedState.CharacterCode).ToString());
118+
runWithCheckBox.Properties.FontFamily.LocalValue = new ThemableFontFamily(checkBoxProperties.CheckedState.Font);
119+
editor.MoveToInlineEnd(sdt.End);
120+
121+
Run uncheckedRun = editor.InsertText(Environment.NewLine+ "UnChecked: ");
122+
editor.MoveToInlineEnd(uncheckedRun);
123+
sdt = editor.InsertStructuredDocumentTag(checkBoxProperties);
124+
editor.MoveToInlineEnd(sdt);
125+
runWithCheckBox = editor.InsertText(((char)checkBoxProperties.UncheckedState.CharacterCode).ToString());
126+
```
127+
128+
![Insert CheckBoxes](images/insert-checkboxes.gif)
129+
93130
### ComboBox and DropDownList
94131

95132
The __ComboBox__ and __DropDownList__ provide the user with options to choose from. The only difference is that when using ComboBox you can add a value that is not defined in the list.
@@ -169,5 +206,6 @@ The __Text__ content control allows you to enter plain text. The text content co
169206
# See Also
170207

171208
* [Working with Content Controls]({%slug wordsprocessing-model-working-with-content-controls%})
209+
* [Generating Dynamic DOCX Documents with Tables and CheckBoxes using RadWordsProcessing]({%slug dynamic-docx-document-generation-radwordsprocessing%})
172210

173211

65 KB
Loading

libraries/radwordsprocessing/model/table.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,4 @@ __Example 5__ demonstrates how to add a __Table__ with 5 rows and 10 columns to
178178
* [TableRow]({%slug radwordsprocessing-model-tablerow%})
179179
* [TableCell]({%slug radwordsprocessing-model-tablecell%})
180180
* [Style Properties]({%slug radwordsprocessing-concepts-style-properties%})
181+
* [Generating Dynamic DOCX Documents with Tables and CheckBoxes using RadWordsProcessing]({%slug dynamic-docx-document-generation-radwordsprocessing%})

0 commit comments

Comments
 (0)