write hello in the

113
WRITE HELLO IN THE PDF FILE In this program we are going to tell you how you can write a "Hello" word in a pdf file irrespective of the fact whether it exists or not. To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program. Now create a file named HelloWordPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program. Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name. Now open the document by document.open(). After that add the content to the document. Create aParagraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph we have passed one String "Hello

Upload: raghupulishetti

Post on 07-Sep-2014

69 views

Category:

Documents


3 download

TRANSCRIPT

WRITE HELLO IN THE PDF FILE

In this program we are going to tell you how you can write a "Hello" word in a pdf file irrespective of the fact whether it exists or not.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named HelloWordPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the content to the document. Create aParagraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph we have passed one String "Hello Pdf" . At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.*;import com.lowagie.text.pdf.*;import com.lowagie.text.*; public class HelloWordPDF { public static void main(String arg[])throws Exception { Document document=new Document(); PdfWriter.getInstance(document,new FileOutputStream("hello.pdf"));

document.open(); document.add(new Paragraph("Hello Pdf")); document.close(); }}

The output of the program is given below:

How to adjust a size of a pdf file

In this program we are going to tell you how we can adjust the size of a pdf file irrespective of the fact whether it exists or not. If it exists then its fine, otherwise a pdf file will be created. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program.

Now create a file named pagesizePDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.  Inside the constructor of Document we have encapsulated predefined page size. These page sizes are encapsulated in the class PageSize.

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the content to the document. Create aParagraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph pass the String . At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes theOutputStream instance. 

The code of the program is given below:

import java.io.*;import java.awt.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;

public class pageSizePDF{ public static void main(String arg[])throws Exception{ Document document=new Document(PageSize.A4,0,0,10,10);PdfWriter.getInstance(document,new FileOutputStream("pageSizePDF.pdf"));document.open();document.add(new Paragraph("A4 page Size--->>roseinia.net"));document.close();}

The output of the program is given below:

A4 size

In this program we are going to tell you how we can automatically assign the right values for a A4 size to the document instance. Its options are from A0 to A10. These text explains the ISO 216 paper size system and the ideas behind its design. It will adjust the size of a pdf file irrespective of the fact whether it exists or not. If it exists then its fine, otherwise a pdf file will be created. After going this tutorial you will better understand how you can adjust the paper size. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program.

Now create a file named a4PDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.  Inside the constructor of Document we have encapsulated predefinedpage size. These page sizes are encapsulated in the class PageSize.

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the content to the document. Create aParagraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph pass the String . At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes theOutputStream instance. 

The code of the program is given below:

import java.io.*;import java.awt.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;public class a4PDF{ public static void main(String arg[])throws Exception{ Document document=new Document(PageSize.A4); PdfWriter.getInstance(document,

new FileOutputStream("a4PDF.pdf")); document.open(); document.add(new Paragraph("A4 page Size--->>roseinia.net")); document.close(); }}

The output of the program is given below:

pdf default size

In this program we are going to tell you how we can automatically set the default size of pdf. Its options are from A0 to A10. These text explains the ISO 216 paper size system and the ideas behind its design. It will adjust the size of a pdf file irrespective of the fact whether it exists or not. If it exists then its fine, otherwise a pdf file will be created. After going this tutorial you will better understand how you can adjust the paper size. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program.

Now create a file named defaultSizePDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.  Inside the constructor of Document we have encapsulated predefinedpage size. These page sizes are encapsulated in the class PageSize. In this example we have use many PageSize. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the content to the document. Create aParagraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph pass the String . Set the page size to the document. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;public class defaultSizePDF{public static void main(String arg[])throws Exception{Document document = new Document();PdfWriter.getInstance(document,new FileOutputStream("DefaultPageSize.pdf"));document.open();document.add(new Paragraph("The default PageSize==>>RoseIndia"));document.setPageSize(PageSize.A3);document.newPage();document.add(new Paragraph("This PageSize is DIN A3."));document.setPageSize(PageSize.A2);document.newPage();document.add(new Paragraph("This PageSize is DIN A2."));document.setPageSize(PageSize.A1);document.newPage();document.add(new Paragraph("This PageSize is DIN A1."));document.setPageSize(PageSize.A0);document.newPage();document.add(new Paragraph("This PageSize is DIN A0."));document.setPageSize(PageSize.A5);document.newPage();document.add(new Paragraph("This PageSize is DIN A5."));document.setPageSize(PageSize.A6);document.newPage();document.add(new Paragraph("This PageSize is DIN A6."));document.setPageSize(PageSize.A7);

document.newPage();document.add(new Paragraph("This PageSize is DIN A7."));document.setPageSize(PageSize.A8);document.newPage();document.add(new Paragraph("This PageSize is DIN A8."));document.setPageSize(PageSize.LETTER);document.newPage();document.add(new Paragraph("This PageSize is LETTER."));document.close();}}

The output of the program is given below:

Inserting image in the pdf file

In this program we are going to tell you how we can insert a image in a pdf file irrespective of the fact whether it exists or not. If it exists, fine otherwise it will get automatically created. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. Without this .jar the application will not run. The packages we were talking about arejava.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named imagesPDF. Remember the name of the file should be such that the reader can easily  understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of Document class. This class describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). Image.getInstance() gets the image. After that add the content to the document. Create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously created Document. Inside the constructor of Paragraph we have passed one String"Roseindia" . At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;

public class imagesPDF{ public static void main(String arg[])throws Exception{   Document document=new Document();  PdfWriter.getInstance(document,new FileOutputStream("imagesPDF.pdf"));  document.open();  Image image = Image.getInstance ("devi.jpg");  document.add(new Paragraph("Roseindia"));  document.add(image);   document.close();}}

The output of the program is given below:

How to wrap image in the pdf file

In this program we are going to tell you how we can insert a image  and wrap it according to your own style in a pdf file irrespective of the fact whether it exists or not. If it exists, fine otherwise it will get automatically created.  

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib directory of your web application. Without this .jar the application will not run.  Its a must to import this .jar file in the above directory.The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program.

Now create a file named imagesWrapPDF. Remember the name of the file should be such that the reader can easily  understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program. 

Firstly make a object of Document class. This class describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.  

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name. 

Now open the document by document.open().  Image.getInstance() gets the image. After that add the content to the document. Create a Paragraph that houses a paragraph of text, tells the PDFdocument writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraphto the previously created Document. Inside the constructor of Paragraph we have passed a String . We have written a paragraph inside the Paragraph constructor so that you can understand what is happening. Now add the object of Paragraph in the Document. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes theOutputStream instance. 

The code of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;

public class imagesWrapPDF{public static void main(String arg[])throws Exception{Document document=new Document();PdfWriter.getInstance(document,new FileOutputStream("imagesWrapPDF.pdf"));document.open();Image image = Image.getInstance ("devi.jpg");Paragraph para=new Paragraph("RoseIndia.net is a global services company that ensures maximum returns by providing quality software solutions and services. The Indian based company provides services to several reputed institutionalclients, in the domain of IT and IT enabled Technologies. We help in understanding the client requirements and offer customized solutions in various specialized areas like Web based Technologies , Database Systems , Client Server Architecture , E-commerce Solutions and Consultancy Services . 

Other services offered include Online Technical Tutorials , Content Development and Generation , Web solutions for B2B, B2C, C2C and Travel portals , Web Development,Promotion and Hosting, and Applications Service Provider Solutions . With expertise and skilled human resource, we also provide assistance for offshore development of projects. ");

document.add(para); document.add(image); document.close();}}

The output of the program is given below:

How to make a Rectangle type pdf

In this program we are going to tell you how we can make a pdf file in the rectangle shape irrespective of the fact whether it exists or not. If it exists then its fine, otherwise a pdf file will be created. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about

are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program.

Now create a file named rectangleSizePDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.  Inside the constructor of Document we have encapsulated predefined page size. These page sizes are encapsulated in the class PageSize.

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the content to the document. Create aParagraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph pass the String . At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes theOutputStream instance. 

Now the pdf is created and it is of type rectangle.

The code of the program is given below:

import java.io.*;import java.awt.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;public class rectangleSizePDF{public static void main(String arg[])throws Exception{Document document=new Document(PageSize.A4,0,0,10,10);PdfWriter.getInstance(document,new FileOutputStream("rectangleSizePDF.pdf"));document.open();document.add(new Paragraph("A4 page Size--->>roseinia.net"));document.close();}}

The output of the program is given below:

pdf background color

In this program we are going to tell you how we can change the background of the pdf file as well as the size of a pdf file irrespective of the fact whether it exists or not. If it exists then its fine, otherwise a pdf file will be created. After going through this tutorial you will be able to change the background of the pdf file which you have built.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program.

Now create a file named backgroundColorPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Make a object of Rectangle and define the size of the Rectangle inside the constructor of Rectangleglass. Call the setBackgroundColor() method of the Rectangle class. Now make a object of classDocument. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.  Inside the constructor of Document we have encapsulated predefined page size. These page sizes are encapsulated in the class PageSize.

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the content to the document. Create aParagraph that houses a paragraph of text, tells the PDF document writer to ensure

that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph pass the String . At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes theOutputStream instance. 

The code of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;public class backgroundColorPDF{ public static void main(String arg[])throws Exception { Rectangle pageSize = new Rectangle(400, 400);

pageSize.setBackgroundColor(new java.awt.Color(0xDF, 0xFF, 0xDE));

Document document = new Document(pageSize); PdfWriter.getInstance(document,

new FileOutputStream("backgroundColorPDF.pdf")); document.open(); Paragraph para=new Paragraph("Page Size and Background color"); document.add(para); document.add(new Paragraph("Background color--->>roseinia.net")); document.close(); }}

The output of the program is given below:

pdf tables

In this program we are going to tell you how we can make a table in a pdf file irrespective of the fact whether it exists or not. If it exists then its fine, otherwise a pdf file will be created. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named tablePDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). Make a object of PdfPTable class. It will create a table with 2 rows and 2 columns as we have passed 2 inside the constructor of a PdfPTable. After that add the content to the table. Now add the table to the Document object. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;public class tablePDF{ public static void main(String arg[])throws Exception{ Document document=new Document(); PdfWriter.getInstance(document,new FileOutputStream("tablePDF.pdf")); document.open(); PdfPTable table=new PdfPTable(2); table.addCell("Name"); table.addCell("Place"); table.addCell("RoseIndia"); table.addCell("Delhi"); document.add(table); document.close()

}}

The output of the program is given below:

pdf Table title

In this program we are going to tell you how the title will be given to the table of the pdf file. Suppose we have one pdf file in which we have a table and we want to give a title to it. We can make a table and give it a title irrespective of the fact whether the file exists or not. If it exists then its fine, otherwise a pdf file will be created. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program.

