Convert HTML to Word, event ImageNodeVisited not fired

Hi

I have the following code in my web api with net core 5, event imageNodeVisited is not fired with images,

Please help!

 public async Task<DownloadFile> DocGetWord([FromBody] RequestDocument param)

        {

            try

            {

                var html = await DGetHTML(param);

                if(html == null) return null;

                byte[] byteArray = Encoding.UTF8.GetBytes(html.HTML);

                MemoryStream stream = new MemoryStream(byteArray);

                Syncfusion.DocIO.DLS.WordDocument document = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Html, Syncfusion.DocIO.DLS.XHTMLValidationType.None);

                // Hooks the ImageNodeVisited event to open the image from a specific location

                document.HTMLImportSettings.ImageNodeVisited += new Syncfusion.DocIO.DLS.ImageNodeVisitedEventHandler(OpenImage);

                var outStreamWord = new MemoryStream();

                document.Save(outStreamWord, Syncfusion.DocIO.FormatType.Docx);

                document.Close();

....

another code....

            }

            catch (Exception ex)

            {

                return null;

            }

        }

       private static void OpenImage(object sender, ImageNodeVisitedEventArgs args)

        {

            if ((args.Uri != string.Empty) && args.Uri.StartsWith("http"))

            {

                WebClient client = new WebClient();

                //Download the image

                byte[] image = client.DownloadData(args.Uri);

                Stream stream = new MemoryStream(image);

                //Set the retrieved stream as image

                args.ImageStream = stream;

            }

        }


html string is like this:

<table>

<tr>

<td >

<img src= 'http:\\url from image.png' style='display:block;margin-left auto;margin-right:auto'/>

</td>

</tr>

</table>






4 Replies

LB Lokesh Baskar Syncfusion Team January 7, 2022 02:58 PM UTC

Hi Jose,  

From the given code snippet, you have imported the html before hooks the ImageNodeVisited event. So that the imageNodeVisited event is not triggered. Kindly refer to the below code snippet to open the html after hooks the ImageNodeVisited event.  
Syncfusion.DocIO.DLS.WordDocument document = new Syncfusion.DocIO.DLS.WordDocument(); 
 
// Hooks the ImageNodeVisited event to open the image from a specific location 
document.HTMLImportSettings.ImageNodeVisited += new Syncfusion.DocIO.DLS.ImageNodeVisitedEventHandler(OpenImage); 
 
document.Open(stream, Syncfusion.DocIO.FormatType.Html, Syncfusion.DocIO.DLS.XHTMLValidationType.None); 

Kindly try this solution and let us know whether it resolves your problem.

Please refer to the below UG documentation link. 



JL jose luis barajas January 7, 2022 04:02 PM UTC

Hi  Lokesh Baskar

Works fine!, thanks for your support!!!




JL jose luis barajas January 7, 2022 04:29 PM UTC

Hi Lokesh


Another question, 

How can I center the images from html to word, I already tried this, but not works...

string_html += $"<img src= '{url}' width='400px' style='display:block;margin-left:auto;margin-right:auto;'/>";


Thanks in advance!



LB Lokesh Baskar Syncfusion Team January 10, 2022 06:51 AM UTC

Hi Jose,   

We have modified your Html string to align the image as a center in HTML to Word conversion using DocIO. Please refer to the below highlighted code snippet.
 
<p style="text-align:center;"> 
<img src= 'https://www.w3schools.com/images/w3schools_green.jpg' /> 
</p> 

Kindly try this solution and let us know whether it resolves your problem.
 

Regards, 
Lokesh B 
 


Loader.
Up arrow icon