java 生成pdf文件(转,测试…

来源:互联网 发布:药物研发软件 编辑:程序博客网 时间:2024/06/06 09:39
package com.testPdf;

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; 
importcom.lowagie.text.DocumentException; 
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.pdf.BaseFont; 
import com.lowagie.text.pdf.PdfCell; 
import com.lowagie.text.pdf.PdfPCell; 
import com.lowagie.text.pdf.PdfPRow; 
import com.lowagie.text.pdf.PdfPTable; 
import com.lowagie.text.pdf.PdfWriter; 
//importcom.sun.java_cup.internal.internal_error; 
 
public class CreatePdf{ 
    Document document = newDocument();// 建立一个Document对象    
     
    private static Fontheadfont ;// 设置字体大小 
    private static Fontkeyfont;// 设置字体大小 
    private static Fonttextfont;// 设置字体大小 
     
 
     
   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);//设置字体大小 
           keyfont =new Font(bfChinese, 8, Font.BOLD);// 设置字体大小 
           textfont =new Font(bfChinese, 8, Font.NORMAL);//设置字体大小 
       } catch (Exception e) {        
          e.printStackTrace(); 
       }  
   
     
     
    public CreatePdf(Filefile) {        
       document.setPageSize(PageSize.A4);//设置页面大小 
        try { 
          PdfWriter.getInstance(document,newFileOutputStream(file)); 
          document.open();  
       } catch (Exception e) { 
          e.printStackTrace(); 
       }  
        
        
   
    int maxWidth =520; 
     
     
     publicPdfPCell createCell(String value,com.lowagie.text.Font font,intalign){ 
        PdfPCell cell = newPdfPCell(); 
       cell.setVerticalAlignment(Element.ALIGN_MIDDLE);       
       cell.setHorizontalAlignment(align);    
        cell.setPhrase(newPhrase(value,font)); 
       return cell; 
   
     
     publicPdfPCell createCell(String value,com.lowagie.text.Fontfont){ 
        PdfPCell cell = newPdfPCell(); 
       cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
       cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
        cell.setPhrase(newPhrase(value,font)); 
       return cell; 
   
 
     publicPdfPCell createCell(String value,com.lowagie.text.Font font,intalign,int colspan){ 
        PdfPCell cell = newPdfPCell(); 
       cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
       cell.setHorizontalAlignment(align);    
       cell.setColspan(colspan); 
        cell.setPhrase(newPhrase(value,font)); 
       return cell; 
   
    public PdfPCellcreateCell(String value,com.lowagie.text.Font font,int align,intcolspan,boolean boderFlag){ 
        PdfPCell cell = newPdfPCell(); 
       cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
       cell.setHorizontalAlignment(align);    
       cell.setColspan(colspan); 
        cell.setPhrase(newPhrase(value,font)); 
       cell.setPadding(3.0f); 
       if(!boderFlag){ 
           cell.setBorder(0); 
           cell.setPaddingTop(15.0f); 
           cell.setPaddingBottom(8.0f); 
        
       return cell; 
   
     publicPdfPTable createTable(int colNumber){ 
       PdfPTable table = newPdfPTable(colNumber); 
       try{ 
          table.setTotalWidth(maxWidth); 
          table.setLockedWidth(true); 
          table.setHorizontalAlignment(Element.ALIGN_CENTER);     
          table.getDefaultCell().setBorder(1); 
       }catch(Exception e){ 
          e.printStackTrace(); 
      
       return table; 
   
     publicPdfPTable createTable(float[] widths){ 
           PdfPTabletable = new PdfPTable(widths); 
          try{ 
             table.setTotalWidth(maxWidth); 
             table.setLockedWidth(true); 
             table.setHorizontalAlignment(Element.ALIGN_CENTER);     
             table.getDefaultCell().setBorder(1); 
          }catch(Exception e){ 
             e.printStackTrace(); 
          
           returntable; 
      
     
     publicPdfPTable createBlankTable(){ 
        PdfPTable table = newPdfPTable(1); 
       table.getDefaultCell().setBorder(0); 
        table.addCell(createCell("",keyfont));         
       table.setSpacingAfter(20.0f); 
       table.setSpacingBefore(20.0f); 
        returntable; 
    
     
     publicvoid generatePDF() throws Exception{ 
       PdfPTable table =createTable(4); 
       table.addCell(createCell("学生信息列表:",keyfont,Element.ALIGN_LEFT,4,false)); 
           
       table.addCell(createCell("姓名", keyfont,Element.ALIGN_CENTER)); 
       table.addCell(createCell("年龄", keyfont,Element.ALIGN_CENTER)); 
       table.addCell(createCell("性别", keyfont,Element.ALIGN_CENTER)); 
       table.addCell(createCell("住址", keyfont,Element.ALIGN_CENTER)); 
        
       for(inti=0;i<5;i++){ 
          table.addCell(createCell("姓名"+i,textfont)); 
          table.addCell(createCell(i+15+"",textfont)); 
          table.addCell(createCell((i%2==0)?"男":"女",textfont)); 
          table.addCell(createCell("地址"+i,textfont)); 
      
       document.add(table); 
        
       document.close(); 
    
     
     publicstatic void main(String[] args) throws Exception{ 
   System.out.println("begin");
        File file = newFile("e:\\text.pdf"); 
       file.createNewFile(); 
       new CreatePdf(file).generatePDF();     
       System.out.println("end");
   
     
     

原创粉丝点击