| Images are one of the major data components beside text information, which are shared among different users. Sharing the Images in PDF format is also a very convenient way. Keeping this aspect in mind, Aspose.Pdf has been designed to support the conversion of Images into PDF format. Over the several years of efforts, our development team has come up with this robust feature which offers the capability to convert almost all the conventional image formats into PDF files. |
Supported image formats
Following is the table showing supported image formats that are suitable for conversion into PDF files.
| Image File Types | Description |
|---|---|
| Ccitt | Ccitt type |
| Gif | Gif type |
| Jpeg | Jpeg type |
| Png | Png type |
| Tiff | Tiff type |
| Bmp | Bmp type |
| Exif | Exif type |
| Icon | Icon type |
| Wmf | Wmf type |
| MemoryBmp | MemoryBmp type |
| Unknown | Unknown type |
DOM approach
In our DOM (document object model) every PDF file is comprised of sections, and each section contains paragraphs. Whereas a paragraph can comprise of text, and image file, Table, Floating Box, Graph, Heading, form field or an attachment. So in order to convert and image file into PDF format, we must enclose it into a paragraph.
| A section can either be the body of the PDF file or Header / Footer. So an image can be enclosed in paragraph residing in PDF body or in Header / Footer section. |
Implementation details
You can use an image placed over some physical location over hard-drive, an image placed in MemoryStream or present over some Web URL. To add an image, simply create an object of Image class, add the image to paragraphs collection of a section, specify the location where an image is placed and specify the image type. For that reason we have a class named ImageInfo which offers the capability to define the image source.
- If an image is placed at some particular location over hard-drive, we need to specify the path location to Image.getImageInfo().setFile() method.
- If an image is placed in MemoryStream, we need to pass the MemoryStream object holding the image to Image.getImageInfo().setMemoryData() method.
- If an image is placed over some web location, we need to pass the image URL value to Image.getImageInfo().setFile() method.
Try using the following code snippet, to convert 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");
<?xml version="1.0" encoding="utf-8" ?> <Pdf xmlns="Aspose.Pdf"> <Section> <Image File="C:/Images/Apple.jpg" Type="jpeg"> <Title>JPEG image</Title> </Image> </Section> </Pdf>
Image class also has a method named setImageScale(), which enables you to set the scale rate of the image when placed over PDF file. You can also use Opacity property in Image class to set the opacity of the image, and value can be specified between 0.0 to 1.0. Where 0.0 is the factor for 100% transparent and 1.0 is for opaque.
Besides setting Top, Left, Margin value for the image you can also set the RotationAngle by using the setRotationAngle() method of ImageInfo class.
While dealing with images, you can also refer to ImageInfo class, which facilitates capability to specify the Image tile information using setTitle() method, adjust the image alignment using setAlignment()method, specify the Image Height & Width using setFixHeight() & setFixWidth() method, set the image border information using setImageBorder() method, specify the image type using setImageFileType() method, specify the image source using setFile and setMemoryData() method (when using MemoryStream), and in case the image is back-and-white you can use setIsBlackWhite() method to improve the performance factor and much more.
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 as described in http://java.sun.com/products/java-media/jai/downloads/download-iio-1001.html page. 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");
| For more detailed information regarding the features offered by ImageInfo class, please visit ImageInfo Members. |
