Using Itext to generate PDF reports in a JSP

Source: Internet
Author: User
Tags getmessage


The origin of the problem

Not long ago done a JSP to generate a PDF report of the small project, it is an open vision. Some of the enterprise information through the network to form an HTML report, although IE can directly print the contents of the display, but from the interface, if the direct HTML display results printed out, it is not very beautiful. If you turn it into a PDF file and then print it, the printing effect will be much better.

Itext Introduction

Itext is an open source Java class Library that can be used to easily generate PDF files. You download the latest version of the class library by visiting http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948, After the download is complete, you will get a. jar package, which can be used by adding this package to the JDK classpath. If you need to see Chinese, Japanese, and Korean characters in the generated PDF file, you also need to download the Itextasian.jar package by accessing Http://itext.sourceforge.net/downloads/iTextAsian.jar.

Http://www.lowagie.com/iText/tutorial/index.html has a more detailed tutorial on the use of the IText class library. This tutorial begins with a more systematic introduction to the methods and techniques of putting text, pictures, forms, and so on in a PDF file. After reading this tutorial, you can basically do some simple to complex PDF files. However, trying to solve all the difficulties in the process of generating PDF files through tutorials is an extravagant hope. Therefore, it is important to read the Itext API documentation. While downloading the class library, the reader can also download the document for the class library.

How to use Itext to generate PDF reports in Java 


The following is one of the simplest examples of the above tutorial, which depicts the general procedural framework for generating PDF files through Itext. Readers only need to add the content they want to put in the PDF file in the middle of Document.open () and Document.close () two statements. This example only adds the word "Hello World" to the PDF file.

Document document = new Document();
try
{
  PdfWriter.getInstance(document, new FileOutputStream ("Chap0101.pdf"));
  document.open();
  document.add(new Paragraph("Hello World"));
}
catch(DocumentException de)
{
  System.err.println(de.getMessage());
}
catch(IOException ioe)
{
  System.err.println(ioe.getMessage());
}
document.close();


As can be seen from the above examples, the framework of the procedure is very clear. However, it is very troublesome to specify the position of text, drawing, and table in the PDF. There seems to be no better way to do this than to constantly modify the location in the program, then run the program, generate a PDF file, and see if the elements are reasonable in the PDF.


Related Article

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.