Java export pdf (1)

Source: Internet
Author: User

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

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.