How to Add And Rotate Images In Pdf in java


In this core java tutorial we will learn How to Add And Rotate Images In Pdf using iText library - core java tutorial in Java with program and examples.



Follow these steps to Add And Rotate Images in java using iText>
  1. Create FileOutputStream - the file in which created PDF will be stored
OutputStream fos = new FileOutputStream(new File(pdfFilePath));

  1. create Document (This document will represent the PDF document)
Document class is present in com.itextpdf.text.Document.
Document document = new Document();

  1. Attach PdfWriter to document,whenever any PDF element is added to document, it will get written to FileOutputStream

PdfWriter.getInstance(document, fos);

  1. open the document.
document.open();
                

  1. Now we will get the image from your local system or if you are working on web applications you may give path of image on server.
And create object/instance of com.itextpdf.text.Image like (Image image = Image.getInstance("E:/tiger.jpg"))

Then optionally you may set absolute position, Then optionally you may set the image width and height.

  1. Set the degree of rotation for rotating the image.
image.setRotationDegrees(90f);
                
  1. add image to document
document.add(image);
  1. close document
document.close();
                

How to Create Roman List In pdf in java - iText java tutorial

How to Set the Font Name, Size, Style and Colour In Pdf using itext in java

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

Convert Html To Pdf in java using iText - iText java tutorial




Download jars required to execute program >

Or, you may download jars from here.

Program/Example to How to Add And Rotate Images In Pdf Using iText library - core java tutorial

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
*  How to Add And Rotate Images In Pdf Example
*  Using iText library - core java tutorial
*
*/
public class AddingAndRotatingImagesInPdfExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "E:/Adding And Rotating Images In Pdf Example.pdf";
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter.getInstance(document, fos);
                 document.open();
                 document.add(new Paragraph(
                              "This is AddingAndRotatingImagesInPdfExample.pdf"));
                 // Add Image from your system
                 Image image = Image.getInstance("E:/tiger.jpg");
                 // Set absolute position for image in PDF (or fixed)
                 image.setAbsolutePosition(100f, 500f);
                 // Scale image's width and height
                 image.scaleAbsolute(200f, 200f);
                 image.setRotationDegrees(90f);
                 document.add(image);
                 document.close();
                 fos.close();
                 System.out.println("PDF created in >> " + pdfFilePath);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/* OUTPUT of above program/Example -
PDF created in >> E:/Adding And Rotating Images In Pdf Example.pdf
*/


PDF formed after executing above java program(How to Add And Rotate Images In Pdf in java) will look like this >
In this PDF we have rotated image by 900.

Summary -
So in this core java tutorial we learned How to Add And Rotate Images In Pdf using iText library - core java tutorial in Java with program and examples.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java

Creating the Table and set the Column Width And Spacing In Pdf in java

Rotate cells, cell padding, horizontal and vertical alignment of Cell in table in Pdf in java itext - iText java tutorial example




Creating LIST in pdf in java - iText tutorial >

Create Greek List In pdf in java using iText - iText java tutorial

How to Create Roman List In pdf in java - iText java tutorial

Create ZapfDingbats List In pdf in java - iText java tutorial program

How to Create Ordered List In pdf in java using iText - iText java tutorial

Creating the UnOrdered List In pdf in java - iText java tutorial example

How to Create Lists And SubLists In Pdf in java - iText java tutorial



Creating PASSWORD PROTECTED PDF in java - iText tutorial >

How to Create Password Protected Pdf in java - iText Example




Set FONT NAME, SIZE, STYLE, COLOUR in pdf in java - iText tutorial >

How to Set the Font Name, Size, Style and Colour In Pdf using itext in java


Create ANCHOR (HYPERLINK), SUPERSCRIPT, SUBSCRIPT, UNDERLINE AND STRIKETHROUGH in pdf in java - iText tutorial >

How To Set HyperLink (Anchor - a tag) In Pdf in java - iText java tutorial

How To Set SuperScript And SubScript In Pdf in java - iText java tutorial

UnderLine Text In Pdf in java - iText java tutorial example

Strikethrough Text In Pdf in java - iText java tutorial program


HTML to PDF in java - iText tutorial >

Convert Html To Pdf in java using iText - iText java tutorial


Create new page, set page height and width in java - iText tutorial >

How To Create New Pages In Pdf Using Itext - iText java tutorial example

How to Find Pdf Page Height And Width in java - iText java tutorial program


No comments:

Post a Comment