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?
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).
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.