Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Add Content of an iText PDF File using java code 
 

In many applications there is a need to generate pdf file dynamically. The iText library can be used to generate PDF documents from Java program.

 

Add Content of an iText PDF File using java code

                         

In many applications there is a need to generate pdf file dynamically. The iText library can be used to generate PDF documents from Java program. The iText is a open source library that provide facility to create pdf file from java application. iText is a freely available Java library from Lowagie.com, it supports the generation of HTML, RTF, and XML and PDFdocuments. You can use variety of fonts and colors in document. To use iText library you need to import package.....

import com.lowagie.text.*;

In the example given below we will create a pdf file at the specified location. In this example we will first create Document object that has arguments page size, left right top and bottom margins. After that create object of PdfWriter class. Other writers are HtmlWriter, RtfWriter, XmlWriter etc.

 

 

CreatePDFiText.java
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Chunk;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfContentByte;
public class CreatePDFiText {
	public static void main(String[] args) {
	    try {
		Document document = new Document(PageSize.
                A4, 50, 50, 50, 50);
		PdfWriter writer = PdfWriter.getInstance
                (document, new FileOutputStream("C:\\my.pdf"));
                document.open();
                // create a chunk object using chunk class
                of itext library.
		Chunk underlined = new Chunk("This is 
                sample pdf file created by : ");
		// set the distance between text and line.
		underlined.setTextRise(8.0f);
		// set the width of the line, 'y' position,
               color and design of the line
		underlined.setUnderline(new Color(0x00, 0x00, 
                0xFF),0.0f, 0.2f, 3.0f, 0.0f,
                   PdfContentByte.LINE_CAP_PROJECTING_SQUARE);
		// finally add object to the document.
		document.add(underlined);
                document.add(new Paragraph("Mahendra Singh",
                FontFactory.getFont(FontFactory.COURIER, 14, 
                Font.BOLD, new Color(255, 150, 200))));;
		document.close();
	     } 
	     catch (Exception e2) {
			System.out.println(e2.getMessage());
	     }
	}
}

Save this java code with .java , before run this code you need to add iText library so first download the .jar file of iText library and add to jdk library. If you are using any IDE, so you can directly add iText library to IDE. After that compile as a java code with javac command and run. This code will create a pdf file on specified location.   

Download Output PDF file my.pdf

 Download Source Code

                         

» View all related tutorials
Related Tags: c web pdf file stl text application import jar diff io make packages port this package itext app if tex

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 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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 | Flex 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.

» View all related tutorials
  » View all related tutorials
Related Tags: c web pdf file stl text application import jar diff io make packages port this package itext app if tex

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 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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 | Flex 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.