• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Creating PDF file using itext

August 7, 2012 By j2eereference Leave a Comment

Here we will see how can we create a PDF file using the itext library

We have to download itext.jar and keep it in the lib folder. Now the below code will create a pdf file called first.pdf

 Java code to generate the PDF file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.*;
 
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
 
public class FirstPDF {
public static void main(String arg[]) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("first.pdf"));
document.open();
document.add(new Paragraph("This is my first Pdf file ---"));
document.close();
}
}

In the above code you can notice that

1) We are making an object of class Document.This works sa a container. This can be used for describing the document’s properties like page size, margins etc.

2) After creating the document object we are calling the getInstance method of the pdfWriter by passing document object and FileOutputstream object, Which will create PDF file. If the mentioned pdf file doesn’t exist then it will create a pdf file.

3) Open the document by document.open().

4) We can add the content to the document by passing the paragraph object to the add method.

4) Finally close the document by using the document.close(). This method will flush and close the OutputStream instance.

Output of the above code

This code will generate a pfd file called first.pdf with the paragraph content as
This is my first Pdf file —

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.