Archive

Posts Tagged ‘pdf’

Java code to Wrap and Rotate image in PDF File

August 2, 2010 2 comments

Introduction:

Now-a-days if we wanna develop an application, that application must have the capability to prefer the Reports. All the reports are  favour to PDF format. If we develop a report for a particular concern the client needs to place their logo in the report. For that we need to insert the image in PDF file. This article deals the same.

The itex.jar makes it most simple and elegant.

Prerequisites:

JDK 1.5 and Above

Jar : itext.jar(Download here)

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.

Steps involved in Insert image in PDF File:

Create Document :

This class describes a PDF file’s page size, margins, and other important attributes. It works as a container for a document’s chapters, sections, images, paragraphs, and other content.

Create PdfWriter:

This class is used to create the pdf file.

By using the method getInstace we can create pdf file from the given output stream.

Open pdf File:

Now open the document by using open() method in document class.

Get the Image content:

By using the method getInstace() of image class we can get the image content.

The method has the image filename as argument.

Add contents to Pdf File:

By using add method in document class we can add paragraph and image in the pdf file.

Resize the image:

By using the scaleAbsolute() method we can resize the image. We can pass the width and height as the argument to resize the image.

Rotate image:

By using setRotationDegrees() method of image class we can set the degree which we want to rotate the image.

After rotate the image we add the image into the pdf file by using add method of document class.

Code:

package com.bsj.itext;

import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

/**
 *
 * @author Muneeswaran
 */
public class WrapImageinPdf
{
 public static void main(String arg[]) throws Exception
 {
 //Create Document
 Document document = new Document(PageSize.A4.rotate());
 //Create PdfWriter:
 PdfWriter.getInstance(document, new FileOutputStream("imagePdf.pdf"));
 Font font = new Font(Font.TIMES_ROMAN, 18, Font.BOLD);
 //Open pdf File:
 document.open();
 Paragraph paragraph = new Paragraph("Original:", font);
 Paragraph par = new Paragraph("After rotate:", font);
 //Get the Image content:
 Image image = Image.getInstance("2.jpg");
 //Add contents to Pdf File:
 document.add(paragraph);
 document.add(image);
 document.add(par);
 //Resize the image:
 image.scaleAbsolute(500.0f, 500.0f);
 image.setBorder(1);
 //Rotate image:
 image.setRotationDegrees(45.0f);
 document.add(image);
 document.close();
 }
}

Output:

I hopes this helps.Go ahead to prefer report and inserting logo in pdf.Before that,if you feel it useful,leave your foot prints here[Comments].

Advertisements