Home Java Java-conversion Convert Text To PDF
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Convert Text To PDF

Advertisement
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, 36, 72, 108, 180);
        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.

Advertisements

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

Ask Questions?    Discuss: Convert Text To PDF   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Sweetu More
April 21, 2011
Convert Text To Pdf for android

hey i liked your code but it,s java application. i want to create pdf into android application.is it possible to send me the code or any suggestion for generating pdf in android application. Thank You In anticipation
Arun
June 14, 2011
pgm for convert text to pdf.

It's simply super and easy to know the logic.
Dhiral Vinay Pandya
May 1, 2012
http://omtlab.com/

Nice tutorial thanks
Nic Pienaar
June 14, 2012
Broken Link

Hi, I was on the lookout for a library for text to pdf, and foud your side, but the download link is no longer available. Are there any alternatives to the get hold of the package? Kind regards, Nic email: nic@ces-h.com
Praveen
September 27, 2012
class Path setting

I did not understand the class path part setting pleas tell me how to set class path and which file to add in class path
vqoley
October 5, 2013
cannot download

hello admin. please upload again file for download library... e-mail me after you update the download link . vqoley7@gmail.com
avneesh atri
October 11, 2013
Problem in pdf creation

I have a file which contain string like this "09.07.13 0207459*** 15:26:23 00:05:25 12 0,8076" "12.07.13 0207459*** 11:01:50 00:00:56 3 0,2019" if you will see them in a text file these are proper aligned and the appear to be like table columns. But when i print a pdf . Spaces between them are getting messed up so they are not coming in proper format . Can you please help me with this.
DMCA.com