I’m loading a msg file that contain some external https url images, next I saving this with the MHtml format and loading this to word document to saved to xps.
The https images are not loaded and it seems that it taking to much time trying to resolve those images.
This is how the original message looks: image.png (39.3 KB)
And this is the Xps conversion: image.png (28.0 KB)
Thanks for your inquiry. We investigated the issue and found that the images are not displaying due to Server permissions where the images are hosted. Please check error image for your reference. https://imgur.com/a/Kvzt6Ju.
Please use the following workaround and provide network credentials where the images are hosted. Hope, this helps.
using(MailMessage mailMessage = MailMessage.Load(@ "..\..\MessageFile.msg")) {
using(MemoryStream mhtmlDocumentStream = new MemoryStream()) {
var saveOptions = SaveOptions.CreateSaveOptions(MailMessageSaveType.MHtmlFormat);
mailMessage.Save(mhtmlDocumentStream, saveOptions);
ImageLoadingWithCredentialsHandler imageLoadingWithCredentialsHandler = new ImageLoadingWithCredentialsHandler();
Aspose.Words.LoadOptions loadOption = new Aspose.Words.LoadOptions();
loadOption.ResourceLoadingCallback = imageLoadingWithCredentialsHandler;
var wordDocument = new Aspose.Words.Document(mhtmlDocumentStream, loadOption);
using(MemoryStream outputDocumentStream = new MemoryStream()) {
wordDocument.Save("D:\\Temp\\NewDocument.xps", global::Aspose.Words.SaveFormat.Xps);
}
}
}
public class ImageLoadingWithCredentialsHandler: IResourceLoadingCallback {
public ImageLoadingWithCredentialsHandler() {
mWebClient = new WebClient();
}
public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args) {
if (args.ResourceType == ResourceType.Image) {
Uri uri = new Uri(args.Uri);
if (uri.Host == "www.aspose.com")
mWebClient.Credentials = new NetworkCredential("User1", "akjdlsfkjs");
else
mWebClient.Credentials = new NetworkCredential("SomeOtherUserID", "wiurlnlvs");
// Download the bytes from the location referenced by the URI.
byte[] imageBytes = mWebClient.DownloadData(args.Uri);
args.SetData(imageBytes);
return ResourceLoadingAction.UserProvided;
} else
{
return ResourceLoadingAction.Default;
}
}
private WebClient mWebClient;
}
thank you for your reply. That’s true some images needs some permissions but that is not the case for all of them. You can open some the images in the browser but still no included in the Xps.
We have checked all available images in your provided document and found that all images are pointing to same server. That is why they are not displayed in XPS. Please check included images URL for your reference.
App Store - https://www.homedepot.ca/content/dam/homedepot/images/corporate-affairs/mobile-app/app-store-badge-179x60.jpg Google Play Store - https://www.homedepot.ca/content/dam/homedepot/images/corporate-affairs/mobile-app/google-play-badge-199x60.jpg Facebook - https://www.homedepot.ca/is/image/content/dam/thdc/social/facebook.png Twitter - https://www.homedepot.ca/is/image/content/dam/thdc/social/twitter.png Pinterest - https://www.homedepot.ca/is/image/content/dam/thdc/social/pinterest.png Youtube - https://www.homedepot.ca/is/image/content/dam/thdc/social/youtube.png Linkedin - https://www.homedepot.ca/is/image/content/dam/thdc/social/linkedin.png Logo - http://homedepot.ca/content/dam/images/emails/hdr_logo_en.gif
ok, This means that this is not supported? The issue is that those images are public and visible (just click any of those links) but the conversion are NOT including them.
It seems that the following domain www.homedepot.ca is only available for specific region/area and not available globally due to that it shows “Access Denied” message. That is the reason images are not importing to Aspose.Words Document Object Model (DOM) and not displayed in XPS.
Well we do have access to that images and the images are not being included, no sure how this images no available for other regions will affect us if we have access to them.
We investigated the scenario further. We were unable to open the images, so we used a VPN. When we tried opening the MSG file using VPN, it failed. Therefore, we created a sample MSG file using the images that opened at our end. We were able to successfully convert the MSG file by using the code snippet that you shared. We have attached the sample MSG and XPS files for your convenience. Please test using the attached MSG file and share your feedback with us.