Skip to content

Commit 6ef2aef

Browse files
authored
Merge pull request #632 from telerik/new-kb-inserting-html-and-styling-radwordsprocessing-fe2f8e3b118d479fb96384bec2b7ec02
Added new kb article inserting-html-and-styling-radwordsprocessing
2 parents 38029ca + a7edbe1 commit 6ef2aef

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed
36.3 KB
Loading
25.9 KB
Loading
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
|![Input Content ><](images/input-flow-content.png) | ![Input Content ><](images/output-flow-content.png) |
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%})

libraries/radwordsprocessing/editing/insert-documents.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ You could merge documents at a specific position using the InsertDocument() meth
9595
* [Clone and Merge]({%slug radwordsprocessing-editing-clone-and-merge%})
9696
* [Section]({%slug radwordsprocessing-model-section%})
9797
* [Paragraph]({%slug radwordsprocessing-model-paragraph%})
98+
* [Inserting Formatted HTML content in another RadFlowDocument using WordsProcessing]({%slug inserting-html-and-styling-radwordsprocessing%})

libraries/radwordsprocessing/editing/radflowdocumenteditor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,4 @@ The above method will delete everything between the "start" and "end" elements.
337337
* [RadFlowDocument API Reference](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Flow.Model.RadFlowDocument.html)
338338
* [Document model]({%slug radwordsprocessing-model%})
339339
* [Find and Replace]({%slug radwordsprocessing-editing-find-and-replace%})
340+
* [Inserting Formatted HTML content in another RadFlowDocument using WordsProcessing]({%slug inserting-html-and-styling-radwordsprocessing%})

0 commit comments

Comments
 (0)