|
| 1 | +--- |
| 2 | +title: Inserting Formatted HTML content in another RadFlowDocument using WordsProcessing |
| 3 | +description: Learn how to insert formatted HTML text in specific locations within a RadFlowDocument and preserve the styling using Telerik WordsProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: How to Insert HTML Content in a Word Document while Preserving its Styles and Formatting |
| 6 | +meta_title: How to Insert HTML Content in a Word Document while Preserving its Styles and Formatting |
| 7 | +slug: inserting-html-and-styling-radwordsprocessing |
| 8 | +tags: word, processing, telerik, document, html, styling, insert, docx, flow, words, formatting |
| 9 | +res_type: kb |
| 10 | +ticketid: 1698628 |
| 11 | +--- |
| 12 | + |
| 13 | +<style> |
| 14 | +img[alt$="><"] { |
| 15 | + border: 1px solid lightgrey; |
| 16 | +} |
| 17 | + |
| 18 | +</style> |
| 19 | + |
| 20 | +## Environment |
| 21 | +| Version | Product | Author | |
| 22 | +| ---- | ---- | ---- | |
| 23 | +| 2025.3.806| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +Learn how to insert HTML content at specific locations within a [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}) using Telerik [WordsProcessing]({%slug radwordsprocessing-overview%}). |
| 28 | + |
| 29 | +|Input Content|Output Content| |
| 30 | +|----|----| |
| 31 | +| |  | |
| 32 | + |
| 33 | +## Solution |
| 34 | + |
| 35 | +To insert HTML content at specific locations in a RadFlowDocument, follow these steps: |
| 36 | + |
| 37 | +1. Use the [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}) to import HTML content into a [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}). |
| 38 | + |
| 39 | +1. Use the [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%}) to insert the imported document (step 1) into a specific location in your target document. |
| 40 | + |
| 41 | +Example: |
| 42 | + |
| 43 | +```csharp |
| 44 | + RadFlowDocument originalDocument = new RadFlowDocument(); |
| 45 | + DocxFormatProvider docxProvider = new DocxFormatProvider(); |
| 46 | + originalDocument = docxProvider.Import(File.ReadAllBytes("original.docx"), TimeSpan.FromSeconds(10)); |
| 47 | + |
| 48 | + HtmlFormatProvider htmlProvider = new HtmlFormatProvider(); |
| 49 | + RadFlowDocument htmlDocument = htmlProvider.Import(File.ReadAllText("content.html"), TimeSpan.FromSeconds(10)); |
| 50 | + |
| 51 | + // Get paragraphs from the imported document |
| 52 | + var importedParagraphs = htmlDocument.EnumerateChildrenOfType<Paragraph>().ToList(); |
| 53 | + |
| 54 | + // Move editor to the start of the target paragraph |
| 55 | + RadFlowDocumentEditor editor = new RadFlowDocumentEditor(originalDocument); |
| 56 | + |
| 57 | + var tableCells = originalDocument.EnumerateChildrenOfType<TableCell>().ToList(); |
| 58 | + TableCell cell = tableCells[3] as TableCell; |
| 59 | + editor.MoveToParagraphStart(cell.Blocks.First() as Paragraph); |
| 60 | + |
| 61 | + editor.InsertDocument(htmlDocument); |
| 62 | + |
| 63 | + string outputFilePath = "output.docx"; |
| 64 | + File.Delete(outputFilePath); |
| 65 | + using (Stream output = File.OpenWrite(outputFilePath)) |
| 66 | + { |
| 67 | + docxProvider.Export(originalDocument, output, TimeSpan.FromSeconds(10)); |
| 68 | + } |
| 69 | + |
| 70 | + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +### Additional Notes |
| 75 | + |
| 76 | +- To target specific locations in the document, use the [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%}) to navigate to the desired position. |
| 77 | +- Ensure the original document and imported HTML content are compatible in terms of styles and formatting. |
| 78 | + |
| 79 | +## See Also |
| 80 | + |
| 81 | +- [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}) |
| 82 | +- [Insert Documents]({%slug radwordsprocessing-editing-insert-documents%}) |
| 83 | +- [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%}) |
0 commit comments