Download Image Bytes, Save to PNG & Insert HTML String in Word Document using C# .NET

Hi,
I am facing issue with Inserting Image to Document using HTML as input using Aspose Word C#. I have replaced the src with full path and still it didn’t worked. The path of the image requires authentication.
I have tried with other image with has no authentication required and it worked.
Note: I can’t share the URL and sample project to reproduce the issue as it is the secured URL.
Following is the steps I have followed:

  1. Read the Jason Object from the Web API using authentication and from the object reading the HTML.
  2. Updating the Image SRC path to full URL path
  3. Passing HTML to builder.InsertHtml((string)args.FieldValue, true);
  4. Generating the document and saving it.
    The above 4 steps are run under the HTTP connection open. After 4 step the HTTP connection of Web api is closed.
    Output: The content of HTML is generating correctly in the document and only issue is with the Image and it is generating Cross Mark.

Is there any other way passing the authentication at the time of generation of document to read the Image?
Thanks,
Vasu

@Vasudeva.dammalapati,
The HtmlLoadOptions.ResourceLoadingCallback property can be used to pass the needed credentials. A class implementing IResourceLoadingCallback is used to control how resources such as images or CSS are handled when they need to be downloaded from an external source i.e a network or internet. Inside IResourceLoadingCallback.ResourceLoading method, you first need to connect through proxy to fetch data and then pass data to the ResourceLoadingArgs.SetData method.

Code.zip (850 Bytes)
Hi,

Thanks for providing the path to Insert Image in the document. I have tried the same way and able to connect to the site by passing Network Credentials and able to load the bytes in args.SetData(imageBytes);

Until this point the application is working fine and I am able to see the count of images using doc.GetChildNodes(NodeType.Shape, true).Count. But after saving the document the image is not displaying in the document. For reference I am attaching the output document and C# Console Project.

Note: I have not shared the correct URL in the Images.html As I am working on the retrieving the image from Confluence Site of my company which is a secured one.

Is there any step I am missing?

Thanks,

VasuOutputDocument.zip (16.3 KB)

@Vasudeva.dammalapati,

Please try to save image byte array to local disk to see if you are actually retrieving/downloading the correct image bytes/stream e.g. using File.WriteAllBytes("C:\\Temp\\direct.png", imageBytes)? Or you can try mWebClient.DownloadFile(args.Uri, "C:\\Temp\\direct.png"); to see if it is actually saving image file onto the local disk or not?