New to Telerik Document Processing? Download free 30-day trial

Handle WEBP Images in Word Documents using Telerik WordsProcessing

Environment

Version Product Author
2025.2.520 RadWordsProcessing Desislava Yordanova

Description

The current implementation of the RadWordsProcessing library cannot handle WEBP images when exporting Word documents. This article shows how to convert WEBP images to any of the already supported formats (like PNG), so they can be exported successfully.

Solution

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:

  1. Install the SixLabors.ImageSharp NuGet package for image conversion.

  2. Import the HTML content using HtmlFormatProvider.

  3. Iterate through the images in the document and convert the WEBP images to PNG.

  4. Replace the WEBP image sources with the converted PNG sources.

  5. Export the document to DOCX.

Here is the complete implementation:

using Telerik.Windows.Documents.Flow.Model;
using Telerik.Windows.Documents.Flow.Model.Editing;
using Telerik.Windows.Documents.Flow.FormatProviders.Docx;
using Telerik.Windows.Documents.Flow.FormatProviders.Html;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using System.IO;
using System.Diagnostics;

// Import HTML content
RadFlowDocument document = new RadFlowDocument();
DocxFormatProvider docxProvider = new DocxFormatProvider();
HtmlFormatProvider htmlFormatProvider = new HtmlFormatProvider();

string path = "sample.html";
using (Stream stream = File.Open(path, FileMode.Open, FileAccess.Read))
{
    document = htmlFormatProvider.Import(stream, new TimeSpan(0, 0, 60));
}

// Convert inline Webp images to PNG
ConvertInlineWebpImagesToPng(document);

void ConvertInlineWebpImagesToPng(RadFlowDocument document)
{
    foreach (ImageInline imageInline in document.EnumerateChildrenOfType<ImageInline>())
    {
        if (imageInline.Image.ImageSource.Extension.Equals("webp", StringComparison.InvariantCultureIgnoreCase))
        {
            using (MemoryStream webpStream = new MemoryStream(imageInline.Image.ImageSource.Data))
            {
                using var image = SixLabors.ImageSharp.Image.Load(webpStream);
                using var pngImageStream = new MemoryStream();
                image.Save(pngImageStream, new PngEncoder());
                imageInline.Image.ImageSource = new ImageSource(pngImageStream.ToArray(), "png");
            }
        }
    }
}

// Export the document to DOCX
string outputFilePath = "result.docx";
using var ms = new MemoryStream();
docxProvider.Export(document, ms, new TimeSpan(0, 0, 60));
File.WriteAllBytes(outputFilePath, ms.ToArray());

// Open the resulting DOCX
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });

See Also

In this article