Convert image to A4 PDF

Hello.

I want to be able to convert various image formats (“jpg”, “gif”, “png”, “tif”, “tiff”, “jpeg”, “bmp”) into an A4 sized PDF, at 300dpi, oriented the same as the source image (could be landscape or portrait, we don’t have control of the source of the image).

I’ve tried this (noting that 2480 and 3508 are A4 pixel dimensions for a 300dpi image:

        int portraitWidth = 2480;
        int landscapeWidth = 3508;
        int targetWidth = portraitWidth;
        com.aspose.imaging.Image sourceImage = com.aspose.imaging.Image.load(conversionDto.getInputStream());
        if ( sourceImage.getWidth() > sourceImage.getHeight() ) { // Landscape
            targetWidth = landscapeWidth;
        }
        if ( sourceImage.getWidth() > targetWidth ) {
            sourceImage.resizeWidthProportionally(targetWidth);
        }
        PdfOptions imageOptions = new PdfOptions();
        imageOptions.setUseOriginalImageSize(false);
        sourceImage.save(conversionDto.getOutputStream(), imageOptions);

ConversionDto holds an inputstream of the source file. We successfully use this method converting word docs and excel spreadsheets to pdf using other aspose libraries.

I get an output file, but it is huge, and not constrained to A4 sizing.

How do I tell the library to make the PDF A4 size instead of it being driven by the size of the image?

Actually I may have found what I needed.

When I looked at the PdfOptions.setPageSize, it took a SizeF. Unfortunately it wasn’t clear from the documentation what units the float parameters represented.

So I used pixels (portraitWidth and landscapeWidth), which are the pixels for a short or long A4 at 300 dpi.

I stumbled elsewhere in the pdf library forums, that the page size is in points. So I can use 842 and 595 points for the page size long and short sides, and the result comes out more or less how I want, albeit still close to the original image size in MB.

I looked at com.aspose.imaging.Image.resizeWidthProportionally() but that takes an int, I don’t know what the int is for and since the other sizing method takes a float, must assume it has different meaning

Ah. Works for a photo off a phone, but not with, for example, a long skinny screenshot.

So my question remains, how to convert an image (jpg, tiff, png etc) into a PDF for an A4 page when printed, maintaining it’s aspect ratio whatever it it (3:2, square, random).

Current code sample is:

        com.aspose.imaging.Image sourceImage = com.aspose.imaging.Image.load(conversionDto.getInputStream());
        SizeF pageSize;
        if ( sourceImage.getWidth() > sourceImage.getHeight() ) { // Landscape
            pageSize = new SizeF(842f, 595f );
        } else {
            pageSize = new SizeF(595f, 842f);
        }
        PdfOptions imageOptions = new PdfOptions();
        imageOptions.setPageSize(pageSize);
        imageOptions.setUseOriginalImageSize(true);
        sourceImage.save(conversionDto.getOutputStream(), imageOptions);
        sourceImage.close();
        sourceImage.dispose();

Thanks in advance

Andrew

Hello, @rouppe ,
Here is a code example:

var imagePath = @"input.png";
var outputPath = inputPath + ".pdf";

try (var image = Image.Load(imagePath))
{
    var widthIsGreater = image.Width > image.Height;
    var a4Size = widthIsGreater ? new SizeF(842f, 595f) : new SizeF(596f, 842f);

    var newImageSize = image.Size;
    if (widthIsGreater) // map image size to A4 size as a proportion (image width / image height = A4 width / A4 height)
    {
        newImageSize.Width = (int)(newImageSize.Height * a4Size.Width / a4Size.Height);
    }
    else
    {
        newImageSize.Height = (int)(newImageSize.Width * a4Size.Height / a4Size.Width);
    }

    image.Resize(newImageSize.Width, newImageSize.Height, ResizeType.CenterToCenter);
    image.Save(outputPath);
}

Thanks for that, but it hasn’t worked…
Archive.zip (39.2 KB)

ScreenShotTest.tiff is a random snip I took off my screen
ScreenShotTest.pdf is the result. It has been cropped

I also believe that the dimension returned from newImageSize.Width is in different units to a4Size.Width (pixels vs points?)

We have appeared to achieved the goal without the imaging library and using the aspose-pdf libraries alone.

Thanks for your assistance.