diff --git a/knowledge-base/images/input-flow-content.png b/knowledge-base/images/input-flow-content.png new file mode 100644 index 00000000..a37cfb1f Binary files /dev/null and b/knowledge-base/images/input-flow-content.png differ diff --git a/knowledge-base/images/output-flow-content.png b/knowledge-base/images/output-flow-content.png new file mode 100644 index 00000000..3d3daba2 Binary files /dev/null and b/knowledge-base/images/output-flow-content.png differ diff --git a/knowledge-base/inserting-html-and-styling-radwordsprocessing.md b/knowledge-base/inserting-html-and-styling-radwordsprocessing.md new file mode 100644 index 00000000..6609f61a --- /dev/null +++ b/knowledge-base/inserting-html-and-styling-radwordsprocessing.md @@ -0,0 +1,83 @@ +--- +title: Inserting Formatted HTML content in another RadFlowDocument using WordsProcessing +description: Learn how to insert formatted HTML text in specific locations within a RadFlowDocument and preserve the styling using Telerik WordsProcessing. +type: how-to +page_title: How to Insert HTML Content in a Word Document while Preserving its Styles and Formatting +meta_title: How to Insert HTML Content in a Word Document while Preserving its Styles and Formatting +slug: inserting-html-and-styling-radwordsprocessing +tags: word, processing, telerik, document, html, styling, insert, docx, flow, words, formatting +res_type: kb +ticketid: 1698628 +--- + + + +## Environment +| Version | Product | Author | +| ---- | ---- | ---- | +| 2025.3.806| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| + +## Description + +Learn how to insert HTML content at specific locations within a [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}) using Telerik [WordsProcessing]({%slug radwordsprocessing-overview%}). + +|Input Content|Output Content| +|----|----| +|![Input Content ><](images/input-flow-content.png) | ![Input Content ><](images/output-flow-content.png) | + +## Solution + +To insert HTML content at specific locations in a RadFlowDocument, follow these steps: + +1. Use the [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}) to import HTML content into a [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}). + +1. Use the [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%}) to insert the imported document (step 1) into a specific location in your target document. + +Example: + +```csharp + RadFlowDocument originalDocument = new RadFlowDocument(); + DocxFormatProvider docxProvider = new DocxFormatProvider(); + originalDocument = docxProvider.Import(File.ReadAllBytes("original.docx"), TimeSpan.FromSeconds(10)); + + HtmlFormatProvider htmlProvider = new HtmlFormatProvider(); + RadFlowDocument htmlDocument = htmlProvider.Import(File.ReadAllText("content.html"), TimeSpan.FromSeconds(10)); + + // Get paragraphs from the imported document + var importedParagraphs = htmlDocument.EnumerateChildrenOfType().ToList(); + + // Move editor to the start of the target paragraph + RadFlowDocumentEditor editor = new RadFlowDocumentEditor(originalDocument); + + var tableCells = originalDocument.EnumerateChildrenOfType().ToList(); + TableCell cell = tableCells[3] as TableCell; + editor.MoveToParagraphStart(cell.Blocks.First() as Paragraph); + + editor.InsertDocument(htmlDocument); + + string outputFilePath = "output.docx"; + File.Delete(outputFilePath); + using (Stream output = File.OpenWrite(outputFilePath)) + { + docxProvider.Export(originalDocument, output, TimeSpan.FromSeconds(10)); + } + + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); +``` + + +### Additional Notes + +- To target specific locations in the document, use the [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%}) to navigate to the desired position. +- Ensure the original document and imported HTML content are compatible in terms of styles and formatting. + +## See Also + +- [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}) +- [Insert Documents]({%slug radwordsprocessing-editing-insert-documents%}) +- [RadFlowDocumentEditor]({%slug radwordsprocessing-editing-radflowdocumenteditor%}) diff --git a/libraries/radwordsprocessing/editing/insert-documents.md b/libraries/radwordsprocessing/editing/insert-documents.md index f99d746b..b83f0aec 100644 --- a/libraries/radwordsprocessing/editing/insert-documents.md +++ b/libraries/radwordsprocessing/editing/insert-documents.md @@ -95,3 +95,4 @@ You could merge documents at a specific position using the InsertDocument() meth * [Clone and Merge]({%slug radwordsprocessing-editing-clone-and-merge%}) * [Section]({%slug radwordsprocessing-model-section%}) * [Paragraph]({%slug radwordsprocessing-model-paragraph%}) +* [Inserting Formatted HTML content in another RadFlowDocument using WordsProcessing]({%slug inserting-html-and-styling-radwordsprocessing%}) diff --git a/libraries/radwordsprocessing/editing/radflowdocumenteditor.md b/libraries/radwordsprocessing/editing/radflowdocumenteditor.md index e41b8e31..8d73fa9b 100644 --- a/libraries/radwordsprocessing/editing/radflowdocumenteditor.md +++ b/libraries/radwordsprocessing/editing/radflowdocumenteditor.md @@ -337,3 +337,4 @@ The above method will delete everything between the "start" and "end" elements. * [RadFlowDocument API Reference](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Flow.Model.RadFlowDocument.html) * [Document model]({%slug radwordsprocessing-model%}) * [Find and Replace]({%slug radwordsprocessing-editing-find-and-replace%}) + * [Inserting Formatted HTML content in another RadFlowDocument using WordsProcessing]({%slug inserting-html-and-styling-radwordsprocessing%})