Java generates PDF documents (tables, lists, add pictures, etc.)

Source: Internet
Author: User
Tags image png

Two packages required and:

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

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

The code is as follows:

1 ImportJava.awt.Color;2 ImportJava.io.FileOutputStream;3 ImportCom.lowagie.text.Cell;4 ImportCom.lowagie.text.Chapter;5 Importcom.lowagie.text.Document;6 ImportCom.lowagie.text.Font;7 ImportCom.lowagie.text.Image;8 Importcom.lowagie.text.List;9 ImportCom.lowagie.text.ListItem;Ten Importcom.lowagie.text.PageSize; One Importcom.lowagie.text.Paragraph; A Importcom.lowagie.text.Section; - Importcom.lowagie.text.Table; - ImportCom.lowagie.text.pdf.BaseFont; the ImportCom.lowagie.text.pdf.PdfWriter; -  Public classItextdemo { -      Public Booleanitexttest () { -         Try { +             /**instantiating a Document object*/ -Document document =NewDocument (PAGESIZE.A4, 50, 50, 50, 50); +             /**Create a PDFWriter object*/ APdfwriter.getinstance (document,//references to document objects at                     NewFileOutputStream ("D://itexttest.pdf"));//the output path of the file + the actual name of the file -Document.open ();//Open Document -             /**The setting of Chinese fonts in PDF document, be sure to add Itextasian.jar package*/ -Basefont Bfchinese = Basefont.createfont ("Stsong-light", -"Unigb-ucs2-h", basefont.not_embedded); -Font Fontchinese =NewFont (Bfchinese, font.normal);//Add Document: in             /**to add content to a document, create a paragraph object*/ -Document.add (NewParagraph ("First page of the document.");//paragraph adding text toDocument.add (NewParagraph ("We Are vermin", Fontchinese)); +             /**Create a Chapter object*/ -Paragraph Title1 =NewParagraph ("The first chapter", Fontchinese); theChapter Chapter1 =NewChapter (Title1, 1); *Chapter1.setnumberdepth (0); $             /**Create a section in a section*/Panax NotoginsengParagraph Title11 =NewParagraph ("Add to Table", Fontchinese); -Section Section1 =chapter1.addsection (TITLE11); the             /**create a paragraph and add it to the subsection*/ +Paragraph Somesectiontext =NewParagraph ("3 X 2 tables are shown below."), A Fontchinese); the Section1.add (somesectiontext); +             /**Create a Table object (a table containing a row and column matrix)*/ -Table T =NewTable (3, 2);//2 rows 3 columns $T.setbordercolor (NewColor (220, 255, 100)); $T.setpadding (5); -T.setspacing (5); -T.setborderwidth (1); theCell C1 =NewCell (NewParagraph ("First Pane", Fontchinese)); - T.addcell (C1);WuyiC1 =NewCell ("Header2"); the T.addcell (C1); -C1 =NewCell ("Header3"); Wu T.addcell (C1); -             //The second line starts without the need for new Cell () AboutT.addcell ("1.1"); $T.addcell ("1.2"); -T.addcell ("1.3"); - Section1.add (t); -             /**Create a section in a section*/ AParagraph title13 =NewParagraph ("Add to List", Fontchinese); +Section Section3 =chapter1.addsection (TITLE13); the             /**create a paragraph and add it to the subsection*/ -Paragraph SOMESECTIONTEXT3 =NewParagraph ("The list is shown below."), Fontchinese); $ Section3.add (SOMESECTIONTEXT3); the             /**Create a list and add it to a PDF document*/ theList L =NewList (true,true, 10);//The first argument is true to create a list to be numbered by itself, the             //If False, no self-numbering is done theL.add (NewListItem ("First item of list")); -L.add (NewListItem ("Second list", Fontchinese)); in Section3.add (l); the Document.add (chapter1); the             /**Create a Chapter object*/ AboutParagraph title2 =NewParagraph ("chapter II"), Fontchinese); theChapter Chapter2 =NewChapter (Title2, 1); theChapter2.setnumberdepth (0); the             /**Create a section in a section*/ +Paragraph title12 =NewParagraph ("PNG image Add", Fontchinese); -Section Section2 =chapter2.addsection (title12); the             /**Add a picture*/BayiSection2.add (NewParagraph ("Picture added: Pie chart", Fontchinese)); theImage png = image.getinstance ("D:/pie.png");//the address of the picture the Section2.add (PNG); - Document.add (chapter2); - document.close (); the             return true; the}Catch(Exception E2) { the System.out.println (E2.getmessage ()); the         } -         return false; the     } the      Public Static voidMain (String args[]) { theSystem.out.println (NewItextdemo (). Itexttest ());94     } the}

If you find that Chinese cannot be displayed, you can start with new Paragraph ("", Fontchinese) where you need to display the Chinese language.

Fontchinese: For the Chinese font settings, see the code.

Java generates PDF documents (tables, lists, add pictures, etc.)

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.