Creating Pdf in itext
iText is a free Java-PDF library that allows you to generate PDF files on the fly (dynamically).iText is an ideal library for developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation. iText is not an end-user tool. Typically you won’t use it on your Desktop as you would use Acrobat or any other PDF application. Rather, you’ll build iText into your own applications so that you can automate the PDF creation and manipulation process.
iText (Java-PDF Library) can be used to:
- Serve PDF to a browser
- Generate dynamic documents from XML files or databases
- Use PDF’s many interactive features
- Add bookmarks, page numbers, watermarks, etc.
- Split, concatenate, and manipulate PDF pages
- Automate filling out of PDF forms
- Add digital signatures to a PDF file
- Technical Requirements to use iText
You should have JDK 1.4 or later to integrate iText PDF generation in your application.
Downloading iText
http://itextpdf.com/download.php
or
http://sourceforge.net/projects/itext/files/
1) Creating a simple pdf using itext in jsp
It is simple to create a pdf file in jsp using itext. This tutorial assume you had some experience with Servlet and JSP technology, know how to setup and run web container such as Apache Tomcat and compile/install Servlet.
Prerequisite : Copy and paste itext-1.4.8.jar(or latest itext jar) into your web application WEB-INF\LIB\ folder. This allow servlet and jsp in your web application gain access to iText library.
Sourcecode :
<%@
page import="java.servlet.*,
javax.servlet.http.*,
java.io.*,
java.util.*,
com.itextpdf.text.pdf.*,
com.itextpdf.text.*,java.sql.*"
%><%
response.setContentType("application/pdf");
Document document = new Document();
try{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance(document, buffer);
document.open();
PdfPTable table = new PdfPTable(2);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
document.add(table);
document.close();
DataOutput dataOutput = new DataOutputStream(response.getOutputStream());
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for(int i = 0; i < bytes.length; i++)
{
dataOutput.writeByte(bytes[i]);
}
}catch(DocumentException e){
e.printStackTrace();
}
%>
2) Creating a simple pdf using itext with Mysql connectivity in jsp.
Sourcecode :
<%@
page import="java.servlet.*,
javax.servlet.http.*,
java.io.*,
java.util.*,
com.itextpdf.text.pdf.*,
com.itextpdf.text.*,java.sql.*"
%><%
response.setContentType("application/pdf");
Document document = new Document();
try{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance(document, buffer);
document.open();
PdfPTable table=new PdfPTable(2);
table.addCell("First Name");
table.addCell("Last Name");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/trydb", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select * from trytable");
while(rs.next()){
table.addCell(rs.getString("fname"));
table.addCell(rs.getString("lname"));
}
document.add(table);
document.close();
DataOutput dataOutput = new DataOutputStream(response.getOutputStream());
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for(int i = 0; i < bytes.length; i++)
{
dataOutput.writeByte(bytes[i]);
}
}
catch(DocumentException e)
{
e.printStackTrace();
}
%>
/* Database : trydb
create table trytable (fname varchar(10), lname varchar(10)); */
No comments:
Post a Comment