First, the preface
In the Enterprise Information System, report processing has always been a relatively important role, this article will introduce a PDF report to generate a Java component--itext. By using JSP or JavaBean to generate PDF reports on the server side, the client uses hyperlinks to display or download the generated report, which solves the problem of the B/s system's report processing.
Ii. introduction of Itext
Itext is a well-known open source site SourceForge a project that is used to generate a PDF document of a Java class library. Not only can you generate PDF or RTF documents through Itext, but you can convert XML and HTML files to PDF files.
IText installation is very convenient, in the Http://www.lowagie.com/iText/download.html-download The Web site download Itext.jar file, only in the system classpath add Itext.jar Path, in the program can use the Itext class library.
Third, the establishment of the first PDF document
It takes 5 steps to build a PDF document with Itext:
① establishes an instance of the Com.lowagie.text.Document object.
Document document = new document ();
② establishes a writer (Writer) that is associated with the document object and can write documents to disk through the writer (Writer).
Pdfwriter.getinstance (document, New FileOutputStream ("Helloworld.pdf"));
③ Open the document.
Document.open ();
④ add content to your document.
Document.add (New Paragraph ("Hello World"));
⑤ closes the document.
Document.close ();
With the 5 steps above, you can produce a helloworld.pdf file with the contents "Hello World".
Creating an instance of a Com.lowagie.text.Document object
There are three constructors for the Com.lowagie.text.Document object, respectively:
public Document();
public Document(Rectangle pageSize);
public Document(Rectangle pageSize,
int marginLeft,
int marginRight,
int marginTop,
int marginBottom);
The parameter PageSize of the constructor is the size of the document page, and for the first build function, the page size is A4, the same as that of document (PAGESIZE.A4); For the third build function, Parameters marginleft, MarginRight, MarginTop, MarginBottom, respectively, are left, right, top, and bottom margins.
Parameters pagesize can be used to set the page size, face background color, and page Landscape/portrait properties. Itext defines paper types such as A0-A10, AL, letter, Halfletter, _11x17, Ledger, note, b0-b5, Arch_a-arch_e, FLSA, and Flse, or through rectangle PageSize = new Rectangle (144, 720), custom paper. The rectangle method Rotate () enables you to set the page to landscape.