Now create a file named tableTitlePDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. The name which we have given to the file indicates what it is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.  

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). Make a object of PdfPTable class. It will create a table with 2 rows and 2 columns as we have passed 2 inside the constructor of a PdfPTable. Now make a object of PdfPCell.  Create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously created Document. Inside the constructor of Paragraph pass the String . Add the column space,horizontal alignment, background color in the cell. After that add the content to the table. Now add thetable to the Document object. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;import java.awt.*;public class tableTitlePDF{public static void main(String arg[])throws Exception{Document document=new Document();PdfWriter.getInstance(document,new FileOutputStream("tableTilePDF.pdf"));document.open();PdfPTable table=new PdfPTable(2);PdfPCell cell = new PdfPCell (new Paragraph ("Rose India"));cell.setColspan (2);cell.setHorizontalAlignment (Element.ALIGN_CENTER);cell.setBackgroundColor (new Color (128, 200, 128));cell.setPadding (10.0f);table.addCell (cell);cell = new PdfPCell (new Paragraph ("Name"));cell.setHorizontalAlignment (Element.ALIGN_CENTER);cell.setBackgroundColor (new Color (255, 200, 0));cell.setPadding (10.0f);table.addCell (cell);cell = new PdfPCell (new Paragraph ("Place"));cell.setHorizontalAlignment (Element.ALIGN_CENTER);cell.setBackgroundColor (new Color (255, 200, 0));cell.setPadding (10.0f);table.addCell (cell);table.addCell("NewsTrack");table.addCell("Delhi");table.addCell("RoseIndia");table.addCell("Delhi");document.add(table);document.close();}}

The output of the program is given below:

pdf section

In this program we are going to tell you how you can divide a pdf file into sections irrespective of the fact whether it exists or not.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/libof your web application. The packages we were talking about are java.io.* for input output, java.awt.*and com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named sectionPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). Make a Chapter and inside the constructor of Chapter create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides create a Section object and add the section to the previously created Chapter and add the Paragraph to the previously created Section. Inside the constructor of Paragraph we have passed one String. Add the Chapter object to the previously created Document. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

 

The code of the program is given below:

import java.io.*;import java.awt.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;public class sectionPDF{public static void main(String arg[])throws Exception{Document document=new Document();PdfWriter.getInstance(document,new FileOutputStream("sectionPDF.pdf"));document.open();Chapter chapter=new Chapter(new Paragraph("Chapter1"),1);Section section1=chapter.addSection(new Paragraph("Section1"));section1.add(new Paragraph("Rose India"));Section section2=chapter.addSection(new Paragraph("section2"));section2.add(new Paragraph("roseinia.net"));document.add(chapter);document.close();}}

The output of the program is given below:

pdf chapter

As we all have seen a pdf file, in the pdf file there are too many chapter. Do you want to make them by your own. In this program we are going to tell you how you can make different chapters in pdf file irrespective of the fact whether it exists or not.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program.

Now create a file named chapterPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). Make a Paragraph object and inside the constructor of the Paragraph pass a String. Now make a Chapter and inside the constructor of Chapter pass aparagraph object that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides. Add the Chapter object to the previously createdDocument. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

 

The code of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;import java.awt.*;public class chapterPDF{public static void main(String arg[])throws Exception{

Document document=new Document();PdfWriter.getInstance(document,new FileOutputStream("chapterPDF.pdf"));document.open();Paragraph paragraph=new Paragraph("Rose-Java");Paragraph paragraph1=new Paragraph("Rose-Java1");Paragraph paragraph2=new Paragraph("Rose-Java2");Paragraph paragraph3=new Paragraph("Rose-Java3");Paragraph paragraph4=new Paragraph("Rose-Java4");Paragraph paragraph5=new Paragraph("Rose-Java5");Chapter chapter=new Chapter(paragraph,0);Chapter chapter1=new Chapter(paragraph1,1);Chapter chapter2=new Chapter(paragraph2,2);Chapter chapter3=new Chapter(paragraph3,3);Chapter chapter4=new Chapter(paragraph4,4);Chapter chapter5=new Chapter(paragraph5,5);document.add(chapter);document.add(chapter1);document.add(chapter2);document.add(chapter3);document.add(chapter4);document.add(chapter5);document.close();}}

The output of the program is given below:

pdf list

In this program we are going to know how we can make a list in a pdf file irrespective of the fact whether it exists or not.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named listDemo. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). Create a numbered List with 30 point field for the numbers. Add the ListItem to the previously created list. Now  add a separator. Create a symboled List with a 30 point field for the symbols. Again add the ListItem to the previously created list.  At last add the list to the document. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance.

 

The code of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;public class listDemo{public static void main(String a[])throws Exception {Document doc=new Document();PdfWriter.getInstance(doc,new FileOutputStream("listDemo.pdf"));doc.open();//Create a numbered List with 30-point field for the numbers.List list=new List(true,30);list.add(new ListItem("First List"));list.add(new ListItem("Second List"));list.add(new ListItem("Third List"));doc.add(list);//Add a separator.doc.add(Chunk.NEWLINE);//Create a symboled List with a 30-point field for the symbols.list=new List(false,30);list.add (new ListItem ("Orange"));list.add (new ListItem ("Apple"));

list.add (new ListItem ("Cherry"));list.add (new ListItem ("Banana"));// Add the list to the document.doc.add (list);doc.close ();}}

The output of the program is given below:

itext chunk

In this free tutorial we are going to tell you about chunk in iText.  iText is a framework for creating pdf files in java. A Chunk is the smallest significant part of the text that can be added to a document. All the contents of the Chunk are of the same font, fontsize, style, color, etc. In the following sections you will know more about the Chunk functionality. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. Without this jar file our application will not run. The packages which we need to import arejava.io.* for input output, java.awt.* package, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named chunkPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file for you by that name.

Now open the document by document.open(). Now make a object of the Chunk class. Use the methodsetUnderline() which takes two argument. This method will draw a line that has the length of the Chunk, the first parameter points to the thickness which positioned at -2f above the baseline of theChunk. The method setBackground(Color.red) lets you define the color of the line. Now add the chunk to the previously created Document. At last closes the document by using thedocument.close(). The closing of the document is important because it flushes and closes theOutputStream instance. 

The code of the program is given below:

import java.io.*;import java.awt.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;

public class chunkPDF{  public static void main(String arg[])throws Exception{  Document document=new Document();  PdfWriter.getInstance(document,new FileOutputStream("chunkPDF.pdf"));  document.open();  Chunk chunk=new Chunk("Welecome To RoseIndia.");  chunk.setUnderline(+1f,-2f);  Chunk chunk1=new Chunk("RoseIndia.net");  chunk1.setUnderline(+4f,-2f);  chunk1.setBackground(Color.red);  document.add(chunk);  document.add(chunk1);  document.close();}}

The output of the program is given below:

combine two pdf files

In this program we are going to tell you how you can read a pdf file and combine more than one pdf into one. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/libof your web application. The packages and classes we are talking about are java.io.* for input output,com.lowagie.text.pdf.Document, com.lowagie.text.Rectangle, com.lowagie.text.pdf.BaseFonti,com.lowagie.text.pdf.PdfContentByte, com.lowagie.text.pdf.PdfImportedPage, com.lowagie.text.pdf.PdfWriter and com.lowagie.text.pdf.PdfReader. 

Now create a file named Comboine2page. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

First of all create a PdfReader object for reading the pdf file. By the object of PdfReader we can get the number of pages in the pdf file. Now make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. 

Now open the document by document.open(). By the object of the PdfWriter we get the reference of PdfContentType. It will be obtained by adding the method getDirectContent() to the previously generated PdfWriter object. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.FileOutputStream;import com.lowagie.text.Document;import com.lowagie.text.Rectangle;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.pdf.PdfContentByte;import com.lowagie.text.pdf.PdfImportedPage;import com.lowagie.text.pdf.PdfReader;import com.lowagie.text.pdf.PdfWriter;

public class Comboine2page {  public static void main(String[] args)throws Exception {  System.out.println("Combines two page");      PdfReader reader = new PdfReader("HelloWorldPdf.pdf");    int n = reader.getNumberOfPages();   Rectangle psize = reader.getPageSize(1);  float width = psize.height();  float height = psize.width();   Document document = new Document(new Rectangle(width, height));   PdfWriter Pdfwriter = PdfWriter.getInstance(document, new FileOutputStream("combine2Page1.pdf")); document.open();   PdfContentByte cb = Pdfwriter.getDirectContent(); int i = 0; int p = 0; while (i < n) { document.newPage(); p++; i++; PdfImportedPage page1 = Pdfwriter.getImportedPage(reader, i); cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120); if (i < n) { i++; PdfImportedPage page2 = Pdfwriter.getImportedPage(reader, i); cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2 + 60, 120); } BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 19); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 2) + (n % 2 > 0? 1 : 0)), width / 2, 40, 0); cb.endText(); } document.close();    }

}

 

The output of the program is given below:

hello world pdf

In this program we are going to tell you how you can write a simple "Hello World" word in a pdf, rtf and html file irrespective of the fact whether it exists or not.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/libof your web application. The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program. The package com.lowagie.text.html.HtmlWriter will help you to write the content in Html file, The package com.lowagie.text.pdf.PdfWriter helps to write the content in the pdffile and com.lowagie.text.rtf.RtfWriter2 helps you to write in rtf format. 

Now create a file named HelloMultipleWorld. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. In our example we have created a three different writers that listen to the document. The different writers are for pdf, html and for rtf. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the paragraph to the document. For that create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph we have passed one String "Hello World" . Now we need to make the references,  we add the references, but only  to the HTML page. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;

import com.lowagie.text.Anchor;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.html.HtmlWriter;import com.lowagie.text.pdf.PdfWriter;import com.lowagie.text.rtf.RtfWriter2;

public class HelloMultipleWorld {

  public static void main(String[] args) {

  System.out.println("Hello World in PDF, RTF and HTML");

    Document document = new Document();  try {    PdfWriter pdf = PdfWriter.getInstance(document,  new FileOutputStream("differentWritersdPdf.pdf"));  RtfWriter2 rtf = RtfWriter2.getInstance(document,  new FileOutputStream("differentWritersRtf.rtf"));  HtmlWriter html = HtmlWriter.getInstance(document,  new FileOutputStream("differentWritersdHtml.html"));

    document.open();    document.add(new Paragraph("Hello World"));    Anchor pdfRef = new Anchor("see Hello World in PDF.");  pdfRef.setReference("./HelloWorldPdf.pdf");

  Anchor rtfRef = new Anchor("see Hello World in RTF.");  rtfRef.setReference("./HelloWorldRtf.rtf");        pdf.pause();  rtf.pause();  document.add(pdfRef);  document.add(Chunk.NEWLINE);  document.add(rtfRef);  pdf.resume();  rtf.resume();    } catch (DocumentException de) {  System.err.println(de.getMessage());  } catch (IOException ioe) {  System.err.println(ioe.getMessage());  }

    document.close();  }}

