Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added knowledge-base/images/input-flow-content.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added knowledge-base/images/output-flow-content.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions knowledge-base/inserting-html-and-styling-radwordsprocessing.md
Original file line number Diff line number Diff line change
@@ -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
---

<style>
img[alt$="><"] {
border: 1px solid lightgrey;
}

</style>

## 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<Paragraph>().ToList();

// Move editor to the start of the target paragraph
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(originalDocument);

var tableCells = originalDocument.EnumerateChildrenOfType<TableCell>().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%})
1 change: 1 addition & 0 deletions libraries/radwordsprocessing/editing/insert-documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -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%})
Original file line number Diff line number Diff line change
Expand Up @@ -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%})