Generate PDF files from Java applications dynamically. A step- by- step guide to i. Text, an open source library that makes PDF. Amit Tuli with Surbhi Agarwal. Published on January 2. Updated: December 1.
Many applications demand dynamic generation of PDF documents. In this article. we will use the i. Text Java library to generate PDF documents. Connect more. The new developer.
Works Premium membership program provides an. Safari Books Online, deep discounts on premier. O'Reilly conferences, and. Sign up today. i.
Text is a freely available Java library from http: //itextpdf. The i. Text library is powerful and. HTML, RTF, and XML documents, in addition to. PDFs. You can choose from a variety of fonts to be used in the. Also, the structure of i. Text allows you to generate any of the.
The i. Text library contains classes to generate PDF text in various fonts. PDF document, add watermarks to pages, and so on. There. are many more features available with i. Text. We will cover the basics required for PDF.
For more detailed information, refer to the documentation from. Being an. open source IDE, Eclipse is freely available and quite powerful. You can. download Eclipse now. The i. Text API. Closer look. The com. itextpdf. Document is the main class for PDF document. This is the first class to be instantiated.
Once the document. The. com. itextpdf. Pdf. Writer is a PDF writer. Some of the other. Paragraph—This class. Chapter—This class. PDF document. It is created using a.
Paragraph as title and an int as chapter. Font—This class contains all. Various fonts are declared as static constants in this. List—This class represents a.
List. Items. com. PDFPTable—This is a. Table. com. itextpdf.
Example code for HTML to PDF conversion in Java. One Response to “how to convert a HTML web page into a PDF file using Java. This tutorial explains how to insert tables in PDF files in Java using IText. Creating a Table; Table Width; Spacing Before and After Table. How to convert HTML file to PDF document using iText Java Library? Environment & Tools. Convert HTML to PDF Using Java ” Rajesh August 23, 2013 at 6:27 am.
Anchor—An Anchor can be a. Downloading and configuring. Text in Eclipse. Being a pure Java library, i. Text comes in the form of a JAR file. Once you have downloaded the library. C: \temp), the following steps will configure the i.
Text. library in an Eclipse environment: Create a new Java project in Eclipse named i. Text. Right- click on the i. Text project in Package Explorer view and select. Properties. Click Java Build Path. On the Libraries tab, click. Add External JARs. Browse to the C: \temp directory and select the itext- 5.
Click OK. i. Text is now configured, and Eclipse is ready to create Java applications. PDF documents. Sample application. What can demonstrate any technology better than a working sample, created. Now that you have the requisite tools (Eclipse IDE) and. Text library), we are all set to design and develop a sample. Let's create a simple PDF document that contains some basic elements like.
The purpose of this application is to familiarize you with. Text library. There are plenty of classes that do lots of. PDF document generation. It will not be possible. The javadocs of i. Text are a good source of.
Let's start coding. The first step is to create a document. A document is the container for all.
PDF document. Instantiation of document. Document document = new Document(Page. Size. A4, 5. 0, 5. The first argument is the page size. The next arguments are left, right. The type of this document is not.
It depends on the type of writer you create. Other. writers are Html. Writer, Rtf. Writer, Xml. Writer, and several others. Their. names explain their purposes self- sufficiently.
Listing 2. Creation of Pdf. Writer. object. Pdf.
Writer writer = Pdf. Writer. get. Instance(document, \. File. Output. Stream(. Otherwise, you can provide your own font. In this PDF, we used the.
To. Top as the link. When you click on the back. To. Top link, it brings you. You need to set the text as the anchor. Let's see how to set the anchor target and set. Creation of paragraph.
Anchor anchor. Target = new Anchor(. To close the document, add. Listing 3. Figure 1. Sample output of code in. Listing 3. View image at full size.
You just saw how to add plain text into PDF document. Next, we need to add. Let's start with creating a new. A chapter is a special section, which starts with a new page and. Listing 4. Creation of chapter. Paragraph title. 1 = new Paragraph(.
In the code in Listing 5, we create a section with the title . Creation of section. Paragraph title. 11 = new Paragraph(. Add. the following two lines to close the document in Figure. Then compile and execute the program to generate the PDF. Figure 2. Sample output of.
View image at full size. Next, we create a table object.
A table contains a matrix of rows and. A cell in a row can span more than one column. Similarly, one. cell in a column can span more then one row.
Listing 6. Creation of table. Pdf. PTable t = new Pdf. PTable(3). t. set. Spacing. Before(2. Spacing. After(2.
Pdf. PCell c. 1 = new Pdf. PCell(new Phrase(. Next. we create three PDFPcell objects, with different text. We keep on adding. They are added in the first row, starting from the. Once the row is. complete, the next cell is added to the first column of the next row.
A. cell can also be added to the table by just providing the text of the. Cell(. At the end, the table object. Finally, let's see how to add a list to the PDF document. A list contains a. List. Items. A list can be numbered or non- numbered. Creation of list. List l = new List(true, false, 1.
List. Item(. Now we add a. We can scale images using one of these Image. Absolute()scale. Absolute. Width()scale. Absolute. Height()scale. Percentage()scale. To. Fit()In Listing 8, we used the scale.
Absolute. And then. Section. Listing 8.
Adding Image to the main. Document. Image image.
Image. get. Instance(. The anchor (link) can. Adding Anchor to the main. Paragraph title. 2 = new Paragraph(. We will also. close the document object here as we are done with the sample. Listing 1. 0. Addition of a chapter. Running the. sample application.
Download the sample application, j- itextsample. Download). Unzip j- itextsample. If you extract it into. C: \temp, for example, this places the source and class files into. C: \temp\com\itext\test.
Open a command prompt and change the directory to C: \temp. Set your system's classpath on this command prompt. Include. C: \temp\itext- 5. On. Windows. Figure 3 shows a screen capture of the first page of. PDF document. Figure 3.
Screen capture of PDF. View image at full size.
Figure 4 shows a screen capture of Chapter 1 and its. PDF document. Figure 4. Screen capture of PDF. View image at full size. Figure 5 shows a screen capture of the Anchor link in. PDF document. Figure 5.
Screen capture of PDF. View image at full size. Conclusion. You have just seen some basic elements of PDF generation.
Also, you can. redirect the output of the writer to the console (in the case of XML and. HTML writers), the output stream of servlets (in the case of a response to.
PDF documents), or any other kind of Output.