Home Java Java-conversion Convert ZIP To PDF

Related Tutorials


 
 

Share on Google+Share on Google+

Convert ZIP To PDF

Advertisement
Lets discuss the conversion of a zipped file into pdf file with the help of an example.

Convert ZIP To PDF

     

Lets discuss the conversion of a zipped file into pdf file with the help of an example.

Download iText API required for the compilation of this example from the link http://www.lowagie.com/iText/download.html.

First set the class path for jar file by using the following steps:

1.Download the iText jar files.
2.Unzip all  the .jar files.
3.Copy all .jar files into lib directory of jdk. e.g. For jdk1.6.0 we need to copy  C:\jdk1.6.0\lib.
4.Set the classpath by using the following steps.
Right click on " My Computer->Click on Properties->Advanced->Environment variables 

In the Environment variable check whether the classpath is set or not in the variable "classpath". If the path is not set then click on "Edit" button (if variable classpath is already created otherwise create the variable "classpath") and set the full path with the name of iText API .jar files then click on OK and Apply button.

Pass the name of  a zipped  file  through command line argument. Take the zipped file as input by using the FileInputStream. Read this file from  the system, unzip it and then store it into a byte array. Convert this array of byte into the string by using the string class constructor.Generate a pdf file by using the following steps.

1.Create an instance of the Document class. The Document class describes a document's page size, margins, and other important attributes. The Document class act as a container for a document's sections, images, paragraphs, chapters and other contents.
2.Open the document by using doc.open ().
3.Pass the string into the paragraph by using the code Paragraph p = new Paragraph (String str).
6.Add this paragraph to the document by using doc.add(p).
Create a pdf file and pass this document in this pdf file by using the code PdfWriter.getInstance (doc, new FileOutputStream ("pdfFile.pdf")) for creating a PDF document writer
5.Close the document by using doc.close ().
Here is the code of the program :

import java.io.*;
import java.awt.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.io.*;
import java.util.zip.*;
public class  ZipToPDF

public static void main(String arg[])throws Exception
  {  
 System.out.println("Hello RoseIndia");
 Document document = new Document(
PageSize.A4, 36, 72, 108, 180);
 PdfWriter.getInstance(document,System.out);
 PdfWriter.getInstance(document,new 
FileOutputStream("pdfFile.pdf"));
 document.open();
 ZipInputStream  zip = new ZipInputStream(new 
BufferedInputStream(new FileInputStream(arg[0])));
   ZipEntry entry;
 while((entry = zip.getNextEntry()) != null)
  {
  byte data[]=new byte[1024];
  int count;
  String text="";
  while((count=zip.read(data,0,1024))!=-1)
  {
  text=new String(data);
  document.add(new Paragraph(text));
  } 
  } 
  document.close();
  }
}

Output of The Example:


Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 14, 2008

Related Tutorials

Discuss: Convert ZIP To PDF  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:8
kumaravel
July 11, 2011
zip to pdf

convert zip into pdf
m.shajahan
December 2, 2011
zip to PDF convater

i want convate zip to pdf
Victor
January 13, 2012
covertt winzip file to pdf

I hope it works
remesh
February 10, 2012
boidata

change to pdf
rahamath
July 18, 2012
conver zip into pdf

zip file
sheesh pal chauhan
August 11, 2012
vinzip to PDF file

i want to converter winzip to PD file
chandra pra kash jha
December 20, 2012
how do zip file convert with pdf

Dear Sir Plese
sudip gautam
January 9, 2013
how do zip file convert with pdf

i wanna do it
DMCA.com