Skip to content

Commit 11ca572

Browse files
author
KB Bot
committed
Added new kb article convert-webp-to-png-radwordsprocessing
1 parent 6699b61 commit 11ca572

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: Converting WEBP Images to PNG in HTML Documents using Telerik WordsProcessing
3+
description: Learn how to handle converting .webp format images in DOCX files using Telerik WordsProcessing when importing HTML content.
4+
type: how-to
5+
page_title: Converting WEBP Images to PNG in HTML Documents using Telerik WordsProcessing
6+
meta_title: Converting WEBP Images to PNG in HTML Documents using Telerik WordsProcessing
7+
slug: convert-webp-to-png-radwordsprocessing
8+
tags: wordsprocessing, telerik-document-processing, images, webp, html-import, docx-export
9+
res_type: kb
10+
ticketid: 1695863
11+
---
12+
13+
## Environment
14+
| Version | Product | Author |
15+
| ---- | ---- | ---- |
16+
| 2025.2.520| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
I want to render `.webp` images in DOCX files using Telerik WordsProcessing. The current implementation fails to process `.webp` format images when importing HTML content and exporting the output.
21+
22+
This knowledge base article also answers the following questions:
23+
- How to convert `.webp` images to supported formats in Telerik WordsProcessing?
24+
- How to handle unsupported image formats in DOCX export using Telerik WordsProcessing?
25+
- How to process `.webp` images when importing HTML content in Telerik WordsProcessing?
26+
27+
## Solution
28+
29+
Telerik WordsProcessing does not support the `.webp` format directly. The supported image formats are listed in the [Supported Image Extensions](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/model/imageinline#supported-image-extensions). To handle `.webp` images, convert them to a supported format like `.png`. Follow the steps below:
30+
31+
1. Install the `SixLabors.ImageSharp` NuGet package for image conversion.
32+
33+
2. Import the HTML content using `HtmlFormatProvider`.
34+
35+
3. Iterate through the images in the document and convert `.webp` images to `.png` format.
36+
37+
4. Replace the `.webp` image sources with the converted `.png` sources.
38+
39+
5. Export the document to DOCX.
40+
41+
Here is the complete implementation:
42+
43+
```csharp
44+
using Telerik.Windows.Documents.Flow.Model;
45+
using Telerik.Windows.Documents.Flow.Model.Editing;
46+
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
47+
using Telerik.Windows.Documents.Flow.FormatProviders.Html;
48+
using SixLabors.ImageSharp;
49+
using SixLabors.ImageSharp.Formats.Png;
50+
using System.IO;
51+
using System.Diagnostics;
52+
53+
// Import HTML content
54+
RadFlowDocument document = new RadFlowDocument();
55+
DocxFormatProvider docxProvider = new DocxFormatProvider();
56+
HtmlFormatProvider htmlFormatProvider = new HtmlFormatProvider();
57+
58+
string path = "sample.html";
59+
using (Stream stream = File.Open(path, FileMode.Open, FileAccess.Read))
60+
{
61+
document = htmlFormatProvider.Import(stream, new TimeSpan(0, 0, 60));
62+
}
63+
64+
// Convert inline Webp images to PNG
65+
ConvertInlineWebpImagesToPng(document);
66+
67+
void ConvertInlineWebpImagesToPng(RadFlowDocument document)
68+
{
69+
foreach (ImageInline imageInline in document.EnumerateChildrenOfType<ImageInline>())
70+
{
71+
if (imageInline.Image.ImageSource.Extension.Equals("webp", StringComparison.InvariantCultureIgnoreCase))
72+
{
73+
using (MemoryStream webpStream = new MemoryStream(imageInline.Image.ImageSource.Data))
74+
{
75+
using var image = SixLabors.ImageSharp.Image.Load(webpStream);
76+
using var pngImageStream = new MemoryStream();
77+
image.Save(pngImageStream, new PngEncoder());
78+
imageInline.Image.ImageSource = new ImageSource(pngImageStream.ToArray(), "png");
79+
}
80+
}
81+
}
82+
}
83+
84+
// Export the document to DOCX
85+
string outputFilePath = "result.docx";
86+
using var ms = new MemoryStream();
87+
docxProvider.Export(document, ms, new TimeSpan(0, 0, 60));
88+
File.WriteAllBytes(outputFilePath, ms.ToArray());
89+
90+
// Open the resulting DOCX
91+
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
92+
```
93+
94+
## See Also
95+
96+
- [Supported Image Extensions](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/model/imageinline#supported-image-extensions)
97+
- [WordsProcessing: Introduce support for importing webp images](https://feedback.telerik.com/document-processing/1543913-wordsprocessing-introduce-support-for-importing-webp-images)
98+
- [RadWordsProcessing Overview](https://docs.telerik.com/devtools/document-processing/libraries/radwordsprocessing/overview)

0 commit comments

Comments
 (0)