Skip to content
Related Articles

Related Articles

Rotating an Image in a PDF Document Using Java
  • Last Updated : 01 Dec, 2020

In this article, we will learn how to  Rotating an Image in a PDF document using Java. For  Rotating an Image in a PDF, we will use the iText library. These are the steps that should be followed to  Rotating an Image in a PDF using java.

1. Creating a PdfWriter object

The PdfWriter class represents the DocWriter for a PDF. The constructor of this class accepts a string, i.e. the path of the file where the PDF is to be created.

// importing the PdfWriter class. 
import com.itextpdf.kernel.pdf.PdfWriter; 

// path where the pdf is to be created. 
String path = "C:/JavaPdf/rotateImage.pdf"; 
PdfWriter pdfwriter = new PdfWriter(path);

2. Creating a PdfDocument object

The PdfDocument class is the class that represents the PDF Document in iText, To instantiate this class in write mode, you need to pass an object of the class PdfWriter (i.e. pdfwriter from above code ) to its constructor.



// Creating a PdfDocument object. 
// passing PdfWriter object constructor of pdfDocument. 
PdfDocument pdfdocument = new PdfDocument(pdfwriter); 

3. Creating the Document object

The Document class is the root element when creating a self-sufficient PDF. One of the constructors of this class accepts an object of the type PdfDocument class(i.e. pdfdocument).

// Creating a Document and passing pdfDocument object 
Document document = new Document(pdfdocument); 

4. Create an Image object

We need the image object to manage the images. In order to create an image object, we need to create an ImageData object. We can create it by passing the string parameter that represents the path of the image to create() method of the ImageDataFactory class. Now we can create an image object by passing the ImageData object, as a parameter to the constructor of Image class.

// Create an ImageData object 
String imageFile = "F:/JavaPdf/image.jpg"; 
ImageData data = ImageDataFactory.create(imageFile); 

// Creating an Image object 
Image image = new Image(data);

5. Rotating an image

To rotate an image we use setRotationAngle(), And we got to pass an integer representing the rotation angle by which we would like to rotate the image.

// Rotating the image 
image.setRotationAngle(90);

6. Add image to the PDF Document

Add the image object using the add() method of the Document class, and close the document using close() method of Document class



// Adding image to the document 
document.add(image); 

// Closing the document  
document.close();

Example: Here is the final code that helps us in understanding how to Rotate an Image in a PDF document using Java.

Java




import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
  
public class RotateImage {
    public static void main(String args[]) throws Exception
    {
  
        try {
            // path where the pdf is to be created.
            String path = "F:/JavaPdf/RotateImage.pdf";
            
            // Creating a PdfWriter
            PdfWriter pdfwriter = new PdfWriter(path);
  
            // Creating a PdfDocument object.
            // passing PdfWriter object constructor of
            // pdfDocument.
            PdfDocument pdfdocument
                = new PdfDocument(pdfwriter);
  
            // Creating a Document and passing pdfDocument
            // object
            Document document = new Document(pdfdocument);
  
            // Create an ImageData object
            String imageFile = "F:/JavaPdf/image.jpg";
            ImageData data
                = ImageDataFactory.create(imageFile);
            
            // Creating an Image object
            Image image = new Image(data);
            
            // Creating an Image object
            Image image = new Image(data);
  
            // Rotating the image
            image.setRotationAngle(90);
  
            // Adding image to the document
            document.add(image);
            
            // Closing the document
            document.close();
  
            System.out.println(
                "Image has been rotated successfully");
        }
        catch (Exception e) {
            System.out.println(
                "failed to rotate the image in the file due to "
                + e);
        }
    }
}

Compile and Execute

javac RotateImage.java 
java RotateImage

Output

Image has been rotated successfully

PDF 

Rotate an image

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :