Post: [Java Tutorial # 4] How to Convert JPEG and TIFF Image to PDF
01-28-2014, 07:14 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); In my last tutorial, I shared the You must login or register to view this content.. 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
    
//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 You must login or register to view this content.. 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.
    
//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");

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo