Java code
Import java. awt. Color;
Import java. io. File;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. text. DecimalFormat;
Import java. text. NumberFormat;
Import java. util. ArrayList;
Import java. util. Date;
Import com. lowagie. text. Document;
Import com. lowagie. text. extends entexception;
Import com. lowagie. text. Element;
Import com. lowagie. text. Font;
Import com. lowagie. text. PageSize;
Import com. lowagie. text. Paragraph;
Import com. lowagie. text. Phrase;
Import com.lowagie.text=. BaseFont;
Import com.lowagie.textexample. nested cell;
Import com.lowagie.text?. sharepcell;
Import com.lowagie.text=. extends prow;
Import com.lowagie.text.pdf. PdfPTable;
Import com.lowagie.text=. writable writer;
Import com. sun. java_cup.internal.internal_error;
Public class PDFReport {
Document document = new Document (); // create a Document Object
Private static Font headfont; // set the Font size.
Private static Font keyfont; // set the Font size.
Private static Font textfont; // set the Font size
Static {
BaseFont bfChinese;
Try {
// BfChinese = BaseFont. createFont ("STSong-Light", "UniGB-UCS2-H", BaseFont. NOT_EMBEDDED );
BfChinese = BaseFont. createFont ("STSong-Light", "UniGB-UCS2-H", BaseFont. NOT_EMBEDDED );
Headfont = new Font (bfChinese, 10, Font. BOLD); // set the Font size.
Keyfont = new Font (bfChinese, 8, Font. BOLD); // set the Font size.
Textfont = new Font (bfChinese, 8, Font. NORMAL); // set the Font size
} Catch (Exception e ){
E. printStackTrace ();
}
}
Public PDFReport (File file ){
Document. setPageSize (PageSize. A4); // you can specify the page size.
Try {
Using writer. getInstance (document, new FileOutputStream (file ));
Document. open ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
Int maxWidth = 520;
Public writable pcell createCell (String value, com. lowagie. text. Font font, int align ){
PdfPCell cell = new PdfPCell ();
Cell. setVerticalAlignment (Element. ALIGN_MIDDLE );
Cell. setHorizontalAlignment (align );
Cell. setPhrase (new Phrase (value, font ));
Return cell;
}
Public writable pcell createCell (String value, com. lowagie. text. Font font ){
PdfPCell cell = new PdfPCell ();
Cell. setVerticalAlignment (Element. ALIGN_MIDDLE );
Cell. setHorizontalAlignment (Element. ALIGN_CENTER );
Cell. setPhrase (new Phrase (value, font ));
Return cell;
}
Public writable pcell createCell (String value, com. lowagie. text. Font font, int align, int colspan ){
PdfPCell cell = new PdfPCell ();
Cell. setVerticalAlignment (Element. ALIGN_MIDDLE );
Cell. setHorizontalAlignment (align );
Cell. setColspan (colspan );
Cell. setPhrase (new Phrase (value, font ));
Return cell;
}
Public writable pcell createCell (String value, com. lowagie. text. Font font, int align, int colspan, boolean boderFlag ){
PdfPCell cell = new PdfPCell ();
Cell. setVerticalAlignment (Element. ALIGN_MIDDLE );
Cell. setHorizontalAlignment (align );
Cell. setColspan (colspan );
Cell. setPhrase (new Phrase (value, font ));
Cell. setPadding (3.0f );
If (! BoderFlag ){
Cell. setBorder (0 );
Cell. setPaddingTop (15.0f );
Cell. setPaddingBottom (8.0f );
}
Return cell;
}
Public PdfPTable createTable (int colNumber ){
PdfPTable table = new PdfPTable (colNumber );
Try {
Table. setTotalWidth (maxWidth );
Table. setLockedWidth (true );
Table. setHorizontalAlignment (Element. ALIGN_CENTER );
Table. getDefaultCell (). setBorder (1 );
} Catch (Exception e ){
E. printStackTrace ();
}
Return table;
}
Public PdfPTable createTable (float [] widths ){
PdfPTable table = new PdfPTable (widths );
Try {
Table. setTotalWidth (maxWidth );
Table. setLockedWidth (true );
Table. setHorizontalAlignment (Element. ALIGN_CENTER );
Table. getDefaultCell (). setBorder (1 );
} Catch (Exception e ){
E. printStackTrace ();
}
Return table;
}
Public PdfPTable createBlankTable (){
PdfPTable table = new PdfPTable (1 );
Table. getDefaultCell (). setBorder (0 );
Table. addCell (createCell ("", keyfont ));
Table. setSpacingAfter (202.16f );
Table. setSpacingBefore (20366f );
Return table;
}
Public void generatePDF () throws Exception {
PdfPTable table = createTable (4 );
Table. addCell (createCell ("Student Information List:", keyfont, Element. ALIGN_LEFT, 4, false ));
Table. addCell (createCell ("name", keyfont, Element. ALIGN_CENTER ));
Table. addCell (createCell ("Age", keyfont, Element. ALIGN_CENTER ));
Table. addCell (createCell ("gender", keyfont, Element. ALIGN_CENTER ));
Table. addCell (createCell ("Address", keyfont, Element. ALIGN_CENTER ));
For (int I = 0; I <5; I ++ ){
Table. addCell (createCell ("name" + I, textfont ));
Table. addCell (createCell (I + 15 + "", textfont ));
Table. addCell (createCell (I % 2 = 0 )? "Male": "female", textfont ));
Table. addCell (createCell ("Address" + I, textfont ));
}
Document. add (table );
Document. close ();
}
Public static void main (String [] args) throws Exception {
File file = new File ("D :\\ text ");
File. createNewFile ();
New PDFReport (file). generatePDF ();
}
}
This article is from "greatjone"