Convert tiff to pdf in Java using aspose api

Hello team,

We have a requirement in our project to convert tiff images to pdf.I am trying to use aspose api to do the same. But the issue is image height and width for the tiff files are not constant. Also sometimes resolution of the image also varies. Because of this the some images are getting compressed. Please guide us to resolve this issue.
I am using below code snippet currently:
for(int p = 0; p < nbPages; p++){
if (p != 0)
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
BufferedImage bufferedImage = reader.read§;
double maxPageHeight = 210 * POINTS_PER_MM;
double maxPageWidth = 297 * POINTS_PER_MM;
double currentImageHeight = bufferedImage.getHeight();
double currentImageWidth = bufferedImage.getWidth();
if (currentImageWidth >= maxPageWidth || currentImageHeight >= maxPageHeight) {
double[] size = CalculateImageSize(bufferedImage, maxPageHeight, maxPageWidth, currentImageHeight, currentImageWidth);
currentImageWidth = size[0];
currentImageHeight = size[1];

				}
				PageSetup ps = builder.getPageSetup();
				ps.setPageWidth(currentImageWidth);
				ps.setPageHeight(currentImageHeight);
				Shape shape = builder.insertImage(bufferedImage,RelativeHorizontalPosition.PAGE,0,RelativeVerticalPosition.PAGE,0,ps.getPageWidth(),ps.getPageHeight(),WrapType.NONE);

public static double[] CalculateImageSize(BufferedImage img, double containerHeight, double containerWidth, double targetHeight, double targetWidth) throws Exception {
targetHeight = containerHeight;

	targetWidth = containerWidth;
	double imgHeight = img.getHeight();
	double imgWidth = img.getWidth();
	if (imgHeight < targetHeight && imgWidth < targetWidth)
	{
	targetHeight = imgHeight;
	targetWidth = imgWidth;
	}
	else
	{
		double ratioWidth = imgWidth / targetWidth;
	double ratioHeight = imgHeight / targetHeight;
	if (ratioWidth > ratioHeight)
		targetHeight = (targetHeight * (ratioHeight / ratioWidth));
	else
		targetWidth = (targetWidth * (ratioWidth / ratioHeight));
	}
	double[] size = new double[2];
	size[0] = targetWidth; //width
	size[1] = targetHeight; //height
	return(size);

}

@Priya05, You can resize your tiff file before converting it to pdf as described here Resize TIFF images via Java | products.aspose.com Also for convertion from tiff to pdf you can use sample code described here Convert TIFF to PDF via Java | products.aspose.com

Hi,

Could you please provide me with code snippet.

Thanks and regards,
Priya

@Priya05 you can use provided behind code examples for resize and convertion for your needs. Here they are:

import com.aspose.imaging.Image;
import com.aspose.imaging.ImageOptionsBase;
import com.aspose.imaging.fileformats.jpeg2000.Jpeg2000Codec;
import com.aspose.imaging.fileformats.png.PngColorType;
import com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat;
import com.aspose.imaging.imageoptions.*;


//This example demonstrates how to convert all supported file formats from one to another
String templatesFolder = "D:\\WorkDir\\";

//Formats that support both - save and load
HashMap<String, ImageOptionsBase> formatsThatSupportExportAndImport = new HashMap<String, ImageOptionsBase>();
formatsThatSupportExportAndImport.put("bmp", new BmpOptions());
formatsThatSupportExportAndImport.put("gif", new GifOptions());
formatsThatSupportExportAndImport.put("dicom", new DicomOptions());
formatsThatSupportExportAndImport.put("emf", new EmfOptions());
formatsThatSupportExportAndImport.put("jpg", new JpegOptions());
formatsThatSupportExportAndImport.put("jpeg", new JpegOptions());
formatsThatSupportExportAndImport.put("jpeg2000", new Jpeg2000Options() );
formatsThatSupportExportAndImport.put("j2k", new Jpeg2000Options() {{ setCodec(Jpeg2000Codec.J2K); }} );
formatsThatSupportExportAndImport.put("jp2", new Jpeg2000Options()  {{ setCodec(Jpeg2000Codec.Jp2); }} );
formatsThatSupportExportAndImport.put("png",new PngOptions() {{ setColorType(PngColorType.TruecolorWithAlpha); }});
formatsThatSupportExportAndImport.put("apng", new ApngOptions());
formatsThatSupportExportAndImport.put("svg", new SvgOptions());
formatsThatSupportExportAndImport.put("tiff", new TiffOptions(TiffExpectedFormat.Default));
formatsThatSupportExportAndImport.put("tif", new TiffOptions(TiffExpectedFormat.Default));
formatsThatSupportExportAndImport.put("wmf", new WmfOptions());
formatsThatSupportExportAndImport.put("emz", new EmfOptions() {{ setCompress(true); }});
formatsThatSupportExportAndImport.put("wmz", new WmfOptions() {{ setCompress(true); }});
formatsThatSupportExportAndImport.put("svgz", new SvgOptions(){{ setCompress(true); }});
formatsThatSupportExportAndImport.put("tga", new TgaOptions());
formatsThatSupportExportAndImport.put("webp", new WebPOptions());

//Formats that can be only saved
HashMap<String, ImageOptionsBase> formatsOnlyForExport = new HashMap<String, ImageOptionsBase>();
formatsOnlyForExport.put("psd", new PsdOptions());
formatsOnlyForExport.put("dxf", new DxfOptions() {{ setTextAsLines(true); setConvertTextBeziers(true); }} );
formatsOnlyForExport.put("pdf", new PdfOptions());
formatsOnlyForExport.put("html", new Html5CanvasOptions());

//Formats that can be only loaded
List<String> formatsOnlyForImport = Arrays.asList("djvu", "dng", "dib", "eps", "cdr", "cmx", "otg", "odg");

//Get total formats that can be saved
HashMap<String, ImageOptionsBase> exportToFormats = new HashMap<String, ImageOptionsBase>(formatsOnlyForExport);
exportToFormats.putAll(formatsThatSupportExportAndImport);

//Get total formats that can be loaded
List<String> importFormats = new LinkedList<>(formatsOnlyForImport);
importFormats.addAll(formatsThatSupportExportAndImport.keySet());
final int[] i = {0};
final Long[] resizeTypes = ResizeType.getValues(ResizeType.class);

importFormats.forEach((formatExt) -> {
	String inputFile = templatesFolder + "template." + formatExt;

	for (Map.Entry<String, ImageOptionsBase> exportFormat : exportToFormats.entrySet())
	{
		String outputFile = String.format("%s\\%s\\%s-%s-to-%s.%s", templatesFolder, "convert", "convert-", formatExt, exportFormat.getKey(), exportFormat.getKey());
		System.out.println(outputFile);
		// More about load method can be found at
		// https://apireference.aspose.com/imaging/java/com.aspose.imaging/Image#load-java.lang.String-
		try (Image image = Image.load(inputFile))
		{
			ImageOptionsBase exportOptions = exportFormat.getValue().deepClone();
			if ((formatExt.equals("emf") || formatExt.equals("emz")) && (exportFormat.getValue() instanceof WmfOptions))
			{
				EmfRasterizationOptions rasterizationOptions = new EmfRasterizationOptions();
				rasterizationOptions.setPageWidth(image.getWidth());
				rasterizationOptions.setPageHeight(image.getHeight());

				exportOptions.setVectorRasterizationOptions(rasterizationOptions);
			}
                        int newWidth = 300;
		        int newHeight = 800;
                        //Here you can provide resize as you need
                        //image.resize(newWidth, newHeight, resizeTypes[i[0]].intValue());
			image.save(outputFile, exportOptions);
		}
	}
});