JavaWeb Integrated Development Wang returned, this book is not bad
Package com. helloweenvsfei. itext;
Import java. io. FileOutputStream;
Import com. lowagie. text. Document;
Import com. lowagie. text. PageSize;
Import com. lowagie. text. Paragraph;
Import com.lowagie.text=. writable writer;
Public class FirstPDF {
Public static void main (String [] args ){
// Create a document object, A4 paper size
Document document = new Document (PageSize. A4 );
Try {
// The output is the E: \ itextextension file.
Using writer = Using writer. getInstance (document,
New FileOutputStream ("E: \ itext "));
// Open the document
Document. open ();
// Write text into the PDF file
Document. add (new Paragraph ("Hello World, Hello iText "));
// Close the document
Document. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
After the program runs, an itextsag file will be generated under the root directory of the E drive. After the PDF file is opened, the effect is 40.2.
(Click to view the larger image) Figure 40.2 PDF file generated by iText
The procedure for generating a pdf file using iText is as follows:
(1) create a Document object.
Document document = new Document ();
Document has three constructor methods:
Public Document ();
Public Document (Rectangle pageSize );
Public Document (Rectangle pageSize, int marginLeft, int marginRight, int marginTop, int marginBottom ).
PageSize is the size of the paper type. It can be expressed by constants in PageSize. For example, PageSize. A4 indicates A4 paper. MarginLeft, marginRight, marginTop, and marginBottom are the padding sizes of the text from the left, right, top, and bottom of the page edge.
(2) create a Writer and associate it with the document object. The Writer can write the document to the disk.
Using writer = Using writer. getInstance (document, new FileOutputStream
("E: \ itext.pdf "));
(3) open the document.
Document. open ();
(4) write the document content.
Document. add (new Paragraph ("Hello iText "));
The content of the written documents can be of various types, including text Paragraph with format, Phrase, Paragraph, Table, and Graphic objects.
(5) Close the document.
Document. close ();
After completing the preceding five steps, you can generate a pdf document.
This article is from the "feisha" blog