In my last tutorial, I shared the java code to convert TIFF frames to JPEG and then transform it to PDF. In this Tutorial I am going to share code for converting JPEG and TIFF images to PDF. I am using Aspose.PDF for Java library for this task you can use the library you want or you can also try this code by using Aspose Trial version. Aspose.PDF for Java Library can convert any image to PDF format but I am using only sharing code for JPEG and TIFF conversion to PDF in this tutorial. I hope the code will help you in understanding how to convert an image to PDF file.

Following code snippet converts a JPEG image into PDF file
Code:
//Instantiate a Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
 
//Create a section in the Pdf object
Section sec1 = pdf1.getSections().add();
 
//Create an image object in the section
aspose.pdf.Image img1 = new aspose.pdf.Image(sec1);
 
//Add image object into the Paragraphs collection of the section
sec1.getParagraphs().add(img1);
 
//Set the path of image file
img1.getImageInfo().setFile("C:/Images/Apple.jpg");
 
//Set the path of image file                                          
img1.getImageInfo().setTitle("JPEG image");
 
//Save the Pdf
pdf1.save("d:/pdftest/JPEG_image_toPDF.pdf");
Converting TIFF image to PDF

TIFF is a special kind of image which comprises of frames. In order to convert TIFF image into PDF format, first you need to install Java Advanced Imaging Image I/O Tools. The JAI Image I/O Tools classes provide additional plug-in for advanced formats such as JPEG-LS, JPEG2000 and TIFF. After installation, import javax.imageio.ImageIO package. The ImageInfo class can be used to specify which particular frame you need to add to the PDF file. The default value is 0 and if the method setTiffFrame method is called with an argument -1, all frames of the tiff images will be added into the PDF document.
Code:
//Instantiate Pdf instance by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create a new section in the Pdf object
Section sec1 = pdf1.getSections().add();
//Create an image object in the section
Image image = new aspose.pdf.Image(sec1);
//Add image object into the Paragraphs collection of the section
sec1.getParagraphs().add(image);
//Set the ImageStream information
image.getImageInfo().setSystemImage(ImageIO.read(new java.net.URL("file:///d:/pdftest/BW_MultiPage_TIFF.tif")));
// set the value that all frames of tiff image need be added into PDF document
image.getImageInfo().setTiffFrame(-1);
//Save the pdf document
pdf1.save("d:/pdftest/TextAlignmentTest.pdf");