Can additional code be added to mimic SautinSoft's PDF redering / image preprocessing to auto resizing embedded html images?

We are moving away from SautinSoft to using Aspose.Words .Net.

We have noticed how when SautinSoft renders a PDF from html it does a preprocessing step where it will resize images to stop an image from adjusting expanding or wrapping the html.

We do not like this as a practice BUT it has caused us a problem - We do know how often this has happened with our existing data. We have LOTS of historic data, far far to much to check.

Is there additional code that can added to an Aspose Word.net implementation that will implement this SautinSoft feature?

This kind of thing we see is shown below:

I am already adding code to capture when rendering an image fails using a CustomResourceLoadingCallback.
(See below)

Is there a way of implementing a similar feature to handle the image resize problem?

I do not want to simply resize all images on a ResourceLoadingAction ResourceLoading call as I do not know whether a size is necessary and I have no context to tell me when it is necessary.

// Within the rendering method: 

LoadOptions loadOptions = new LoadOptions 
{ 
    LoadFormat = loadFormat,
    ResourceLoadingCallback = new CustomResourceLoadingCallback() // Register / implement this call back to see where handling individual resources like images can raise errors
};
Document doc = new Document(inputStream, loadOptions);
// Now the CustomResourceLoadingCallback class

using Aspose.Words.Loading;
using System;

namespace XXXX
{
    internal class CustomResourceLoadingCallback : IResourceLoadingCallback
    {
        /// <summary>
        /// ResourceLoading
        /// This method is called when Aspose.Words needs to load a resource, such as an image.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
        {
            if (args.ResourceType == ResourceType.Image)
            {
                try
                {
                    using (WebClient client = new WebClient())
                    {
                        System.Diagnostics.Debug.WriteLine(String.Format("{0} {1}", ServicePointManager.SecurityProtocol, args.Uri));
                        Byte[] imageData = client.DownloadData(args.Uri);
                        args.SetData(imageData);
                        return ResourceLoadingAction.UserProvided;
                    }
                }
                catch (Exception Ex)
                {
                    // Report when and why image loading failed
                    String errorMessage = String.Format("Failed to load image from {0}\nMessage: {1}\nStackTrace: {2}", args.Uri, Ex.Message, Ex.StackTrace);
                    System.Diagnostics.Debug.WriteLine(errorMessage);
                    AsposePDFHandler.Logger.LogError(errorMessage, Ex);
                    args.SetData(null);
                }
            }
            return ResourceLoadingAction.Default;

        }        
    }
}

Thanks

@James.Kitching

To mimic the image resizing feature from SautinSoft in your Aspose.Words .NET implementation, you can implement a custom image resizing logic within your CustomResourceLoadingCallback. This approach allows you to selectively resize images based on specific conditions rather than applying a blanket resize to all images.

Cause:

The need for custom image resizing arises when images exceed certain dimensions, which can affect the layout and performance of your document.

Solution:

You can implement a custom resizing logic in your CustomResourceLoadingCallback. Here’s a step-by-step approach:

  1. Determine Image Size Requirements: Establish criteria for when an image should be resized, such as its dimensions or other properties.

  2. Modify the ResourceLoading Method: In your CustomResourceLoadingCallback, after downloading the image data, check its dimensions. If it meets your criteria, resize it accordingly.

  3. Use the ImageSize Class: Utilize the ImageSize class to get the dimensions of the image and apply the necessary scaling.

Here’s a code snippet to illustrate this:

public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
{
    if (args.ResourceType == ResourceType.Image)
    {
        try
        {
            using (WebClient client = new WebClient())
            {
                byte[] imageData = client.DownloadData(args.Uri);
                using (MemoryStream ms = new MemoryStream(imageData))
                {
                    using (Image img = Image.FromStream(ms))
                    {
                        // Check if resizing is necessary
                        if (img.Width > 1000 || img.Height > 1000) // Example condition
                        {
                            // Resize logic
                            var newWidth = img.Width / 2; // Example resizing
                            var newHeight = img.Height / 2;

                            using (var resizedImg = new Bitmap(img, new Size(newWidth, newHeight)))
                            {
                                using (var resizedStream = new MemoryStream())
                                {
                                    resizedImg.Save(resizedStream, ImageFormat.Png);
                                    args.SetData(resizedStream.ToArray());
                                }
                            }
                        }
                        else
                        {
                            args.SetData(imageData); // Use original image if no resize needed
                        }
                    }
                }
                return ResourceLoadingAction.UserProvided;
            }
        }
        catch (Exception ex)
        {
            // Handle exceptions
            args.SetData(null);
        }
    }
    return ResourceLoadingAction.Default;
}

Next Steps:

  • Adjust the resizing conditions based on your specific requirements.
  • Test the implementation with various images to ensure it behaves as expected.
  • Monitor the performance, especially if you have a large number of images to process.

If you have any further questions or need additional assistance, feel free to ask!

@James.Kitching Could you please attach your problematic input, expected and current output documents? We will check your documents and provide you more information.

1 Like

The supplied robot solution works.
I can apply it just for html embedded images.
As an alternative though I am looking to fix the style sheets that generate the html.
Putting the fix in place in my Aspose code would mimic Sautinsoft, but it is not a great solution.
As, I believe the PDF rendering should copy what we see in a browser and not try and magically fix stuff.

Thanks

@James.Kitching It is perfect that you managed to achieve what you need. Please feel free to ask in case of any further issues. We are always glad to help you.

Please note, Aspose.Words is designed to work with MS Word documents. HTML documents and MS Word documents object models are quite different and it is not always possible to provide 100% fidelity after conversion one format to another. In most cases Aspose.Words mimics MS Word behavior when work with HTML.