Rotating image in the pdf file

In this section, you will learn how to rotate an image in the pdf in java.

Rotating image in the pdf file

Rotating image in the pdf file


In this program we are going to tell you how we can insert a image in a pdf file and rotate it irrespective of the fact whether it exists or not. If it exists, fine otherwise it will get automatically created. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. Without this .jar the application will not run. The packages we were talking about are* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named imagesRotationPDF. Remember the name of the file should be such that the reader can easily  understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of Document class. This class describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specific OutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by Image.getInstance() gets the image. After that add the content to the document. Add the paragraph and then add the image and set image.scalePercent (20.0f) and   image.setRotationDegrees (30.0f).at finally close the document.  

The code of the program is given below:

The output of the program is given below:

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class imagesRotationPDF
public static void main(String arg[])throws Exception
Document document=new Document();
Image image = Image.getInstance ("devi.jpg");
document.add(new Paragraph("Original:"));
image.scalePercent (20.0f);
image.setRotationDegrees (30.0f); 
document.add(new Paragraph("After Scalable and Rotation"));

Download this example.