|
| 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 | + |
| 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 | + |
0 commit comments