The output of  the program is given below:

How to write hello world by using Multiple Writers in pdf

In this program we are going to tell you how you can write a simple "Hello World" word in a pdf, rtf and html file irrespective of the fact whether it exists or not.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program. The

package com.lowagie.text.html.HtmlWriter will help you to write the content in Html file, The package com.lowagie.text.pdf.PdfWriter helps to write the content in the pdffile and com.lowagie.text.rtf.RtfWriter2 helps you to write in rtf format. 

Now create a file named differentWritersMultiple. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. In our example we have created a three different writers that listen to the document. The different writers are for pdf, html and for rtf. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the paragraph to the document. For that create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph we have passed one String "Hello World" . Now we need to make the references,  we add the references, but only  to the HTML page. At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;

import com.lowagie.text.Anchor;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.html.HtmlWriter;import com.lowagie.text.pdf.PdfWriter;import com.lowagie.text.rtf.RtfWriter2;

public class differentWritersMultiple {

  public static void main(String[] args) {

  System.out.println("Hello World in PDF, RTF and HTML");

    Document document = new Document();  try {    PdfWriter pdf = PdfWriter.getInstance(document,  new FileOutputStream("differentWritersdPdf.pdf"));  RtfWriter2 rtf = RtfWriter2.getInstance(document,  new FileOutputStream("differentWritersRtf.rtf"));  HtmlWriter html = HtmlWriter.getInstance(document,  new FileOutputStream("differentWritersdHtml.html"));

    document.open();    document.add(new Paragraph("Hello World"));    Anchor pdfRef = new Anchor("see Hello World in PDF.");  pdfRef.setReference("./HelloWorldPdf.pdf");  Anchor rtfRef = new Anchor("see Hello World in RTF.");  rtfRef.setReference("./HelloWorldRtf.rtf");        pdf.pause();  rtf.pause();  document.add(pdfRef);  document.add(Chunk.NEWLINE);  document.add(rtfRef);  pdf.resume();  rtf.resume();    } catch (DocumentException de) {  System.err.println(de.getMessage());  } catch (IOException ioe) {  System.err.println(ioe.getMessage());  }

    document.close();  }}

The output of  the program is given below:

Download this program.

pdf system

In this example we are going to see how the data which we have in the pdf file will be printed on the command prompt. After going through this example you will be able to write the pdf data on the command prompt, just go through this tutorial properly. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output,com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and usepdf file in our program.

Now create a file named helloSystemPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Paragraph. Now make a object of class Document. The Documentdescribes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. Inside the constructor of theDocument pass the PageSize. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name. In this program we are strictly specifying where the output has to be printed. We are printing the output on the command prompt. 

Now open the document by document.open(). After that add the content to the document. Create aParagraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph pass any  String.  To set the margins

of the pdf file call the method setMargins and add it to Document object. The method setMarginMirroring(true), the parameter passed in the method allows you to margin mirroring. Now add the Paragraph object to the previously created Document object. At last closes the document by using the document.close().The closing of the document is important because it flushes and closes the OutputStream instance. 

To run this program firstly compile the program by using javac. The output of the file will be given by thejava command. 

The code of the program is given below:

import java.io.*;import java.awt.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;

public class helloSystemPDF{public static void main(String arg[])throws Exception{ System.out.println("Hello RoseIndia");Paragraph paragraph=new Paragraph();  Document document = new Document(PageSize.A4, 36, 72, 108, 180);PdfWriter.getInstance(document,System.out);PdfWriter.getInstance(document,new FileOutputStream("helloSystemPDF.pdf"));document.open();  document.add(new Paragraph("Margin--->>roseinia.net"));document.setMargins(180,108,71,10);document.add(new Paragraph("You can visit roseindia.net for more java tutorials"));paragraph.add("Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net.");  document.setMarginMirroring(true);document.add(new Paragraph("Starting on the next page, the margins will be mirrored."));  document.add(paragraph);document.close();}}

The output of the program is given below:

pdf landscape

In this free tutorial we are going to tell you about landscape portrait in iText.  iText is a framework for creating pdf files in java. In this tutorial you will know more about the landscape portrait. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. Without this jar file our application will not run. The packages which we need to import are java.io.* for input output, java.awt.*package, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named landscapeportraitPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Document. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file for you by that name.

Now open the document by document.open(). Create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both

sides. Inside the constructor of Paragraph we have passed a String . Now use the method setPageSize to set the page size of the pdf file. To make a new page use newPage() method and add it to previously created Document.  At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.*;

import com.lowagie.text.*;import com.lowagie.text.pdf.*;

public class landscapeportraitPDF{

public static void main(String arg[])throws Exception{Document document = new Document(PageSize.A4.rotate());PdfWriter.getInstance(document,new FileOutputStream("landscapeportraitPDF.pdf"));document.open();document.add(new Paragraph("landscape format, just make the height smaller than the width."));document.setPageSize(PageSize.A4);document.newPage();document.add(new Paragraph("This is portrait again"));

document.close();}}

The output of the program is given below:

pdf margin

In this example we are going to see how we can give a margin to our pdf file. Margin gives the file good look and feel. After going through this example you will be able to give the margins to your own made pdf file. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. The packages we were talking about are java.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named marginPDF. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of class Paragraph. Now make a object of class Document. The Documentdescribes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. Inside the constructor of theDocument pass the PageSize. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). After that add the content to the document. Create aParagraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph pass any  String.  To set the margins of the pdf file call the method setMargins and add it to Document object. The method setMarginMirroring(true), the parameter passed in the method allows you to margin mirroring. Now add the Paragraph object to the previously created Document object. At last closes the document by using the document.close().The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.*;import java.awt.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;

public class marginPDF{  public static void main(String arg[])throws Exception{  Paragraph paragraph=new Paragraph();    Document document = new Document(PageSize.A4, 36, 72, 108, 180);  PdfWriter.getInstance(document,new  FileOutputStream("marginPDF.pdf"));  document.open();    document.add(new Paragraph("Margin--->>roseinia.net"));  document.setMargins(180,108,71,10);  document.add(new Paragraph("You can visit roseindia.net for more java tutorials"));  for (int i = 0; i < 200; i++) {    paragraph.add("Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net,Hello roseindia.net.");}  document.setMarginMirroring(true);  document.add(new Paragraph("Starting on the next page, the margins will be mirrored."));    document.add(paragraph);  document.close();}}

The output of the program is given below:

pdf Metadata

In this program we are going to know what is metadata in pdf. 

Metadata is data about something, whether it is an object, person, or any other data. The term has come to mean structured information that feeds into automated processes, and this is the most useful way to think about metadata. Metadata is useful  to the people and systems that keeps it, primarily within the context of a particular system. Metadata can take many forms, and metadata records can vary tremendously in richness, creating an array of  content management and economic models. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. Without this jar our application cannot run. The packages we were talking about arejava.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named Comboine2page. Remember the name of the file should be such that the reader can understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of Document class. This class describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. 

Now open the document by document.open(). Add the method addTitle() to the previously created Document, then also add subject, keywords, creator, author to the already created Document.  Create a Paragraph that houses a paragraph of text, tells the PDF document writer to ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph we have passed one String "Roseindia". At last closes the document by using the document.close(). The closing of the document is important because it flushes and closes the OutputStream instance. 

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.*;import com.lowagie.text.pdf.*;

public class MetaData {  public static void main(String[] args) throws Exception{  System.out.println("Meta data");    Document document = new Document();    PdfWriter.getInstance(document,new FileOutputStream("metaDataPdf.pdf"));  document.addTitle("Meta Data");  document.addSubject("This example explains how to add metadata.");  document.addKeywords("Rose India");  document.addCreator("My program using iText");  document.addAuthor("Rajesh Kumar");  document.open();  document.add(new Paragraph("Rose india"));  document.close();  }}

The output of the program is given below:

itext version

In this program we are going to find versionof  the iText jar file which is using to make a pdf file through the java program.

In this example we need iText.jar file, without this jar file we never compile our application . The packages we need to import is java.io.FileOutputStream, java.io.IOException, com.lowagie.text.Document, com.lowagie.text.DocumentException, com.lowagie.text.Paragraph, com.lowagie.text.Rectangle,com.lowagie.text.pdf.PdfWriter.

The java.io.FileOutputStream and java.io.IOException classes are used to create file and throw the exception, if there is any exception occurs while creating a file. The com.lowagie.text.Documentclass is used to create a document object,  com.lowagie.text.DocumentException is used to throw exception if there is any exception. com.lowagie.text.Paragraph classes is used to make paragraph , com.lowagie.text.Rectangle classes is used to create object of Rectangle class which will be used for setting the left, right, top, bottom margin, com.lowagie.text.pdf.PdfWriter class is used to write the document on a pdf file. 

To make the program for finding the iText version firstly we will make a class VersionPDF. Remember the name of the file should be such that, if any other person sees the example then just by seeing the name of the program he can understand what the program is going to do without seeing the code. 

Inside this class declare the main method which must throws Exception. After that follow the simple steps find the version:

1. Create Rectangle object and set the length and width of the page.

2. Create Document object and inside the constructor of Document pass pageSize and four integer which set the left,right,top and bottom margin. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

3. Create object of PrintWiter to add the value to document for this we need to use getInstance()method, in which we pass two arguments document andFileOutputStream("VersionPDF.pdf").

4. Open the document.5. Create a Paragraph that hold a paragraph of text, tells the PDF document writer to

ensure. The Paragraph's text justify on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph we have to pass a String .Create paragraph and add it to the document.

6. To find the version of iText use getVersion() method which is static. It is a method ofDocument.

7. Add next paragraph.8. Close the document. The closing of the document is important because it flushes and

closes theOutputStream instance. 

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.Rectangle;import com.lowagie.text.pdf.PdfWriter;public class VersionPDF { public static void main(String[] args)throws Exception {System.out.println("Example of a Find version of iText");    Rectangle pageSize = new Rectangle(288, 720);  Document document = new Document(pageSize, 36, 18, 72, 72);PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("VersionPDF.pdf"));   document.open();  document.add(new Paragraph("Version Of iText-->>Rose India"));  document.add(new Paragraph("This page was made using " +Document.getVersion()));    document.add(new Paragraph("-->>RoseIndai.net"));  document.close();  }}

The output of the program is given below:

pdf file measurement

In this program we are going to display the measurement of pdf pages through java program. First ad the value in the paragraphs then add it finally in the document.

