Skip to content

Commit 6215e18

Browse files
author
KB Bot
committed
Added new kb article dynamic-docx-document-generation-radwordsprocessing
1 parent 0d0baa3 commit 6215e18

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
## Solution
23+
24+
To generate a DOCX document dynamically, including tables and checkboxes, follow these steps:
25+
26+
1. Create a new [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}) and a [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%}).
27+
2. Use the editor to insert a [table]({%slug radwordsprocessing-model-table%}) into the document.
28+
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).
29+
4. Customize the checkbox appearance and checked state.
30+
5. [Export the document]({%slug radwordsprocessing-formats-and-conversion-docx-docxformatprovider%}) as a DOCX file.
31+
32+
Here is a sample code snippet demonstrating how to accomplish this:
33+
34+
```csharp
35+
static void Main(string[] args)
36+
{
37+
RadFlowDocument document = new RadFlowDocument();
38+
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
39+
Table table = editor.InsertTable();
40+
41+
for (int i = 0; i < 5; i++)
42+
{
43+
TableRow row = table.Rows.AddTableRow();
44+
45+
for (int j = 0; j < 3; j++)
46+
{
47+
TableCell cell = row.Cells.AddTableCell();
48+
if (j == 2)
49+
{
50+
Run r = cell.Blocks.AddParagraph().Inlines.AddRun("Yes/No ");
51+
SdtCheckBoxState checkedBoxState = new SdtCheckBoxState
52+
{
53+
Font = new FontFamily("MS Gothic"),
54+
CharacterCode = 0x2611
55+
};
56+
57+
SdtCheckBoxState uncheckedBoxState = new SdtCheckBoxState
58+
{
59+
Font = new FontFamily("MS Gothic"),
60+
CharacterCode = 0x2610
61+
};
62+
63+
CheckBoxProperties checkBoxProperties = new CheckBoxProperties
64+
{
65+
CheckedState = checkedBoxState,
66+
UncheckedState = uncheckedBoxState,
67+
Checked = i % 2 == 0
68+
};
69+
70+
editor.MoveToInlineEnd(r);
71+
SdtRangeStart sdt = editor.InsertStructuredDocumentTag(checkBoxProperties);
72+
editor.MoveToInlineEnd(sdt);
73+
Run runWithCheckBox = editor.InsertText(((char)(checkBoxProperties.Checked ? checkBoxProperties.CheckedState.CharacterCode : checkBoxProperties.UncheckedState.CharacterCode)).ToString());
74+
runWithCheckBox.Properties.FontFamily.LocalValue = new ThemableFontFamily(checkBoxProperties.CheckedState.Font);
75+
editor.MoveToInlineEnd(sdt.End);
76+
}
77+
else
78+
{
79+
cell.Blocks.AddParagraph().Inlines.AddRun(string.Format("Cell {0}, {1}", i, j));
80+
}
81+
cell.PreferredWidth = new TableWidthUnit(150);
82+
}
83+
}
84+
85+
Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
86+
string outputFilePath = "output.docx";
87+
File.Delete(outputFilePath);
88+
using (Stream output = File.OpenWrite(outputFilePath))
89+
{
90+
provider.Export(document, output);
91+
}
92+
93+
Process.Start(new ProcessStartInfo { FileName = outputFilePath, UseShellExecute = true });
94+
}
95+
```
96+
97+
98+
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).
99+
100+
## See Also
101+
102+
- [RadWordsProcessing Overview]({%slug radwordsprocessing-overview%})
103+
- [Working with Tables in RadWordsProcessing]({%slug radwordsprocessing-model-table%})
104+
- [Content Controls in RadWordsProcessing]({%slug wordsprocessing-model-content-controls%})
105+

0 commit comments

Comments
 (0)