java生成PDF文件

转载自:http://blog.csdn.net/dinphi/article/details/14122311

1、需要导入itext.jar和iTextAsian.jar  下载地址:http://sourceforge.net/projects/itext/files/

[java]  view plain  copy
  1. import java.awt.Color;    
  2. import java.io.File;    
  3. import java.io.FileNotFoundException;    
  4. import java.io.FileOutputStream;    
  5. import java.io.IOException;    
  6. import java.text.DecimalFormat;    
  7. import java.text.NumberFormat;    
  8. import java.util.ArrayList;    
  9. import java.util.Date;    
  10.     
  11. import com.lowagie.text.Document;    
  12. import com.lowagie.text.DocumentException;    
  13. import com.lowagie.text.Element;    
  14. import com.lowagie.text.Font;    
  15. import com.lowagie.text.PageSize;    
  16. import com.lowagie.text.Paragraph;    
  17. import com.lowagie.text.Phrase;    
  18. import com.lowagie.text.pdf.BaseFont;    
  19. import com.lowagie.text.pdf.PdfCell;    
  20. import com.lowagie.text.pdf.PdfPCell;    
  21. import com.lowagie.text.pdf.PdfPRow;    
  22. import com.lowagie.text.pdf.PdfPTable;    
  23. import com.lowagie.text.pdf.PdfWriter;    
  24. import com.sun.java_cup.internal.internal_error;    
  25.     
  26. public class PDFReport{    
  27.     Document document = new Document();// 建立一个Document对象        
  28.         
  29.     private static Font headfont ;// 设置字体大小    
  30.     private static Font keyfont;// 设置字体大小    
  31.     private static Font textfont;// 设置字体大小    
  32.         
  33.     
  34.         
  35.     static{    
  36.         BaseFont bfChinese;    
  37.         try {    
  38.             //bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);    
  39.             bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);    
  40.             headfont = new Font(bfChinese, 10, Font.BOLD);// 设置字体大小    
  41.             keyfont = new Font(bfChinese, 8, Font.BOLD);// 设置字体大小    
  42.             textfont = new Font(bfChinese, 8, Font.NORMAL);// 设置字体大小    
  43.         } catch (Exception e) {             
  44.             e.printStackTrace();    
  45.         }     
  46.     }    
  47.         
  48.         
  49.     public PDFReport(File file) {            
  50.          document.setPageSize(PageSize.A4);// 设置页面大小    
  51.          try {    
  52.             PdfWriter.getInstance(document,new FileOutputStream(file));    
  53.             document.open();     
  54.         } catch (Exception e) {    
  55.             e.printStackTrace();    
  56.         }     
  57.             
  58.             
  59.     }    
  60.     int maxWidth = 520;    
  61.         
  62.         
  63.      public PdfPCell createCell(String value,com.lowagie.text.Font font,int align){    
  64.          PdfPCell cell = new PdfPCell();    
  65.          cell.setVerticalAlignment(Element.ALIGN_MIDDLE);            
  66.          cell.setHorizontalAlignment(align);        
  67.          cell.setPhrase(new Phrase(value,font));    
  68.         return cell;    
  69.     }    
  70.         
  71.      public PdfPCell createCell(String value,com.lowagie.text.Font font){    
  72.          PdfPCell cell = new PdfPCell();    
  73.          cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
  74.          cell.setHorizontalAlignment(Element.ALIGN_CENTER);     
  75.          cell.setPhrase(new Phrase(value,font));    
  76.         return cell;    
  77.     }    
  78.     
  79.      public PdfPCell createCell(String value,com.lowagie.text.Font font,int align,int colspan){    
  80.          PdfPCell cell = new PdfPCell();    
  81.          cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
  82.          cell.setHorizontalAlignment(align);        
  83.          cell.setColspan(colspan);    
  84.          cell.setPhrase(new Phrase(value,font));    
  85.         return cell;    
  86.     }    
  87.     public PdfPCell createCell(String value,com.lowagie.text.Font font,int align,int colspan,boolean boderFlag){    
  88.          PdfPCell cell = new PdfPCell();    
  89.          cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
  90.          cell.setHorizontalAlignment(align);        
  91.          cell.setColspan(colspan);    
  92.          cell.setPhrase(new Phrase(value,font));    
  93.          cell.setPadding(3.0f);    
  94.          if(!boderFlag){    
  95.              cell.setBorder(0);    
  96.              cell.setPaddingTop(15.0f);    
  97.              cell.setPaddingBottom(8.0f);    
  98.          }    
  99.         return cell;    
  100.     }    
  101.      public PdfPTable createTable(int colNumber){    
  102.         PdfPTable table = new PdfPTable(colNumber);    
  103.         try{    
  104.             table.setTotalWidth(maxWidth);    
  105.             table.setLockedWidth(true);    
  106.             table.setHorizontalAlignment(Element.ALIGN_CENTER);         
  107.             table.getDefaultCell().setBorder(1);    
  108.         }catch(Exception e){    
  109.             e.printStackTrace();    
  110.         }    
  111.         return table;    
  112.     }    
  113.      public PdfPTable createTable(float[] widths){    
  114.             PdfPTable table = new PdfPTable(widths);    
  115.             try{    
  116.                 table.setTotalWidth(maxWidth);    
  117.                 table.setLockedWidth(true);    
  118.                 table.setHorizontalAlignment(Element.ALIGN_CENTER);         
  119.                 table.getDefaultCell().setBorder(1);    
  120.             }catch(Exception e){    
  121.                 e.printStackTrace();    
  122.             }    
  123.             return table;    
  124.         }    
  125.         
  126.      public PdfPTable createBlankTable(){    
  127.          PdfPTable table = new PdfPTable(1);    
  128.          table.getDefaultCell().setBorder(0);    
  129.          table.addCell(createCell("", keyfont));             
  130.          table.setSpacingAfter(20.0f);    
  131.          table.setSpacingBefore(20.0f);    
  132.          return table;    
  133.      }    
  134.          
  135.      public void generatePDF() throws Exception{    
  136.         PdfPTable table = createTable(4);    
  137.         table.addCell(createCell("学生信息列表:", keyfont,Element.ALIGN_LEFT,4,false));    
  138.                 
  139.         table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER));    
  140.         table.addCell(createCell("年龄", keyfont, Element.ALIGN_CENTER));    
  141.         table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER));    
  142.         table.addCell(createCell("住址", keyfont, Element.ALIGN_CENTER));    
  143.             
  144.         for(int i=0;i<5;i++){    
  145.             table.addCell(createCell("姓名"+i, textfont));    
  146.             table.addCell(createCell(i+15+"", textfont));    
  147.             table.addCell(createCell((i%2==0)?"男":"女", textfont));    
  148.             table.addCell(createCell("地址"+i, textfont));    
  149.         }    
  150.         document.add(table);    
  151.             
  152.         document.close();    
  153.      }    
  154.          
  155.      public static void main(String[] args) throws Exception {    
  156.          File file = new File("D:\\text.pdf");    
  157.          file.createNewFile();    
  158.         new PDFReport(file).generatePDF();          
  159.     }    
  160.         
  161.         
  162. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值