|
| 1 | +--- |
| 2 | +title: Handle WEBP Images in Word Documents using Telerik WordsProcessing |
| 3 | +description: Learn how to handle WEBP Images in Word documents when using Telerik WordsProcessing, by converting them to PNG. |
| 4 | +type: how-to |
| 5 | +page_title: Handle WEBP Images in Word Documents using Telerik WordsProcessing |
| 6 | +meta_title: Handle WEBP Images in Word Documents using Telerik WordsProcessing |
| 7 | +slug: convert-webp-to-png-radwordsprocessing |
| 8 | +tags: words, processing, telerik, document, image, webp, html, import, docx, export, png, convert |
| 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 | +The current implementation of the [RadWordsProcessing]({%slug radwordsprocessing-overview%}) library cannot handle `WEBP` images when exporting Word documents. This article shows how to convert `WEBP` images to any of the already [supported formats]({%slug radwordsprocessing-model-imageinline%}) (like PNG), so they can be exported successfully. |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +This solution uses an HTML document as an example. To handle its `WEBP` images by converting them to a supported format like `PNG`, follow the steps below: |
| 25 | + |
| 26 | +1. Install the `SixLabors.ImageSharp` NuGet package for image conversion. |
| 27 | + |
| 28 | +2. Import the HTML content using [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}). |
| 29 | + |
| 30 | +3. Iterate through the images in the document and convert the `WEBP` images to `PNG`. |
| 31 | + |
| 32 | +4. Replace the `WEBP` image sources with the converted `PNG` sources. |
| 33 | + |
| 34 | +5. Export the document to [DOCX]({%slug radwordsprocessing-formats-and-conversion-docx-docxformatprovider%}). |
| 35 | + |
| 36 | +Here is the complete implementation: |
| 37 | + |
| 38 | +```csharp |
| 39 | +using Telerik.Windows.Documents.Flow.Model; |
| 40 | +using Telerik.Windows.Documents.Flow.Model.Editing; |
| 41 | +using Telerik.Windows.Documents.Flow.FormatProviders.Docx; |
| 42 | +using Telerik.Windows.Documents.Flow.FormatProviders.Html; |
| 43 | +using SixLabors.ImageSharp; |
| 44 | +using SixLabors.ImageSharp.Formats.Png; |
| 45 | +using System.IO; |
| 46 | +using System.Diagnostics; |
| 47 | + |
| 48 | +// Import HTML content |
| 49 | +RadFlowDocument document = new RadFlowDocument(); |
| 50 | +DocxFormatProvider docxProvider = new DocxFormatProvider(); |
| 51 | +HtmlFormatProvider htmlFormatProvider = new HtmlFormatProvider(); |
| 52 | + |
| 53 | +string path = "sample.html"; |
| 54 | +using (Stream stream = File.Open(path, FileMode.Open, FileAccess.Read)) |
| 55 | +{ |
| 56 | + document = htmlFormatProvider.Import(stream, new TimeSpan(0, 0, 60)); |
| 57 | +} |
| 58 | + |
| 59 | +// Convert inline Webp images to PNG |
| 60 | +ConvertInlineWebpImagesToPng(document); |
| 61 | + |
| 62 | +void ConvertInlineWebpImagesToPng(RadFlowDocument document) |
| 63 | +{ |
| 64 | + foreach (ImageInline imageInline in document.EnumerateChildrenOfType<ImageInline>()) |
| 65 | + { |
| 66 | + if (imageInline.Image.ImageSource.Extension.Equals("webp", StringComparison.InvariantCultureIgnoreCase)) |
| 67 | + { |
| 68 | + using (MemoryStream webpStream = new MemoryStream(imageInline.Image.ImageSource.Data)) |
| 69 | + { |
| 70 | + using var image = SixLabors.ImageSharp.Image.Load(webpStream); |
| 71 | + using var pngImageStream = new MemoryStream(); |
| 72 | + image.Save(pngImageStream, new PngEncoder()); |
| 73 | + imageInline.Image.ImageSource = new ImageSource(pngImageStream.ToArray(), "png"); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Export the document to DOCX |
| 80 | +string outputFilePath = "result.docx"; |
| 81 | +using var ms = new MemoryStream(); |
| 82 | +docxProvider.Export(document, ms, new TimeSpan(0, 0, 60)); |
| 83 | +File.WriteAllBytes(outputFilePath, ms.ToArray()); |
| 84 | + |
| 85 | +// Open the resulting DOCX |
| 86 | +Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 87 | +``` |
| 88 | + |
| 89 | +## See Also |
| 90 | + |
| 91 | +- [Supported Image Formats]({%slug radwordsprocessing-model-imageinline%}) |
| 92 | +- [RadWordsProcessing]({%slug radwordsprocessing-overview%}) |
0 commit comments