Converting large images

Hey,

We are currently developing a new application. During the development process we are using the trail version of Aspose.Words.

We now ran into an issue converting very large images.
Using the code found here: https://docs.aspose.com/words/net/convert-a-document/

This works perfectly fine, untill we try to convert larger images.
Today I tried to convert a 4 MB JPG and it just returned an empty PDF.
Or not entirely empty. The PDF has the same size as the original image, its about 3MB large but its just completly white.
When i tried it with a 2MB JPG it worked fine.

So i’m wondering if this is an issue with the codeblock or aspose words. Or is this maybe because we are currently still testing with the trailversion and he truncated the image?

I have added the image in attachements so you can try to convert it, to see if it does work with the retail version.

Thanks in advance
Niels

Hello
Thanks for your request. The problem occurs because the size of image (and page) is bigger than max allowed size of MS Word page. Please try using the following code to workaround this problem:

public static void ConvertImageToPdf(string inputFileName, string outputFileName)
{
    // Create Aspose.Words.Document and DocumentBuilder.
    // The builder makes it simple to add content to the document.
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    // Read the image from file, ensure it is disposed.
    using (Image image = Image.FromFile(inputFileName))
    {
        // Get the number of frames in the image.
        int framesCount = image.GetFrameCount(FrameDimension.Page);
        // Loop through all frames.
        for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
        {
            // Insert a section break before each new page, in case of a multi-frame TIFF.
            if (frameIdx != 0)
                builder.InsertBreak(BreakType.SectionBreakNewPage);
            // Select active frame.
            image.SelectActiveFrame(FrameDimension.Page, frameIdx);
            // Max page size
            const double maxPageHeight = 1584;
            const double maxPageWidth = 1584;
            double currentImageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);
            double currentImageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
            if (currentImageWidth >= maxPageWidth || currentImageHeight >= maxPageHeight)
            {
                // Get max image size.
                CalculateImageSize(image, maxPageHeight, maxPageWidth, out currentImageHeight, out currentImageWidth);
            }
            // We want the size of the page to be the same as the size of the image.
            // Convert pixels to points to size the page to the actual image size.
            PageSetup ps = builder.PageSetup;
            ps.PageWidth = currentImageWidth;
            ps.PageHeight = currentImageHeight;
            // Insert the image into the document and position it at the top left corner of the page.

            builder.InsertImage(image,
            RelativeHorizontalPosition.Page,
            0,
            RelativeVerticalPosition.Page,
            0,
            ps.PageWidth,
            ps.PageHeight,
            WrapType.None);
        }
    }
    // Save the document to PDF.
    doc.Save(outputFileName);
}
/// Calculates size of Image
///
/// Original image
/// Height of container where image should be inserted.
/// Width of container where image should be inserted.
/// Height of the image
/// Width of the image
public static void CalculateImageSize(Image img, double containerHeight, double containerWidth, out double targetHeight, out double targetWidth)
{
    // Calculate width and height
    targetHeight = containerHeight;
    targetWidth = containerWidth;
    // Get size of an image
    double imgHeight = ConvertUtil.PixelToPoint(img.Height);
    double imgWidth = ConvertUtil.PixelToPoint(img.Width);
    if (imgHeight <targetHeight && imgWidth <targetWidth)
    {
        targetHeight = imgHeight;
        targetWidth = imgWidth;
    }
    else
    {
        // Calculate size of an image in the document
        double ratioWidth = imgWidth / targetWidth;
        double ratioHeight = imgHeight / targetHeight;
        if (ratioWidth> ratioHeight)
            targetHeight = (targetHeight * (ratioHeight / ratioWidth));
        else
            targetWidth = (targetWidth * (ratioWidth / ratioHeight));
    }
}

Best regards,

Hey Andrey

That did the trick

Thanks m8