Add images to PDF using iText library.
Posted By : Manish Gupta | 31-Dec-2018
In this blog, we will see how to add images to the PDF using iText library in java. Particularly, we will see how to add images to a table and set the width and heights of the image.
Let's suppose, we have to add images to a table with the text in front of the image and also suppose we are having the URL for the image which has to add on the PDF and let's say it is stored in some variable "imagePath".
iText provides a class com.itextpdf.text.Image to create an instance of the image. So, we need to create an instance of the com.itextpdf.text.Image. Now to get the instance of the image, use the below code.
Image myImage = Image.getInstance( imagePath );
com.itextpdf.text.Image: It is a representation of the graphic elements like GIF, JPEG, PNG, etc.
There are other ways as well to get the image instance, like if we have the byte[] array of the image, then also we can generate an image instance using com.itextpdf.text.
Image myImage = Image.getInstance( myImgBytes );
Note: myImgBytes is an array holding the bytes of the actual image object.
Now to set the width and height of the image, iText provides scaleToFit( float width, float height ) method.
myImage.scaleToFit( 70, 70 );
There are various other things provided by the library that can be done on a myImage object like:
1) set the image background color, for ex: myImage.setBackgroundColor( BaseColor.RED );
2) set the absolute position of the image, for ex: myImage.setAbsolutePosition( 70, 100 );
3) set the border widths and color, fox: image.setBorderWidth( 2 );
4) rotate the image and any other, fox ex: image.setRotation( 50 ); where 50 is in radians.
Also, if the image cannot be shown for some reason, then we can show the alternate text in place of the image. For this, iText provides setAlt( String alt )
myImage.setAlt( "photo" );
Now, let's add the above image instance to a table. The first column will have the image and the second column will have the description of the image.
PdfPTable tableWithImage = new PDfPTable(2);
PdfPCell imageCell = new PdfPCell();
imageCell.addElement( myImage );
tableWithImage.addCell( imageCell );
PdfPCell imageDesc = new PdfPCell("This is demo for adding image to the table");
tableWithImage.addCell( imageDesc );
Now in order to add the table to the document, let's use the Document class.
Document doc = new Document();
doc.open();
doc.add( tableWithImage );
doc.close();
-------- Thanks --------
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Manish Gupta
Manish is a Java Developer with hands on experience in Core Java, JSP, Spring framework, JavaScript, JQuery, HTML, CSS, and SQL/PL-SQL. Tools used: Eclipse, Netbeans, DBeaver, Oracle SQL Developer, Toad, MS SQL Server. He is a keen learner and technology