Completed
Last Updated: 22 Jan 2020 10:49 by ADMIN
Release LIB 2020.1.127 (01/27/2020)
ADMIN
Boby
Created on: 09 Jun 2017 06:49
Category: WordsProcessing
Type: Bug Report
2
WordsProcessing: WebException on export to docx of document imported from HTML and containing image which src is invalid or relative URL
If HTML document is imported, and it contains image with invalid URL, then the image is imported with this URL in the document model. On subsequent export to Docx, the library tries to download the image data, which throws WebException. Instead, the image should be replaced with generic 'error' image.

Workaround: Manually test the image URL for correctness on HTML import, and replace the data:

        static void Main(string[] args)
        {
            HtmlFormatProvider htmlFormatProvider = new HtmlFormatProvider();
            htmlFormatProvider.ImportSettings.LoadFromUri += (sender, e) =>
            {
                if (!IsValid(e.Uri))
                {
                    e.SetData(File.ReadAllBytes("no-image.png"));
                }
            };
        }

        private static bool IsValid(string uri)
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    client.DownloadData(uri);
                }
            }
            catch (WebException)
            {
                return false;
            }

            return true;
        }

The same issue appears when exporting to PDF.
0 comments