Java generates PDF documents

Source: Internet
Author: User
Tags image png

Two required packages and:

(1) iText. jar: http://download.csdn.net/source/296416

(2) iTextAsian. jar (used for Chinese conversion): http://download.csdn.net/source/172399

 

The following is a sample code:

 

Import java. awt. Color;
Import java. io. FileOutputStream;

Import com. lowagie. text. Cell;
Import com. lowagie. text. Chapter;
Import com. lowagie. text. Document;
Import com. lowagie. text. Font;
Import com. lowagie. text. Image;
Import com. lowagie. text. List;
Import com. lowagie. text. ListItem;
Import com. lowagie. text. PageSize;
Import com. lowagie. text. Paragraph;
Import com. lowagie. text. Section;
Import com. lowagie. text. Table;
Import com.lowagie.text=. BaseFont;
Import com.lowagie.text=. writable writer;

Public class itextdemo {
Public Boolean itexttest (){
Try {
/** Instantiate the Document Object */
Document document = new document (pagesize. A4, 50, 50, 50, 50 );

/** Create an external writer object */
Using writer. getinstance (document, // reference of the Document Object
New fileoutputstream ("D: // itexttest.pdf"); // file output path + actual file name
Document. open (); // open the document

/** Set the Chinese font in the pdf document. Be sure to add the iTextAsian. jar package */
BaseFont bfChinese = BaseFont. createFont ("STSong-Light ",
UniGB-UCS2-H, BaseFont. NOT_EMBEDDED );
Font FontChinese = new Font (bfChinese, 12, Font. NORMAL); // Add document:

/** Add content to the document and create a paragraph object */
Document. add (new Paragraph ("First page of the document."); // Paragraph adds text
Document. Add (new paragraph ("We are pests", fontchinese); // fontchinese: Set the Chinese font

/** Create a chapter object */
Paragraph title1 = new paragraph ("Chapter 1", fontchinese );
Chapter Chapter1 = new chapter (title1, 1 );
Chapter1.setnumberdepth (0 );
/** Create a section */
Paragraph title11 = new paragraph ("Add Table", fontchinese );
Section Section1 = chapter1.addsection (title11 );
/** Create a paragraph and add it to the Section */
Paragraph somesectiontext = new paragraph ("Table 3x2 is shown below .",
Fontchinese );
Section1.add (somesectiontext );

/** Create a table object (a table containing a row-column matrix )*/
Table T = new table (3, 2); // two rows and three columns
T. setbordercolor (new color (220,255,100 ));
T. setpadding (5 );
T. setSpacing (5 );
T. setBorderWidth (1 );
Cell c1 = new Cell (new Paragraph ("first grid", FontChinese ));
T. addCell (c1 );
C1 = new Cell ("Header2 ");
T. addCell (c1 );
C1 = new Cell ("Header3 ");
T. addCell (c1 );
// New Cell () is not required from the second line ()
T. addCell ("1.1 ");
T. addCell ("1.2 ");
T. addCell ("1.3 ");
Section1.add (t );

/** Create a section */
Paragraph title13 = new Paragraph ("add list", FontChinese );
Section section3 = chapter1.addSection (title13 );
/** Create a paragraph and add it to the Section */
Paragraph someSectionText3 = new Paragraph ("the list is shown below.", FontChinese );
Section3.add (someSectionText3 );
/** Create a list and add it to the pdf document */
List l = new List (true, true, 10); // if the first parameter is true, a List of IDS is created,
// If this parameter is set to false, no serial number is assigned.
L. add (new ListItem ("First item of list "));
L. add (new ListItem ("second list", FontChinese ));
Section3.add (l );
Document. add (chapter1 );

/** Create a chapter object */
Paragraph title2 = new Paragraph ("Chapter 2", FontChinese );
Chapter chapter2 = new Chapter (title2, 1 );
Chapter2.setNumberDepth (0 );
/** Create a section */
Paragraph title12 = new Paragraph ("add png Image", FontChinese );
Section section2 = chapter2.addSection (title12 );

/** Add an image */
Section2.add (new paragraph ("image adding: Pie Chart", fontchinese ));
Image PNG = image. getinstance ("D:/pie.png"); // The image address
Section2.add (PNG );

Document. Add (Chapter2 );
Document. Close ();
Return true;
} Catch (exception E2 ){
System. Out. println (e2.getmessage ());
}
Return false;
}

Public static void main (string ARGs []) {
System. Out. println (New itextdemo (). itexttest ());
}
}

 

 

Note: If you find that Chinese characters cannot be displayed, You can firstNewParagraph ("", fontchinese );

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.