In this example we need iText.jar file, without the jar file we never compile our application . The package we need to import is java.io.FileOutputStream, java.io.IOException, com.lowagie.text.Document, com.lowagie.text.DocumentException, com.lowagie.text.Paragraph, com.lowagie.text.Rectangle,com.lowagie.text.pdf.PdfWriter..

The java.io.FileOutputStream and java.io.IOException classes are used to create file and throw the exception, if there is any exception occurs while creating a file. The com.lowagie.text.Documentclass is used to create a document object, com.lowagie.text.DocumentException is used to throw exception if there is any exception. com.lowagie.text.Paragraph classes is used to make paragraph , com.lowagie.text.Rectangle classes is used to create object of Rectangle class which will be used for setting the left, right, top, bottom margin, com.lowagie.text.pdf.PdfWriter class is used to write the document on a pdf file,.

To make the program for changing the pdf version firstly we have to make a class MeasurementsPDF. Remember the name of the file should be such that, if any other person see the example then just by seeing the name of the program he can understand what the program is going to do without seeing the code. 

Inside this class declare the main method which must throws Exception. After that follow the simple steps find the version:

1. Create Rectangle object and set the length and width of the page.2. Create Document object and inside the constructor of Document pass pageSize and

four integer which set the left,right,top and bottom margin. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.

3. Create object of PrintWiter to add the value to document for. We use getInstance() method, in which we pass two arguments document and FileOutputStream("Measurements.pdf").

4. Open the document.5. Create a Paragraph that hold a paragraph of text, tells the PDF document writer to

ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously createdDocument. Inside the constructor of Paragraph we pass a String .Create paragraph and add it into document .

6. Add the paragraph in the document.7. close the document. The closing of the document is important because it flushes and

closes theOutputStream instance. 

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfWriter;public class MeasureOfWidth {  public static void main(String[] args)throws Exception {  System.out.println("Example of measure Width of the chunk");  Document document = new Document();  PdfWriter.getInstance(document,new FileOutputStream("Width.pdf"));  document.open();  Chunk c = new Chunk("Rose India");  float w = c.getWidthPoint();  Paragraph p = new Paragraph("The width of the chunk: '");  p.add(c);  p.add("' is ");  p.add(String.valueOf(w));

  p.add(" points or ");  p.add(String.valueOf(w / 72f));  p.add(" inches.");  document.add(p);  document.add(c);  document.add(Chunk.NEWLINE);  c.setHorizontalScaling(0.5f);  document.add(c);  document.add(c);  document.close();  }}

The output of the program is given below:

 

Insert pages pdf

In this program we are going to insert a new blank pages in pdf  file  through java program.

In this example we need iText.jar file, without this jar file we never compile our application . The package we need to import is java.io.FileOutputStream, java.io.IOException, com.lowagie.text.Document, com.lowagie.text.DocumentException, com.lowagie.text.Paragraph, com.lowagie.text.Rectangle,com.lowagie.text.pdf.PdfWriter..

The java.io.FileOutputStream and java.io.IOException classes are used to create file and throw the exception, if there is any exception occurs while creating a file. The com.lowagie.text.Documentclass is used to create a document object,  com.lowagie.text.DocumentException is used to throw exception if there is any exception. com.lowagie.text.Paragraph classes is used to make paragraph , com.lowagie.text.Rectangle classes is used to create object of Rectangle class which will be used for setting the left, right, top, bottom margin, com.lowagie.text.pdf.PdfWriter class is used to write the document on a pdf file,.

To make the program for changing the pdf version firstly we will make a class NewpageInsertingPDF. Remember the name of the file should be such that, if any other person sees the example then just by seeing the name of the program he can understand what the program is going to do without seeing the code. 

Inside this class declare the main method which must throws Exception. After that follow the simple steps find the version:

1. Create Rectangle object and set length and width of the page.2. Create Document object and inside the constructor of Document pass pageSize and

four integer which set the left,right,top and bottom margin. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.

3. Create object of PrintWiter to add the value to document for this we use getInstance() method in which we pass two arguments document and FileOutputStream("NewPagePDF.pdf").

4. Open the document.5. Create a Paragraph that houses a paragraph of text, tells the PDF document writer to

ensure that the Paragraph's text is justified on both sides and adds the Paragraph to

the previously created Document. Inside the constructor of Paragraph we have passed a String .Create paragraph and add it into document .

6. Add the paragraph in the document.7. Use setPageEmpty(boolean pageEmpty) to make sure that page is added.

IfsetPageEmpty(false) then invoking newPage() after blank page will add a newPage().

8. close the document. The closing of the document is important because it flushes and closes theOutputStream instance. 

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.Rectangle;import com.lowagie.text.pdf.PdfWriter;public class NewpageInsetingPDF { public static void main(String[] args)throws Exception { System.out.println("Example of a PDF File  to blank pages and new page");   Rectangle pageSize = new Rectangle(288, 720);  Document document = new Document(pageSize, 36, 18, 72, 72);  PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("NewPagePDF.pdf"));  document.open();  document.add(new Paragraph("First Page-->>Rose India"));  document.add(new Paragraph("Next Two blank Pages"));    document.newPage();  writer.setPageEmpty(false);  document.newPage();  writer.setPageEmpty(false);    document.newPage();    document.add(new Paragraph("Last Page with some data-->>RoseIndai.net"));    document.close();  }}

The output of the program is given below:

change pdf version

In this program we are going to change  theversion of pdf file  through java program.

In this example we need iText.jar file, without this jar file we never compile our application . The package we need to import is java.io.FileOutputStream, java.io.IOException, com.lowagie.text.Document, com.lowagie.text.DocumentException, com.lowagie.text.Paragraph, com.lowagie.text.Rectangle,com.lowagie.text.pdf.PdfWriter,  import com.lowagie.text.pdf.PdfReader..

The java.io.FileOutputStream and java.io.IOException classes are used to create file and throw the exception, if there is any exception occurs while creating a file. The com.lowagie.text.Documentclass is used to create a document

object,  com.lowagie.text.DocumentException is used to throw exception if there is any exception. com.lowagie.text.Paragraph classes is used to make paragraph , com.lowagie.text.Rectangle classes is used to create object of Rectangle class which will be used for setting the left, right, top, bottom margin, com.lowagie.text.pdf.PdfWriter class is used to write the document on a pdf file,com.lowagie.text.pdf.PdfReader class provide  a getPdfVersion() method which used to find the version of pdf file.

To make the program for changing the pdf version firstly we will make a class ChaningVersionPDF. Remember the name of the file should be such that, if any other person sees the example then just by seeing the name of the program he can understand what the program is going to do without seeing the code. 

Inside this class declare the main method which must throws Exception. After that follow the simple steps find the version:

1. Create Rectangle object to set length and width of page.2. Create Document object and inside the constructor of Document pass pageSize and

four integer which set the left,right,top and bottom margin. The Document describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content.

3. Create object of PrintWiter to add the value to document for this we use getInstance() method in which we pass two arguments document andFileOutputStream("cahngingVersionPDF.pdf").

4. Used the setPdfVersion(PdfWriter.VERSION_1_2) to set the version of pdf. 5. Open the document.6. Create a Paragraph that houses a paragraph of text, tells the PDF document writer to

ensure that the Paragraph's text is justified on both sides and adds the Paragraph to the previously created Document. Inside the constructor of Paragraph we have passed a String .Create paragraph and add it into document .

7. Add the paragraph in the document.8. close the document. The closing of the document is important because it flushes and

closes theOutputStream instance. 9. Create object of PdfReader to read the pdf file.10. To get the version of pdf file use a method getPdfVersion(). It is method of 

PdfReader class.

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.Rectangle;import com.lowagie.text.pdf.PdfWriter;import com.lowagie.text.pdf.PdfReader;public class ChaningVersionPDF {public static void main(String[] args)throws Exception {System.out.println("Example of Changing the PDF version of a document"); Rectangle pageSize = new Rectangle(288, 720);Document document = new Document(pageSize, 36, 18, 72, 72);PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("changedVersionPDF.pdf"));writer.setPdfVersion(PdfWriter.VERSION_1_2);document.open();document.add(new Paragraph("Version Of PDF-->>Rose India"));document.add(new Paragraph("-->>RoseIndai.net"));document.close();PdfReader reader = new PdfReader("changedVersionPDF.pdf");System.out.println("Version of PDF:"+reader.getPdfVersion());}}

The output of the program is given below:

Concatenate two pdf files

In this program we are going to concatenate two pdf files into a  pdf file through java program. The all data of files  are concatenated intothe pdf. You can concentrate only two  files into a single file. 

In this example we need iText.jar file, without this jar file we never compile our application . The package we need to import are java.io. FileOutputStream,j com.lowagie.text.pdf.PdfCopy,com.lowagie.text.pdf.PdfReader,

The java.io.FileOutputStream  class is used to create file.T. com. lowagie.text.pdf.PdfCopy  class is used to make copies of  PDF documents and It extends PdfWriter. The com.lowagie.text.pdf.PdfReader class is used to read pdf from specified file. PdfReader class throws an IOException  and extends Objectclass.  

To make the program for concatenate pdf  files  firstly we will make a class Concanenate. Remember the name of the file should be such that, if any other person sees the example then just by seeing the name of the program he can understand what the program is going to do without seeing the code. The steps to execute this program:

1. Makes minimum two pdf file.2. Download the  iTex.jar file .3. Copy iText.jar into lib directory and Set the class.4.  Make or Download the source code of program.5. Put the source code and pdf files into same directory.6. Compile the source code.7. Press enter to execute your program. 

Inside this class declare the main method which must throws Exception. After that follow the simple steps find the version:

1. Makes two object of  PdfReader to read the files.2. Create object of  PdfCopyFields to create new pdf file where we copy two pdf files.3. add the two pdf into copy object by using addDocument() method.4.  close the copy object

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;

import com.lowagie.text.pdf.PdfCopyFields;import com.lowagie.text.pdf.PdfReader;public class Concatenate2PDF {public static void main(String[] args)throws Exception {System.out.println("Concatenate Two PDF"); PdfReader reader1 = new PdfReader("1PDF.pdf");PdfReader reader2 = new PdfReader("2PDF.pdf");PdfCopyFields copy = new PdfCopyFields(new FileOutputStream("concatenatedPDF.pdf"));copy.addDocument(reader1);copy.addDocument(reader2);copy.close();}}

Download file1 example.

Download file2 example.

The output of the program is given below:

Concatenate pdf files using command argument

In this program we are going to concatenate pdf files into a destination pdf files through java program. The all data of files  are concatenated intodestination pdf. You can concentrate two or more file into a single file. 

In this example we need iText.jar file, without this jar file we never compile our application . The package we need to import are

