Here, we will read about itext version in java.
itext version
In this program we are going to find version of
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.Document class 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:
- Create Rectangle object and set the length and width of
the page.
- 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.
- 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 and
FileOutputStream("VersionPDF.pdf").
- Open the document.
- 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 created Document. Inside the constructor of Paragraph we have
to pass a String .Create paragraph and add it to the document.
- To find the version of iText use getVersion() method
which is static. It is a method of Document.
- Add next paragraph.
- Close the document. 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;
|
The output of the program is given below: