How to generate a PDF file dynamically from a Java application

Source: Internet
Author: User
Tags valid

Many applications require a PDF document to be generated dynamically. These applications cover readers who generate customer statements and mail-delivered banks to purchase specific book chapters and receive these book chapters in PDF format. This list is too numerous. In this article, we will use the IText Java library to generate PDF documents. We'll show you a sample application so that you can do it yourself and understand it better.

Familiar with IText V5.3.0 edition

IText is a Java library that can be obtained free of charge from http://itextpdf.com/. The IText library is powerful and supports the generation of HTML, RTF, and XML documents, as well as the production of PDF documents. You can select a variety of fonts for use in your document. In addition, the ITEXT structure allows you to use the same code to generate any type of document mentioned above.

The IText library contains classes that generate PDF text using various fonts, generate tables in a PDF document, add watermarks to pages, and so on. IText also offers a number of other features. All of these features cannot be demonstrated in an article. We will describe the basics needed to build PDFs. For more details, see the documentation provided by your vendor.

We will use Eclipse to develop our sample application. As an open source ide,eclipse is free to get and very powerful. You can download Eclipse now.

IText API: Learn more about

Com.itextpdf.text.Document is the main class used to generate PDF documents. This is the first class that needs to be instantiated. After you create a document, you need a writer to write information to it. Com.itextpdf.text.pdf.PdfWriter is a PDF writer. Some other common classes are provided below:

Com.itextpdf.text.Paragraph: This class represents an indented paragraph.

Com.itextpdf.text.Chapter: This class represents a chapter in a PDF document. Use Paragraph as the title, int as the chapter number to create the class.

Com.itextpdf.text.Font: This class contains all the specifications for a font, such as font set, font size, style, and color. Various fonts are declared as static constants in this class.

Com.itextpdf.text.List: This class represents a list, which, in turn, contains many listitems.

Com.itextpdf.text.pdf.PDFPTable: This is a table that can be placed in an absolute position or added to a document as a class table.

Com.itextpdf.text.Anchor: A Anchor may be a reference, or a reference target.

Downloading and configuring IText in Eclipse

As a pure Java library, IText is provided in the form of a JAR file. After downloading the library (assuming download to the path C:\temp), use the following steps to configure the IText library in the ECLIPSE environment:

Create a new Java project named IText in Eclipse.

Right-click the IText item in the Package Explorer view to select Properties.

Click Java Build Path. On the Libraries tab, click Add External JARs.

Locate the C:\temp directory and select Itext-5.3.0.jar in this directory.

Click OK.

IText is now configured, Eclipse is ready to create a Java application to generate a dynamic PDF document.

Sample Application

What better way to demonstrate any technology than a valid example that you created yourself? Now that you have the tools you need (the Eclipse IDE) and the library (IText library), we are ready to design and develop a valid sample program.

Start by creating a simple PDF document that contains basic elements such as plain text, colored text with Non-default fonts, tables, lists, chapters, sections, and so on. The purpose of this application is to familiarize you with the way the IText library is used. There are a number of classes that perform a lot of work related to the production of PDF documents. This article is unlikely to introduce all of these classes. IText's Javadocs is a good source of information about how to use these classes. Next we start coding.

The first step is to create a document. A document is a container for all the elements of a PDF document.

Listing 1. Instantiating a Document object

Document document = new Document (PAGESIZE.A4, 50, 50, 50, 50);

The first parameter is the page size. The next few parameters are the left margin, the right margin, the top margin, and the bottom margin. The type of this document is not yet defined. It depends on the type of writer you create. For our sample, we choose Com.itextpdf.text.pdf.PdfWriter. Other writers include HTMLWriter, Rtfwriter, XmlWriter, etc. Their names illustrate their actual use.

Listing 2. Creating PDFWriter objects

PDFWriter writer = pdfwriter.getinstance (document,
     
new FileOutputStream ("C:\\itexttest.pdf"));
     
Document.open ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.