java.io. FileOutputStream,java.util.ArrayList,java.util.List,com.lowagie.text.Document,com.lowagie.text.pdf.PRAcroForm, com.lowagie.text.pdf.PdfCopy,com.lowagie.text.pdf.PdfImportedPage,com.lowagie.text.pdf.PdfReader,com.lowagie.text.pdf.SimpleBookmark.

The java.io.FileOutputStream  class is used to create file.The java.util.ArrayList and java.util.Listclasses are used to create object of an array list and a list.  The com.lowagie.text.Document class is used to create a document object, com. lowagie.text.pdf.PRAcroForm class extends DpfDictionary. The PRAcroForm class captures an AcroForm on input.

AcroForm is  a collection of fields that is used for gathering information interactively from the user. com. lowagie.text.pdf.PdfCopy  class is used to make copies of  PDF documents and It extends PdfWriter.The com.lowagie.text.pdf. PdfImportedPage class is used to import an page. The PdfImportedPage class extends PdfTemplate. The com.lowagie.text.pdf.PdfReader class is used to read pdf from specified file. PdfReader class throws an IOException  and extends Object class. The class  com.lowagie.text.pdf.SimpleBookmark is to create an instance of SimpleBookmark.  

To make the program for concatenate pdf  files  firstly we will make a class Concanenate. Remember the name of the file should be such that, if any other person sees the example then just by seeing the name of the program he can understand what the program is going to do without seeing the code. 

The steps to execute this program:

1. Makes minimum two pdf file.2. Download the  iTex.jar file .3. Copy iText.jar into lib directory and Set the class.4.  Make or Download the source code of program.5. Put the source code and pdf files into same directory.

6. Compile the source code.7. Pass the name of two (or more files) and  and destination pdf file name.8. Press enter to execute your program. 

Inside this class declare the main method which must throws Exception. After that follow the simple steps find the version:

1. First check the number of arguments passed in command line. If it is less than three then print "arguments: file1 [file2 ...] destfile" else  proceed  next.  

2. Create an object of ArrayList   and store the destination   file name an String object outFile.

3. Initialize document and PdfWriter null .4. Start a while loop upto destination file name.5. Read the file  from command argument and find number of pages.6. Store the file into an object boomarks 7. And add 8.  close the document

The code of the program is given below:

import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import com.lowagie.text.Document;import com.lowagie.text.pdf.PRAcroForm;import com.lowagie.text.pdf.PdfCopy;import com.lowagie.text.pdf.PdfImportedPage;import com.lowagie.text.pdf.PdfReader;import com.lowagie.text.pdf.SimpleBookmark;public class Concatenate { public static void main(String args[])throws Exception {  if (args.length < 2) { System.err.println("arguments: file1 [file2 ...] destfile");  }  else { System.out.println("PdfCopy example");   int pageOffset = 0; ArrayList master = new ArrayList(); int f = 0; String outFile = args[args.length-1]; Document document = null; PdfCopy  writer = null; while (f < args.length-1) {  PdfReader reader = new PdfReader(args[f]); reader.consolidateNamedDestinations();

 int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); master.addAll(bookmarks); } pageOffset += n; if (f == 0) {   document = new Document(reader.getPageSizeWithRotation(1));   writer = new PdfCopy(document, new FileOutputStream(outFile));   document.open();   }  PdfImportedPage page;  for (int i = 0; i < n; ) {  ++i; page = writer.getImportedPage(reader, i); writer.addPage(page);  } PRAcroForm form = reader.getAcroForm(); if (form != null)  writer.copyAcroForm(reader);  f++;  } if (!master.isEmpty())  writer.setOutlines(master);  document.close();   }  }}

Download file1 example.

Download file2 example.

The output of the program is given below:

Rotating image in the pdf file

In this program we are going to tell you how we can insert a image in a pdf file and rotate it irrespective of the fact whether it exists or not. If it exists, fine otherwise it will get automatically created. 

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. Without this .jar the application will not run. The packages we were talking about arejava.io.* for input output, com.lowagie.text.pdf.*, and com.lowagie.text.*. These two package will help us to make and use pdf file in our program.

Now create a file named imagesRotationPDF. Remember the name of the file should be such that the reader can easily  understand what the program is going to perform. Inside the class declare a main method inside which we are going to write the logic of our program.

Firstly make a object of Document class. This class describes a document's page size, margins, and other important attributes. It works as a container for a document's chapters, sections, images, paragraphs, and other content. 

Now create a document writer that writes the equivalent syntax for a document's content to a specificOutputStream. PdfWriter.getInstance() creates a PDF document writer that writes PDF syntax to concerned file by a FileOutputStream. If the pdf file doesn't exist then it will create a pdf file by that name.

Now open the document by document.open(). Image.getInstance() gets the image. After that add the content to the document. Add the paragraph and then add the image and set image.scalePercent (20.0f) and   image.setRotationDegrees (30.0f).at finally close the document.  

The code of the program is given below:

The output of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;public class imagesRotationPDF{ public static void main(String arg[])throws Exception{ Document document=new Document();PdfWriter.getInstance(document,new FileOutputStream("imagesRotationPDF.pdf"));document.open();Image image = Image.getInstance ("devi.jpg");document.add(new Paragraph("Original:"));document.add(image);image.scalePercent (20.0f);image.setRotationDegrees (30.0f); document.add(new Paragraph("After Scalable and Rotation"));document.add(image);

document.close();}}

Hello World' file from a servlet (PDF, HTML or RTF).

In this program we are going to tell you how we can create three file rtf,pdf and html files.And we are printing here Hello Word on PDF, HTML, And RTF formate.

To make a program over this, firstly we need to import some packages. Remember to make this program the first and foremost thing to remember is to place the iText.jar in WEB-INF/lib of your web application. Without this .jar the application will not run. The packages we were talking about arejava.io.* for input output, com.lowagie.text.pdf.*,import java.awt.*;import javax.servlet.*;import javax.servlet.http.*,import com.lowagie.text.*,import com.lowagie.text.rtf.*,import com.lowagie.text.html.*;

Now create a file named helloServletPDF which extends Httpservlet. Remember the name of the file should be such that the reader can easily  understand what the program is going to

perform. Inside the class declare a main method inside which we are going to write the logic of our program.

In doGet(HttpServletRequest req,HttpServletResponse res) create Document object  and getParameter  passed by user. Then if parameter passed is equals to "rtf" then  set res.setContentType("text/rtf") and  writer  file on Clint side as with RtfWriter2.getInstance (document,res. getOutputStream()) or if "html" then  set res.setContentType("text/html") and  writer  file on Clint side as with HtmlWriter.getInstance (document,res. getOutputStream()) or if "pdf" then  set res.setContentType("application/pdf") and  writer  file on Clint side as with PdfWriter.getInstance (document,res. getOutputStream()). Finaly close the document.  

The code of the program is given below:

import java.io.*;import java.awt.*;import javax.servlet.*;import javax.servlet.http.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;import com.lowagie.text.rtf.*;import com.lowagie.text.html.*;

public class helloServletPDF extends HttpServlet{public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException {String str=req.getParameter("Text1");

Document document=new Document();

try{ if(str.equals("pdf")){res.setContentType("application/pdf");PdfWriter.getInstance(document,res.getOutputStream());}if(str.equals("rtf"))

{res.setContentType("text/rtf");RtfWriter2.getInstance(document,res.getOutputStream());}if(str.equals("html")){res.setContentType("text/html");HtmlWriter.getInstance(document,res.getOutputStream());}document.open();document.add(new Paragraph("Hello World"));}catch(DocumentException de) {de.printStackTrace();System.err.println("document: " + de.getMessage());}document.close();

}}The code of the program is given below:Index.Html<html><head><title>Example of servlet to display "Hello Word "on PDF,RTF and HTML format using iText</title></head><body><p><br><br><center><b>Enter Data Type Format (Only rtf,html or pdf)</b><br><form action="../helloServletPDF" method="GET" name="upform"><input type="text" name="Text1" ><br><input type="submit" name="Submit" value="Submit" ></form></center>

</body></html>

 

The output of the program is given below:

adding Under Lines

In this program we are going to tell you how you can add underlines. You can  any number of lines. This example gives you an idea ,how to make a java program to add a underline and you can also make program for making line on the text.In this example we create chunk and then we  use the setUnderLine(0.2f, -2f) method to add the line. There are an other chunk  object on which we use setUnderLine(0.5f, 3f).So you can create any number of chunk object and set line as your requirements. 

Code Description:

You can create line for chunk object to any position of the text .

To make a program you need to import some packages one more package: import java.awt.Color;

setUnderLine(float f,float f):We use the setUnderLine(float f,float f) method. Float values may be negative or positive depending on  where you want to fix the line for the text.

You can create line on small text .For this Chunk class is provided by iText API,s.

 The code of the program is given below:

import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfWriter;public class addUnderLines{ public static void main(String[] args) throws Exception{ System.out.println("Underline, Strike through,..."); Document document = new Document(); PdfWriter.getInstance(document,   new FileOutputStream("underLinesPdf.pdf")); document.open(); Chunk chunk = new Chunk("RoseIndia.net"); chunk.setUnderline(0.2f, -2f); Paragraph paragraph =   new Paragraph("The following chunk is "); paragraph.add(chunk); document.add(paragraph);

 Chunk chunk1 = new Chunk("roseindia.net"); chunk1.setUnderline(0.5f, 3f);   document.add(chunk1);   document.close();  }}

The output of the program is given below:

changing Background Color

In this program, we are going to tell you how you can change background color of a pdf file. You can change the background color of any chunk with the help ofsetBackground(Color color). 

In this example, we are creating a chunk then we use setBackground(Color color) to set the background color of that chunk. Here we pass the object of Color by using new Color(0xFF, 0xFF, 0x00). We are passing three hexadecimal values into the Color constructor. Which is used to create the object of the Color. 

Code Description:

setBackground(Color color): If you want to change the background color  then we use this method. This method is used to change the color of  chunk of text into the chunk object. We use this for set the background color of any text.

setTextRise(float):There are some methods that can be invoked on a Chunk. The method setTextRise(float f)is used to write the chunks  in super or subscript.

The code of the program is given below:

import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.FontFactory;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfWriter;public class changeBackgroundColor { public static void main(String[] args)throws Exception {  System.out.println("Background color"); Document document = new Document();  PdfWriter.getInstance(document,new FileOutputStream("cahngeBackgroundColor.pdf"));  document.open();  Chunk chunk =new Chunk("RoseIndia.net");   chunk.setBackground(new Color(0xFF, 0xFF, 0x00));  Paragraph paragraph = new Paragraph("The following chunk is ");  paragraph.add(chunk);  document.add(paragraph);  Chunk chunk2,chunk3;  chunk2 = new Chunk("RoseIndia.net");  chunk2.setBackground(new Color(0xb0, 0xb0, 0xb0));  document.add(chunk2);  chunk3 = new Chunk("RoseIndia.net");  chunk3.setTextRise(8);  chunk3.setBackground(new Color(0xFF, 0xDE, 0xAD));  document.add(chunk3);    document.add(paragraph);  document.close();  }}

