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.