First, the following jar packages are imported:
PackagePoi.zr.com.pojo;ImportJava.awt.Color;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;Importcom.lowagie.text.BadElementException;ImportCom.lowagie.text.Cell;Importcom.lowagie.text.Document;Importcom.lowagie.text.DocumentException;Importcom.lowagie.text.Element;ImportCom.lowagie.text.Font;Importcom.lowagie.text.PageSize;Importcom.lowagie.text.Paragraph;Importcom.lowagie.text.Phrase;Importcom.lowagie.text.Table;ImportCom.lowagie.text.pdf.BaseFont;ImportCom.lowagie.text.pdf.PdfWriter; Public classPOI { Public Static voidMain (string[] args)throwsdocumentexception, IOException {//Create Document Object (page size is A4, left, right, top, bottom margins are ten)Document document =NewDocument (PAGESIZE.A4, 10, 10, 10, 10); //set up a writing devicePdfwriter.getinstance (document,NewFileOutputStream ("/users/apple/desktop/poi. PDF ")); //setting the relevant parametersPoi.setparameters (document, "Developer Test", "Test", "Test developer Debug", "Ganyu Road", "Ganyu Road")); //Open DocumentDocument.open (); //using fonts in Itextasian.jarBasefont Basefont = Basefont.createfont ("Stsong-light", "Unigb-ucs2-h", basefont.not_embedded); Font Font=NewFont (Basefont); List<User> users =NewArraylist<user>(); //Looping Add Objects for(inti = 0; I < 5; i++) {User User=NewUser (); User.setid (""+i); User.setinfo ("Developer Test" +i); User.setname ("Test" +i); Users.add (user); } Table Table=poi.settable (users); Document.add (NewParagraph ("User information is as follows:"), Poi.setfont ())); Document.add (table); //Close DocumentDocument.close (); } Public StaticTable settable (list<user> users)throwsbadelementexception{//Create a table with 3 columnsTable Table =NewTable (3); Table.setborderwidth (1); Table.setbordercolor (NewColor (0, 0, 255)); Table.setpadding (5); Table.setspacing (5); //Create a table headerCell cell1 = Poi.settableheader ("Number ID"); Cell cell2= Poi.settableheader ("Basic Information"); Cell Cell3= Poi.settableheader ("name"); Table.addcell (CELL1); Table.addcell (CELL2); Table.addcell (CELL3); //each page will display a header after you add this codetable.endheaders (); for(inti = 0; I < users.size (); i++) {Cell celli1=Poi.settableheader (Users.get (i). GetId ()); Cell Celli2=Poi.settableheader (Users.get (i). GetInfo ()); Cell Celli3=Poi.settableheader (Users.get (i). GetName ()); Table.addcell (CELLI1); Table.addcell (CELLI2); Table.addcell (CELLI3); } returntable; } /*** Set Font encoding format *@return */ Public StaticFont SetFont () {Basefont Basefont=NULL; Try{Basefont= Basefont.createfont ("Stsong-light", "Unigb-ucs2-h", basefont.not_embedded); } Catch(documentexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } Font Font=NewFont (Basefont, 8, Font.normal,color.blue); returnfont; } /*** Set Cell *@paramname *@return * @throwsbadelementexception*/ Public StaticCell Settableheader (String name)throwsbadelementexception{Cell Cell=NewCell (NewPhrase (Name,poi.setfont ())); //Cell Horizontal Alignmentcell.sethorizontalalignment (Element.align_center); //Cell Vertical Alignmentcell.setverticalalignment (element.align_center);//Cell.setheader (true);Cell.setbackgroundcolor (color.red); returncell; } /*** Set relevant parameters *@paramDocument *@return */ Public StaticDocument Setparameters (document Document,string title,string subject,string keywords,string author, String Creator) {//Set TitleDocument.addtitle (title); //Set ThemeDocument.addsubject (subject); //Set Authordocument.addkeywords (keywords); //Set AuthorDocument.addauthor (author); //Set CreatorDocument.addcreator (creator); //set up producerDocument.addproducer (); //Set creation Datedocument.addcreationdate (); returndocument; } }
The result of the code running is as follows:
Java uses itext to generate PDF tables