The output of the program is given below:

changing Chunk Color

In this program we are going to tell you how you can change the color of chunk color .This example tell you how you can change the color of chunk and how you can add fonts and gives the size of the fonts.

Code Description:

In this Example we create the object of the paragraph .In this paragraph we add the chunk and  set the font by using the getFont(fontname,size,coloor ).The getFont() method is a method of FontFactory. We are changing the color of the chunk by passing the color values into the getFont() method.

FontFactory:The class FontFactory is used to create a Font. It is easiest way to get a new Font object. FontFactory has an API with methods that can construct a Font object of any kind. The constructed font is in a uniform way. There are some CID fonts which are not in it.  getFont:The getFont() method is used to gets the current font for the window.The getFont method is used to get Font-Object from FontFactory class.

Font.DEFAULTSIZE:This is used to set the default size.

Color(0xFF, 0x00, 0x00):This constructor is used to create the object of the color. We pass three hexadecimal values to which gives  the combination of colors. 

Font.BOLD:This is used to set text BOLD. 

Font.ITALIC:This is used to set text ITALIC.

FontFactory.HELVETICA:This is used to set text style HELVETICA.

The code of the program is given below:

import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Font;import com.lowagie.text.FontFactory;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.pdf.PdfWriter;public class changeChunkColor {  public static void main(String[] args) throws Exception  {  System.out.println("FontColor");  Document document = new Document();  PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("changeChunkColor.pdf"));  document.open();  Paragraph paragraph;  paragraph = new Paragraph("Roses");  paragraph.add(new Chunk("india", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.BOLD, new Color(0xFF, 0x00, 0x00))));  document.add(paragraph);  paragraph = new Paragraph("India");  paragraph.add(new Chunk(".net", FontFactory.getFont(FontFactory.HELVETICA, Font.DEFAULTSIZE, Font.ITALIC, new Color(0x00, 0x00, 0xFF))));  document.add(paragraph);    document.close();  }}

The output of the program is given below:

Create Multiple Lines 

iText APIs provides facility to  make the lines. In this program we are going to tell you how you can make single  lines and how you can make multiple lines on the text in pdf files. You can make multiple lines and also make colorful lines by using this example.

In this example we are making an object of Chunk. Then set under line, using setUnderLine( float f, float f) method. For new line we use Chunk.NEWLINE. We are also creating colorful lines.

Code Description:

Following methods are used for creating multiple and colorful lines:

Chunk.NEWLINE:This method is used to a make new line.

Chunk.setUnderLine(Color color, float thickness, float thicknessmul, float y, float ymul, int cap):This method is used to create a colorful line. You can set thickness and y position of the text where the line will be seen. When you increase it's value then the line will be seen towards the top of the text and the line will be seen towards the bottom of the text when you decrease the value. You can understand better changing it's value. Inside the method, parameter thicknessmul is used for setting line thickness according to the font size. You can also set ypositionmul for setting the line position on text towards the top or bottom. It is also a multiplication factor with the font size. Integer type value cap is used to set the line at beginning and ending point of the line.

PdfContentByte.LINE_CAP_PROJECTING_SQUARE:This is projecting square cap. The stroke continues beyond the endpoint of the line. It's value is equal to 2 that is of integer type.

PdfContentByte.LINE_CAP_BUTT:This is Butt Cap. This stoke is squared off the endpoint of line. There is no projection beyond the end of the line. It's value is equal to 0 that is of integer type.

PdfContentByte.LINE_CAP_ROUND:This is Round Cap. A semicircular arc with a diameter equal to the width is drawn around the endpoint and filled in. It's value is 1 that is of integer type.

The code of the program is given below:

import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.FontFactory;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfContentByte;import com.lowagie.text.pdf.PdfWriter;public class Lines {  public static void main(String[] args)throws Exception {  System.out.println("Underline, Strike through,...");    Document document = new Document();    PdfWriter.getInstance(document,new FileOutputStream("Linespdf.pdf"));  document.open(); Chunk underlined = new Chunk("Rose India-->>underlined");  underlined.setUnderline(0.2f, -2f);  Paragraph p = new Paragraph("The following chunk is ");  p.add(underlined);  document.add(p);  Chunk strikethru = new Chunk("Rose India-->>Strike");  strikethru.setUnderline(0.5f, 3f);    document.add(strikethru);  document.add(Chunk.NEWLINE);  document.add(Chunk.NEWLINE);  document.add(Chunk.NEWLINE);  Chunk c;  c = new Chunk("rose India-->>Multiple lines");  c.setUnderline(new Color(0xFF, 0x0f, 0x0f), 0.0f, 0.3f, 0.0f, 0.4f, PdfContentByte.LINE_CAP_ROUND);  c.setUnderline(new Color(0x0f, 0xFF, 0x0f), 5.0f, 0.0f, 0.0f, -0.5f, PdfContentByte.LINE_CAP_PROJECTING_SQUARE);  c.setUnderline(new Color(0x00, 0x0f, 0xFF), 0.0f, 0.2f, 15.0f, 0.0f, PdfContentByte.LINE_CAP_BUTT);  document.add(c);  document.add(Chunk.NEWLINE);  document.add(Chunk.NEWLINE);  document.add(Chunk.NEWLINE);  c = new Chunk("Multiple lines", FontFactory.getFont(FontFactory.HELVETICA, 24));  c.setUnderline(new Color(0xFF, 0x0f, 0x00), 0.0f, 0.3f, 0.0f, 0.4f, PdfContentByte.LINE_CAP_ROUND);  c.setUnderline(new Color(0x0f, 0xF0, 0x00), 5.0f, 0.0f, 0.0f, -0.5f,

 PdfContentByte.LINE_CAP_PROJECTING_SQUARE);  c.setUnderline(new Color(0x0f, 0x0f, 0xFF), 0.0f, 0.2f, 15.0f, 0.0f, PdfContentByte.LINE_CAP_BUTT);  document.add(c);  document.close();  }}

The output of the program is given below:

Creating Multiple Lists

In this program we are going to tell you how you can make lists into a pdf files. You can make lists and also make  sublist You can makeordered list or symbolic list. iText API,s  provides facility to make list. List may be ordered  or list may be unordered. Depending on our requirement we can create both types of list. We can add a symbols as list. 

Code Description:

There some package used in the program for the purpose. These are as follows:

1. import com.lowagie.text.Listt;2. import com.lowagie.text.Listitem;

List:   List class is used to create the object of the list. In List constructor we can pass two arguments List(boolean value,sizeOfList).If Boolean value is true then the list is ordered. If

we want to make unsolder list or we want to add a symbol  then the Boolean value must be false.

ListItem: ListItem class is used to add the items.

setListSymbol(): This method is used to set the symbol for list. We can pass two arguments with in setListsymbol() method.The deatails are:

1.public void setListSymbol(Chunk symbol)

In this the parameters passed are chunk.2.public void setListSymbol(String symbol)

In this the parameters passed are string. This is  a shortcut for setListSymbol(Chunk symbol).

setFirst(Char c):   This method is used to set the starting value of unordered list. 

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;

import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Font;import com.lowagie.text.FontFactory;import com.lowagie.text.List;import com.lowagie.text.ListItem;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfWriter;public class Lists  {public static void main(String[] args)throws Exception   { System.out.println("Create List object"); Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("lists.pdf")); document.open();  List list = new List(true, 20);

  list.add(new ListItem("First line"));  list.add(new ListItem("The second "));  list.add(new ListItem("Third line"));  document.add(list);  document.add(new Paragraph("Tutorials Provided By Roseindia.net"));  ListItem listItem;  list = new List(true, 15);  listItem = new ListItem("Core Java", FontFactory.getFont   (FontFactory.TIMES_ROMAN, 13)); listItem.add(new Chunk(" by rose india", FontFactory.getFont(   FontFactory.TIMES_ROMAN, 13, Font.ITALIC))); list.add(listItem); listItem = new ListItem("J2EE", FontFactory.getFont  (FontFactory.TIMES_ROMAN, 12)); listItem.add(new Chunk(" by rose india",   FontFactory.getFont(FontFactory.TIMES_ROMAN, 13, Font.ITALIC))); list.add(listItem); listItem = new ListItem("JSP", FontFactory.getFont   (FontFactory.TIMES_ROMAN, 12)); listItem.add(new Chunk(" by rose india", FontFactory.getFont(  FontFactory.TIMES_ROMAN, 13, Font.ITALIC))); list.add(listItem); document.add(list); Paragraph paragraph = new Paragraph("Some open source project"); list = new List(false, 10);  list.add("chat server");  list.add("pie chart");  list.add("online shopping"); paragraph.add(list); document.add(paragraph); document.add(new Paragraph("Some iText Example"));  list = new List(false, 20);list.setListSymbol(new Chunk("\u2021", FontFactory.getFont(   FontFactory.HELVETICA, 21, Font.BOLD))); listItem = new ListItem("Generates a simple 'Hello World' PDF"+"    file");  list.add(listItem);  List sublist;

  sublist = new List(false, true, 10);sublist.setListSymbol(new Chunk("", FontFactory.getFont(  FontFactory.HELVETICA, 7)));  sublist.add("Creating Paragraph using iText");  sublist.add("Creating Section using iText");  sublist.add("Creating A4 PDF using iText.");  sublist.add("Create size(509,50,50,50) A4 PDF Using iText");  list.add(sublist);  listItem = new ListItem("Craeting  table object using iText");    list.add(listItem);  sublist = new List(false, true, 10);  sublist.setFirst('a');sublist.setListSymbol(new Chunk("", FontFactory.getFont(  FontFactory.HELVETICA, 7)));  sublist.add("Creating  list object using iText ");  sublist.add("Hotel New Hampshire");  sublist.add("Creating  list object using iText ");  sublist.add("Creating  list object using iText "); list.add(sublist); listItem = new ListItem("Creating  list object using iText "); list.add(listItem); sublist = new List(false, true, 10); sublist.setListSymbol(new Chunk("", FontFactory.getFont(  FontFactory.HELVETICA, 7))); sublist.add("Creating  list object using iText "); sublist.add("Creating  list object using iText "); sublist.add("Creating  list object using iText "); sublist.add("Creating  list object using iText "); list.add(sublist); document.add(list);   document.close();  }}

The output of the program is given below:

different Font

