Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML

Search:
   Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML Facing Programming Problem? Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Convert Text To PDF
Here we are discussing the convertion of a text file into a pdf file by using an example. In this example we take a string from console, pass this string into an object of paragraph class. Pass this paragraph object into the add() method of the document
 
 

Convert Text To PDF

                         

Here we are discussing the convertion of a text file into a pdf file by using an example. In this example we take a string  from console, pass this string into an object of paragraph class. Pass this paragraph object into the add() method of the document class to generate a pdf file. Download iText API required for the conversion of a text file into the pdf file from 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.To set the classpath follow these 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 button and finally on Apply button.

Use InputStream for reading the text from console that returns a string into byte format. Convert this byte formatted string into string by using BufferedReader. Follow the steps given below to generate a pdf file.

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.Use PdfWriter.getInstance (doc, new FileOutputStream ("pdfFile.pdf")) for creating a PDF document writer.
3. Open the document by using doc.open ().
4. Add content to the document. Use Paragraph p = new Paragraph (String str) to create a Paragraph that is used to store the string.
6.Use doc.add(p) to add the text.
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 TextToPDF{
  public static void main(String arg[]){
    try{
      InputStreamReader in= new InputStreamReader(System.in);
      BufferedReader bin= new BufferedReader(in);
      System.out.println("Enter text:");
        String text=bin.readLine();
        Document document = new Document(PageSize.A4, 3672108180);
        PdfWriter.getInstance(document,new FileOutputStream("pdfFile.pdf"));
        document.open();
        document.add(new Paragraph(text));
        System.out.println("Text is inserted into pdf file");
        document.close();
    }catch(Exception e){}
  }
}

 Input on dos Prompts :

C:\convert\completed>javac TextToPDF.java
C:\convert\completed>java TextToPDF
Enter text:
Hi, I am Rajesh Kumar
Text is inserted into pdf file

Output of The Example:


Download this example.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.