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>
|
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); |
Hi Lokesh Baskar
Works fine!, thanks for your support!!!
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!
|
<p style="text-align:center;">
<img src= 'https://www.w3schools.com/images/w3schools_green.jpg' />
</p> |