In this program, we are going to give you an example that helps you for the procedure of using various fonts in a pdf document. Here,add(object) method of the Paragraphclass is used to add text and it's font name with size.

Code Description:

The following method and fields are used for creating multiple colorful fonts.

add(Chunk chunk,Font font): The add method is used to add the chunk text and name of font with font size.

Following are some font name used in the given program:

1. Font.ZAPFDINGBATS2. Font.TIMES_ROMAN3. Font.SYMBOL 

The code of the program is given below:

import java.io.*;import com.lowagie.text.*;import com.lowagie.text.pdf.*;public class exampleOfDifferentFonts{  public static void main(String[] args) throws Exception{    Document document = new Document();  PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("differentfontspdf.pdf"));  document.open();  Paragraph p = new Paragraph();  p.add(new Chunk("This text is in Times Roman. ",  new Font(Font.TIMES_ROMAN, 12)));  p.add(new Chunk("This is ZapfDingbats: ", new Font(Font.ZAPFDINGBATS, 12)));  p.add(new Chunk(". This is font Symbol: ", new Font(Font.TIMES_ROMAN, 12)));  p.add(new Chunk("This text is in Times Roman.", new Font(Font.SYMBOL, 12)));  document.add(new Paragraph(p));  document.close();  }}

The output of the program is given below:

Font Selection

In this section, you will learn how to design some text according to you. That means if you want to keep some text in different font like "Times TIMES_ROMAN", "ZAPFDINGBATS" etc. particularly. When you create a pdf through the Java Program then you have to decide the specific font name for the specific text from the pdf content and the font name has to be mentioned in the java code during the creation of the pdf whether it is static (hard-coded) or dynamic.

Here, you will learn by trying the following example that is given ahead. This example uses the static font name for the pdf content particularly by using addFont() method of the FontSelector class.

Code Description:

To select  font   iText provides a constructor named as FontSelector() and  the addFont() method of theFontSelector class to add the fonts for selecting and change it's font by the specified font name.

There some package used in the program for the purpose. These are as follows:

1. import com.lowagie.text.Font;2. import com.lowagie.text.pdf.FontSelector;3. import com.lowagie.text.Phrase; 

In this example, we have taken a String type variable input that is initialized with some text and Unicode. The input variable is passed through the process() method of the FontSelector class for applying the specified font name and size on the pdf content. The Unicode is converted into symbol after processing.

All the APIs used in the code of the program are derived here:

FontSelector: This is the class of iText api used for selecting fonts for the text it extends Object class.FontSelector class comes under com.lowagie.text.pdf package.

addFont(Font f): The addFont(Font f) method is used to add font into font selector object its return type is void. 

process(String input) method: The process(String input) is use to process the font on the text .Its return type is Phrase.

Phrase: The class Phrase is used as render of process. This class comes under the com.lowagie.text.Phrase.

The code of the program is given below:

import java.io.FileOutputStream;import com.lowagie.text.Document;import com.lowagie.text.Font;import com.lowagie.text.Paragraph;import com.lowagie.text.Phrase;import com.lowagie.text.pdf.FontSelector;import com.lowagie.text.pdf.PdfWriter;public class FontSelection {   public static void main(String[] args)throws Exception {    Document document = new Document();  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("fontselection.pdf"));  document.open();   String input = "Rose india is a  \u275d java \u275e site.   Roseindia provide  "+ "online \u2798 tutorial  \u279a of core java,j2ee,jsp and strust.\n\n"  + "\u2766\u00a0\u0ea0\u039c\u03c7\u03bd\u03b9\u0dbd \u03b1\u03b5\u03d9\u0db4\u02b5, \u03b1\u03b5\u0311, \u03a0\u03b7\u03bb\u03b7\u03b1\u0db1\u03b4\u03b5\u02c9 \u0391\u03c7\u03b9\u03bb\u03b7\u03bf\u03c2";  FontSelector fontselector = new FontSelector();  fontselector.addFont(new Font(Font.TIMES_ROMAN, 12));  fontselector.addFont(new Font(Font.ZAPFDINGBATS, 12));  fontselector.addFont(new Font(Font.SYMBOL, 12));  Phrase ph = fontselector.process(input);  document.add(new Paragraph(ph));  document.close();    }}

The output of the program is given below:

Phrase

In this section, you will learn how to design some text according to our need by use of Phrase. We can format our pdf by using Phrase() constructor. We can add the text, Chunk and set the font size , name and style of the font.

Code Description:

All the APIs used in the code of the program are derived here:

Phrase: The class Phrase is used as render of process. This class comes under the com.lowagie.text.Phrase. The Phrase   extends ArrayList and it implements the TextElementArray . A Phrase is a series of Chunks.  A Phrase has a main Font, but some chunks within the phrase can have a Font that differs from the main Font. All the Chunks in a Phrase have the same leading.e.g.

// Passed as a parameter with the default leading = 16 Phrase phrase0 = new Phrase(25, "this is a phrase with leading 16");//Default Constructor Phrase phrase1 = new Phrase();//Passing String Phrase phrase2 = new Phrase("this is a phrase"); // Font is passed (explicitely or embedded in a chunk), the default leading = 1.5 * size of the font Phrase phrase3 = new Phrase("this is a phrase with a red, normal font Courier, size 12", FontFactory.getFont(FontFactory.COURIER, 12, Font.NORMAL, new Color(255, 0, 0)));//Passing Chunk Phrase phrase4 = new Phrase(new Chunk("this is a phrase"));

//Passing Chunk,Font with the default leading = 18 Phrase phrase5 = new Phrase(18, new Chunk("this is a phrase", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));

The code of the program is given below:

import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Font;import com.lowagie.text.FontFactory;import com.lowagie.text.Phrase;import com.lowagie.text.pdf.PdfWriter;public class Phrases {  public static void main(String[] args)throws Exception  {  System.out.println("Phrases");  Document document = new Document();  PdfWriter.getInstance(document, new FileOutputStream("Phrasespdf.pdf"));  document.open();  Phrase phrase0 = new Phrase();  Phrase phrase1 = new Phrase("This is a phrase\n");  Phrase phrase2 = new Phrase(25,"This is a phrase with leading 25. \n");  Phrase phrase3 = new Phrase("This is a phrase with a red, normal font Courier20",  FontFactory.getFont(FontFactory.COURIER, 20, Font.NORMAL,new Color(255, 0, 0)));Phrase phrase4 = new Phrase(new Chunk("(4) this is a phrase\n"));Phrase phrase5 = new Phrase(18,new Chunk("This is a phrase in Helvetica, bold, red and size 16 with a given leading of 18 points.\n",FontFactory.getFont(FontFactory.HELVETICA, 16,Font.BOLD, new Color(255, 0, 0))));  Chunk chunk = new Chunk(" This is a font: ");Phrase phrase6=new Phrase();

phrase6.add(chunk);  phrase6.add(new Chunk("Helvetica", FontFactory.getFont(FontFactory.HELVETICA, 12)));  phrase6.add(chunk);  phrase6.add(new Chunk("ZapfDingBats", FontFactory.getFont(FontFactory.ZAPFDINGBATS, 12)));  Phrase phrase7 = new Phrase("If you don't add a newline !");  document.add(phrase1);  document.add(phrase2);  document.add(phrase3);  document.add(phrase4);  document.add(phrase5);  document.add(phrase6);  document.add(phrase7);    document.close();  }}

The output of the program is given below:

Rendering Example

In this section, you will learn how to make a program by which you can make a render page. iText api provides a method setTextRenderMode(int mode,float strokeWidth,Color strokeColor).We can gives various color and int mode.   

Code Description: In this example we are using setTextRenderMode(int mode,float strokeWidth,Color strokeColor) method for define the strokeWidth , stokeColor and the mode. We are giving

more details about this method following:setTextRenderMode(int mode,Color strokeColor):  With the setTextRenderMode(int mode,float strokeWidth,Color strokeColor) we can change the outline of the text.We are passing here three parameters. First is  int  mode it can  be one of the following values:

1. PdfContentByte.TEXT_RENDER_MODE_FILL:The mode is used for  glyphs fontcolor, the strokeWidth and strokeColor don't play a role here.

2. PdfContentByte.TEXT_RENDER_MODE_STROKE: We will only see the outline of the glyphs, with the given strokeWidth and strokeColor.

3. PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE: The glyphs will be filled with the fontcolor and will have an outline with the given strokeWidth and strokeColor.

4. PdfContentByte.TEXT_RENDER_MODE_INVISIBLE: The glyphs will be invisible.

The second  one is stroke Color to set the  color of the text.

setTextRenderMode(int mode,float strokeWidth,Color strokeColor):  With the setTextRenderMode(int mode,float strokeWidth,Color strokeColor) we can change the outline of the text.We are passing here three parameters. First is  int  mode it can  be one of the following values:

1. PdfContentByte.TEXT_RENDER_MODE_FILL:The mode is used for  glyphs fontcolor, the strokeWidth and strokeColor don't play a role here.

2. PdfContentByte.TEXT_RENDER_MODE_STROKE: We will only see the outline of the glyphs, with the given strokeWidth and strokeColor.

3. PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE: The glyphs will be filled with the fontcolor and will have an outline with the given strokeWidth and strokeColor.

4. PdfContentByte.TEXT_RENDER_MODE_INVISIBLE: The glyphs will be invisible.

The second  strokeWidth and the thread one is stroke Color to set the width of and color of the text.

The code of the program is given below:

import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfContentByte;import com.lowagie.text.pdf.PdfWriter;public class exampleOFRendering   {  public static void main(String[] args)throws Exception  {  System.out.println("Example of Rendering");    Document document = new Document();  PdfWriter.getInstance(document,new FileOutputStream("Rendering.pdf"));  document.open();  Paragraph p = new Paragraph("Rose Text Rendering:");  document.add(p);  Chunk chunk = new Chunk("Rendering Test");  chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL, 100f, new Color(0xc0, 0x00, 0x0f));  document.add(chunk);  document.add(Chunk.NEWLINE);  chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.3f, new Color(0xFc, 0x00, 0x00));  document.add(chunk);  document.add(Chunk.NEWLINE);  chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE, 100f, new Color(0x0c, 0xF0, 0x00));  document.add(chunk);  document.add(Chunk.NEWLINE);  chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.3f, new Color(0x0c, 0x00, 0xF0));  document.add(chunk);  document.add(Chunk.NEWLINE);  Chunk bold = new Chunk("This looks like STROKE for Bold look");  bold.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f, new Color(0x0c, 0x00, 0x00));  document.add(bold);  document.close();  }}

The output of the program is given below:

Skewing Text 

In this section, you will learn how to make a program by which you can set skew in for chunk. iText api provides a method setSkew(float value1,float value2).This method is used in palce of italic font.   

