|
| 1 | +--- |
| 2 | +title: Inserting Formatted HTML content in another RadFlowDocument |
| 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 and Preserve the Styles in RadWordsProcessing Document |
| 6 | +meta_title: How to Insert HTML and Apply Preserve the in RadWordsProcessing Document |
| 7 | +slug: inserting-html-and-styling-radwordsprocessing |
| 8 | +tags: word, processing,telerik,document,html,styling,insert, docx, flow |
| 9 | +res_type: kb |
| 10 | +ticketid: 1698628 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +| Version | Product | Author | |
| 15 | +| ---- | ---- | ---- | |
| 16 | +| 2025.3.806| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I need to insert HTML content into specific locations within a RadFlowDocument created using Telerik WordsProcessing. Additionally, I want to apply specific styles to the inserted content, such as font family, size, and weight. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to merge HTML content into a specific paragraph in Telerik WordsProcessing? |
| 24 | +- How to style a RadFlowDocument content programmatically? |
| 25 | +- How to use RadWordsProcessing to insert documents into specific table cells? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To insert HTML content into specific locations in a RadFlowDocument and apply styles, follow these steps: |
| 30 | + |
| 31 | +### Inserting HTML Content into a Specific Location |
| 32 | + |
| 33 | +Use the `HtmlFormatProvider` to import HTML content into a `RadFlowDocument`. Then, use the `RadFlowDocumentEditor` to insert the imported document into a specific location in your target document. |
| 34 | + |
| 35 | +Example: |
| 36 | + |
| 37 | +```csharp |
| 38 | +RadFlowDocument originalDocument = new RadFlowDocument(); |
| 39 | +DocxFormatProvider docxProvider = new DocxFormatProvider(); |
| 40 | +originalDocument = docxProvider.Import(File.ReadAllBytes("original.docx"), TimeSpan.FromSeconds(10)); |
| 41 | + |
| 42 | +HtmlFormatProvider htmlProvider = new HtmlFormatProvider(); |
| 43 | +RadFlowDocument htmlDocument = htmlProvider.Import(File.ReadAllText("content.html"), TimeSpan.FromSeconds(10)); |
| 44 | + |
| 45 | +RadFlowDocumentEditor editor = new RadFlowDocumentEditor(originalDocument); |
| 46 | +var tableCells = originalDocument.EnumerateChildrenOfType<TableCell>().ToList(); |
| 47 | +TableCell cell = tableCells[3] as TableCell; |
| 48 | + |
| 49 | +// Move editor to the start of the target paragraph |
| 50 | +editor.MoveToParagraphStart(cell.Blocks.First() as Paragraph); |
| 51 | + |
| 52 | +// Insert the HTML document |
| 53 | +editor.InsertDocument(htmlDocument); |
| 54 | + |
| 55 | +string outputFilePath = "output.docx"; |
| 56 | +File.Delete(outputFilePath); |
| 57 | +using (Stream output = File.OpenWrite(outputFilePath)) |
| 58 | +{ |
| 59 | + docxProvider.Export(originalDocument, output, TimeSpan.FromSeconds(10)); |
| 60 | +} |
| 61 | +Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 62 | +``` |
| 63 | + |
| 64 | +### Applying Styles to Imported Content |
| 65 | + |
| 66 | +Apply styles to the entire imported document before merging it into the target document. Use the `ThemableFontFamily`, `FontSize`, and `FontWeight` properties. |
| 67 | + |
| 68 | +Example: |
| 69 | + |
| 70 | +```csharp |
| 71 | +htmlDocument.FontFamily = new ThemableFontFamily("Arial Narrow"); |
| 72 | +htmlDocument.FontSize = UnitHelper.PointToDip(10); |
| 73 | +htmlDocument.FontWeight = FontWeights.Bold; |
| 74 | +``` |
| 75 | + |
| 76 | +### Additional Notes |
| 77 | + |
| 78 | +- To target specific locations in the document, use the `RadFlowDocumentEditor` to navigate to the desired position. |
| 79 | +- Ensure the original document and imported HTML content are compatible in terms of styles and formatting. |
| 80 | + |
| 81 | +## See Also |
| 82 | + |
| 83 | +- [Insert Documents](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/editing/insert-documents) |
| 84 | +- [RadWordsProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/overview) |
| 85 | +- [HtmlFormatProvider API Reference](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/formats/html) |
| 86 | +- [RadFlowDocumentEditor API Reference](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/editing/document-editor) |
| 87 | +--- |
0 commit comments