WEB工程生成PDF并导出到本地

package com.powersi;


import java.io.File;
import java.io.FileOutputStream;
import java.sql.Blob;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.powersi.Test;
import com.powersi.hygeia.framework.ParameterMapping;
import com.powersi.hygeia.framework.util.DBHelper;
import com.powersi.hygeia.framework.util.UtilFunc;


public class Test {




    Document document = null;// 建立一个Document对象      
    private static Font headFont ;
    private static Font keyFont ;
    private static Font nbspFont;
    private static Font textfont_H ;
    private static Font textfont_B ;
    private static Font textfont_13;
    private static Font textfont_12;
    private static Font textfont_11;
    private static Font textfont_10;
    private static Font textfont_9;
    private static Font textfont_8;
    private static Font textfont_7;
    private static Font textfont_6;
    int maxWidth = 520;


    static{    
        BaseFont bfChinese_H;
        try {    
            /** 
             * 新建一个字体,iText的方法 STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀 
             * UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀 H 代表文字版式是 横版, 相应的 V 代表竖版  
             */
            bfChinese_H = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);    


            headFont = new Font(bfChinese_H, 14, Font.NORMAL);   
            keyFont = new Font(bfChinese_H, 26, Font.BOLD); 
            nbspFont = new Font(bfChinese_H, 13, Font.NORMAL);
            textfont_H = new Font(bfChinese_H, 10, Font.NORMAL); 
            textfont_B = new Font(bfChinese_H, 12, Font.NORMAL); 
            textfont_13 = new Font(bfChinese_H, 13, Font.NORMAL);  
            textfont_12 = new Font(bfChinese_H, 12, Font.NORMAL); 
            textfont_11 = new Font(bfChinese_H, 11, Font.NORMAL); 
            textfont_10 = new Font(bfChinese_H, 10, Font.NORMAL); 
            textfont_9 = new Font(bfChinese_H, 9, Font.NORMAL); 
            textfont_8 = new Font(bfChinese_H, 8, Font.NORMAL); 
            textfont_7 = new Font(bfChinese_H, 7, Font.NORMAL);
            textfont_6 = new Font(bfChinese_H, 6, Font.NORMAL); 


        } catch (Exception e) {             
            e.printStackTrace();    
        }     
    }


    /**
     * 设置页面属性
     * @param file
     */
    public Test(File file) {


        //自定义纸张
        Rectangle rectPageSize = new Rectangle(350, 620);


        // 定义A4页面大小
        //Rectangle rectPageSize = new Rectangle(PageSize.A4);  
        rectPageSize = rectPageSize.rotate();// 加上这句可以实现页面的横置
        document = new Document(rectPageSize,10, 150, 10, 40);


        try {
            PdfWriter.getInstance(document,new FileOutputStream(file));    
            document.open();
        } catch (Exception e) {
            e.printStackTrace();    
        }
    }


    /**
     * 建表格(以列的数量建)
     * @param colNumber
     * @return
     */
    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); 
            table.setSpacingBefore(10);
            table.setWidthPercentage(100);
        }catch(Exception e){    
            e.printStackTrace();    
        }    
        return table;
    }


    /**
     * 建表格(以列的宽度比建)
     * @param widths
     * @return
     */
    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); 
            table.setSpacingBefore(10);
            table.setWidthPercentage(100);
        }catch(Exception e){    
            e.printStackTrace();    
        }    
        return table;    
    }




    /**
     * 表格中单元格
     * @param value
     * @param font
     * @param align
     * @return
     */
    public PdfPCell createCell(String value,Font font,int align){    
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);            
        cell.setHorizontalAlignment(align);        
        cell.setPhrase(new Phrase(value,font));   
        
        return cell;    
    }


    /**
     * 表格中单元格
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param rowspan
     * @return
     */
    public PdfPCell createCell(String value,Font font,int align_v,int align_h,int colspan,int rowspan){
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(align_v);    
        cell.setHorizontalAlignment(align_h);        
        cell.setColspan(colspan); 
        cell.setRowspan(rowspan); 
        cell.setPhrase(new Phrase(value,font));  
        return cell;
    }
    /**
     * 创建无边框表格
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param rowspan
     * @return
     */
    public PdfPCell createCellNotBorder(String value,Font font,int align_v,int align_h,int colspan,int rowspan){
        PdfPCell cell = new PdfPCell();    
        cell.setVerticalAlignment(align_v);    
        cell.setHorizontalAlignment(align_h);        
        cell.setColspan(colspan); 
        cell.setRowspan(rowspan); 
        cell.setPhrase(new Phrase(value,font));  
        cell.setBorderWidth(0f);
        return cell;
    }


    /**
     * 建短语
     * @param value
     * @param font
     * @return
     */
    public Phrase createPhrase(String value,Font font){ 
        Phrase phrase = new Phrase();
        phrase.add(value);
        phrase.setFont(font);
        return phrase;
    }  


    /**
     * 建段落
     * @param value
     * @param font
     * @param align
     * @return
     */
    public Paragraph createParagraph(String value,Font font,int align){ 
        Paragraph paragraph = new Paragraph();
        paragraph.add(new Phrase(value,font));
        paragraph.setAlignment(align);
        return paragraph;
    }
    
    public void generatePDF(String tjid) throws Exception{


    document.add(createParagraph("机构名称",headFont,Element.ALIGN_CENTER));
    document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
    document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
    document.add(createParagraph("文档名称",keyFont,Element.ALIGN_CENTER));
    document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
    document.add(createParagraph(" ",nbspFont,Element.ALIGN_CENTER));
   
    //表格信息
        float[] widths1 = {20f,15f,10f,10f,20f,8f,9f,8f};   //整个表格的列宽,列数。
        PdfPTable table1 = createTable(widths1);   
        // 内容   字体 垂直对齐   横向对齐                                占列     占行
        table1.addCell(createCell("体检号码:", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("姓名", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("婚姻", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("性别", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
        table1.addCell(createCell("年龄", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        
        table1.addCell(createCell("证件号码", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1)); 
        table1.addCell(createCell("联系电话", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1));
        
        table1.addCell(createCell("体检类型", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1)); 
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1)); 
        table1.addCell(createCell("体检日期", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,3,1));
        
        table1.addCell(createCell("单位名称", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,1,1));
        table1.addCell(createCell("", textfont_8, Element.ALIGN_MIDDLE, Element.ALIGN_CENTER,7,1));
        document.add(table1); 
   
        
        
        document.close();
    }


    public static void main(String[] args) throws Exception {
        File file = new File("D:\\test.pdf");
        file.createNewFile();
        String tjid="";
       new Test(file).generatePDF(tjid); 
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值