Code Description:We can use the method setSkew(float alpha,float beta) in place of italic fonts (if we are using a font that doesn,t  support  Font.ITALIC). We take 0f as alfa and 12f as value for beta.Alfa is the angle of the baseline in degrees. In the example, we rotate some text 45f and -45f degrees. Beta is the square-angle on the baseline.

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfWriter;public class exampleOfSkew {  public static void main(String[] args)throws Exception  {  System.out.println("Example of Skew");  Document document = new Document();  PdfWriter.getInstance(document,new FileOutputStream("Skew.pdf"));  document.open();  Paragraph p = new Paragraph("Rose India.net");  document.add(p);  Chunk chunk = new Chunk("Rose India.net");

  chunk.setSkew(45f, 0f);  document.add(chunk);  document.add(Chunk.NEWLINE);  chunk.setSkew(0f, 45f);  document.add(chunk);  document.add(Chunk.NEWLINE);  chunk.setSkew(-45f, 0f);  document.add(chunk);  document.add(Chunk.NEWLINE);  chunk.setSkew(0f, -45f);  document.add(chunk);  document.add(Chunk.NEWLINE);  chunk.setSkew(16f, 35f);  document.add(chunk);  document.add(Chunk.NEWLINE);  Chunk italic = new Chunk("This looks like Font.ITALIC");  italic.setSkew(0f, 15f);  document.add(italic);    document.close();  }}

The output of the program is given below:

Creating the Subscript and Superscript 

In this section, you will learn how we can create  the subscript and superscript.  iText api provides a setTextrise(float value1) method to set the position of text. We can set  float value

as positive or negative .Positive value is used for  Superscript and negative value used for subscript.  

Code Description:The setTextRise(float f) method is used to set subscript and superscript. The more about setTextRise(float rise)  

setTextRise:public Chunk setTextRise(float rise)is used to sets the text displacement relative to the baseline. Positive values rise the text, negative values lower the text. It can be used to implement sub/superscript.  

One more method to getTextRise is:

getTextRisepublic float getTextRise()is used to gets the text displacement relatiev to the baseline.

 

The code of the program is given below:

import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.pdf.PdfWriter;

public class exampleOfSubSupScript {    public static void main(String[] args)throws Exception {  System.out.println("Sub- and Superscript");    Document document = new Document();  PdfWriter.getInstance(document,new FileOutputStream("SubSupScriptPDF.pdf"));  document.open();    Chunk c1,c2,c3;  c1 = new Chunk("Rose");  c1.setTextRise(7.0f);  document.add(c1);  c2 = new Chunk("India");  c2.setTextRise(3.0f);  document.add(c2);

  c3 = new Chunk(".net");  c3.setTextRise(-2.0f);  document.add(c3); document.close(); }}

The output of the program is given below:

Uses Of Chunk Object  

In this section, you will learn how we can change the background color  and how we can create  the subscript and superscript.  iText api provides a setTextrise(float value1) and setBackground(Color  color) method to set the position of text and to set the background. We can set  float value as positive or negative .Positive value is used for  Superscript and negative value used for subscript.  

Code Description:The setTextRise(float f) method is used to set subscript and superscript. The more aboutsetTextRise(float rise)  

setTextRise:public Chunk setTextRise(float rise)is used to sets the text displacement relative to the baseline. Positive values rise the text, negative values lower the text. It can be used to implement sub/superscript.  

One more method to getTextRise is:

getTextRisepublic float getTextRise()is used to gets the text displacement relatiev to the baseline.

The code of the program is given below:

import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.pdf.PdfContentByte;import com.lowagie.text.pdf.PdfWriter;public class UsesOfChunks   {public static void main(String[] args)throws Exception   {  System.out.println("The Chunk object");  Document document = new Document();  PdfWriter.getInstance(document,new FileOutputStream("Chunks.pdf"));  document.open();  Chunk chunk = new Chunk("Rose India");  chunk.setTextRise(6.0f);  chunk.setBackground(new Color(0xFc, 0xDc, 0xAc));  Chunk chunk3 = new Chunk(" roseindia.net ");  Chunk chunk2 = new Chunk("rose");  chunk2.setTextRise(-8.0f);  chunk2.setUnderline(new Color(0xFF, 0x0c, 0x0c), 3.0f, 0.0f, 3.0f, 0.0f,PdfContentByte.LINE_CAP_ROUND);  document.add(chunk);  document.add(chunk2);  document.add(chunk3);  document.close();  }}

The output of the program is given below:

Measure Of Width

In this section, you will learn how we can measure the width of the page.  iText api provides a getWidthPoint() to know the width of the page. The width will be in centimeters or inches.

Code Description:getWidthPoint():If we have a Chunk and we want to know it's width on a page then we can use the methodgetWidthPoint(). It gives us the width in points and we will have to do maths to know the width in inches or centimeters.setHorizontalScaling(float scale):With the method setHorizontalScaling(float scale), we can shrink (scale < 1.0f) or expand (scale > 1.0f) the Chunk. In the example we first print the Chunk in its actual size, then we print it twice at half its size (scale = 0.5f).

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Chunk;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfWriter;public class MeasureOfWidth {    public static void main(String[] args)throws Exception {  System.out.println("Example of measure Width of the chunk");  Document document = new Document();  PdfWriter.getInstance(document,new FileOutputStream("Width.pdf"));  document.open();  Chunk c = new Chunk("Rose India");  float w = c.getWidthPoint();  Paragraph p = new Paragraph("The width of the chunk: '");  p.add(c);  p.add("' is ");  p.add(String.valueOf(w));  p.add(" points or ");  p.add(String.valueOf(w / 72f));  p.add(" inches.");  document.add(p);  document.add(c);  document.add(Chunk.NEWLINE);  c.setHorizontalScaling(0.5f);  document.add(c);  document.add(c);  

    document.close();  }}

The output of the program is given below:

Negative Leading

In negative leading  you will learn how we can overlap a text using negative and positive leading values in  Phrase constructor. You can make shadow of any text. For this you have just change the values of  the leading in Phrase constructor and the text.

 

The code of the program is given below:

 

 

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Phrase;import com.lowagie.text.pdf.PdfWriter;public class NegativeLeading {  public static void main(String[] args)throws Exception {  System.out.println("Negative Leading");  Document document = new Document();  PdfWriter.getInstance(document, new FileOutputStream(  "NegativeLeading.pdf"));

  document.open();  document.add(new Phrase(16, "Rose india provides online java interview question and answer.Rose india provides online java"));  document.add(new Phrase(-5,"Rose india provides online java interview question and answer.Rose india provides online java")); document.close();  }}

The output of the program is given below:

Make Paragraph and Set Alignment  

In this example , we are going to explain how we can make paragraph and how we can gives various alignment. To make a paragraph we use Paragraph("Text") constructor . To gives alignment we use setAlignment(int alignment).

Code Description:setAlignment(int alignment):

We can set the aligment of a Paragraph with the method setAlignment(int alignment). The alignment can be one of the following values:

1. Element.ALIGN_LEFT2. Element.ALIGN_CENTER3. Element.ALIGN_RIGHT4. Element.ALIGN_JUSTIFIED

If you want the alignment to be justified, the space between the words can be stretched, as well as the space between the characters of a word. The default ratio of this word-char space is 2.5. You can change this ratio with setSpaceCharRatio. For instance, if you don't want the space between the characters of a word to be stretched, just do:

writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Element;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.PdfWriter;public class ParagraphAttributes   {  public static void main(String[] args)throws Exception   {  System.out.println("The Paragraph object");  Document document = new Document();  PdfWriter.getInstance(document, new FileOutputStream("ParagraphAttributes.pdf"));  document.open();  Paragraph[] p = new Paragraph[5];  p[0] = new Paragraph("RoseIndia.net");  p[1] = new Paragraph("RoseIndia.net");  p[2] = new Paragraph("RoseIndia.net");  p[3] = new Paragraph("RoseIndia.net");  p[4] = new Paragraph("RoseIndia.net"); for (int i = 0; i < 5; i++)  {  p[i].setAlignment(Element.ALIGN_JUSTIFIED);  document.add(p[i]);  }  document.close();  }}

The output of the program is given below:

Set Space Ratio and Alignment 

In this example , we are going to explain how we can set space ratio and how we can gives various alignment. You can make pdf with no space between the characters of a word .

Code Description:setAlignment(int alignment):

We can set the aligment of a Paragraph with the method setAlignment(int alignment). The alignment can be one of the following values:

1. Element.ALIGN_LEFT2. Element.ALIGN_CENTER3. Element.ALIGN_RIGHT4. Element.ALIGN_JUSTIFIED

setSpaceCharRatio:If you want the alignment to be justified, the space between the words can be stretched, as well as the space between the characters of a word. The default ratio of this word-char space is 2.5. You can change this ratio with setSpaceCharRatio. For instance, if you don't want the space between the characters of a word .We can do this by:

writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);

The code of the program is given below:

import java.io.FileOutputStream;import com.lowagie.text.Document;import com.lowagie.text.Element;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;

import com.lowagie.text.pdf.PdfWriter;public class SpaceWordRatio {    public static void main(String[] args)throws Exception {  System.out.println("Example of Space Word Ratio");    Document document = new Document(PageSize.A4, 50, 350, 50, 50);   PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("spacewordratio.pdf"));  document.open();  String input = "Rose india is a java site which provide you online tutorial and online interview question help";  Paragraph paragraph = new Paragraph(input);  paragraph.setAlignment(Element.ALIGN_JUSTIFIED);  document.add(paragraph);  document.newPage();  writer.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);  document.add(paragraph);  document.close();  }}

The output of the program is given below:

Symbol Substitution

In this example , we are going to explain how we can make symbols using iText api,s. We are using integer values which will  converted it into symbol. For this we just convert the integer value into its character value.

Code Description:In this example we  convents the integer values from 33 to 255 and 913 to 969 into character by using explicit casting  of integer values to character values. We use add(Phrase.getInstance(String s))method to add the values into the document. 

The code of the program is given below:

import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Phrase;import com.lowagie.text.pdf.PdfWriter;import com.lowagie.text.Paragraph;public class SymbolSubstitution {  public static void main(String[] args) throws Exception {  System.out.println("Symbol Substitution");  Document document = new Document();  PdfWriter.getInstance(document,new FileOutputStream("SymbolSubstitution.pdf"));  document.open();  document.add(new Paragraph("The values and its symboles are given below:"));  for (int i = 33; i < 255; i++) {  document.add(Phrase.getInstance(" " + i + ": " + (char) i)); }   for (int i = 913; i < 969; i++) {  document.add(Phrase.getInstance(" " + i + ": " + (char) i));  }  document.close();  }}

The output of the program is given below:

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.javaimport 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 lineunderlined.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.