Java generates PDF documents

Source: Internet
Author: User

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"
 

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.