PdfUtil.java 8.41 KB
Newer Older
1 2 3
package cn.wisenergy.service.common;

import cn.wisenergy.model.app.Volunteer;
4
import com.itextpdf.text.*;
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import java.io.Serializable;
import java.util.List;

/**
 * 创建PDF工具类
 */
public class PdfUtil implements Serializable {
    private static final long serialVersionUID = -8441049720133017215L;
    // 定义全局的字体静态变量
    private static Font titlefont;
    private static Font headfont;
    private static Font keyfont;
    private static Font textfont;

    //序号
    private  Integer id=0;

    // 最大宽度
    private static int maxWidth = 520;

    // 静态代码块
    static {
        try {
            // 不同字体(这里定义为同一种字体:包含不同字号、不同style)
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(bfChinese, 16, Font.BOLD);
            headfont = new Font(bfChinese, 14, Font.BOLD);
            keyfont = new Font(bfChinese, 10, Font.BOLD);
            textfont = new Font(bfChinese, 10, Font.NORMAL);

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


    // 生成PDF文件
46
    public  void  generatePDF(Document document, List<Volunteer> list,String name, String queryRecordTime) throws Exception {
47

48

49 50
        // 表格
        PdfPTable table = createTable(new float[] { 40,80,80, 80, 80, 80, 80, 80, 40, 40, 40 });
51
        table.addCell(createCell("查询时间:"+queryRecordTime+"     "+"用户名:"+name,headfont,Element.ALIGN_LEFT, 11, false));
52 53 54 55 56 57 58 59 60
        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));
        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));
        table.addCell(createCell("投出数", keyfont, Element.ALIGN_CENTER));
liaoanyuan's avatar
liaoanyuan committed
61
        table.addCell(createCell("参考分", keyfont, Element.ALIGN_CENTER));
62 63 64 65 66 67 68 69 70 71 72 73
        table.addCell(createCell("最低位次", keyfont, Element.ALIGN_CENTER));
        for (Volunteer volunteer : list)
            {
            table.addCell(createCell((++id).toString() , textfont));
            table.addCell(createCell(volunteer.getMajorName(), textfont));
            table.addCell(createCell(volunteer.getAcademy(), textfont));
            table.addCell(createCell(volunteer.getCourseDemand(), textfont));
            table.addCell(createCell(volunteer.getNature(), textfont));
            table.addCell(createCell(volunteer.getYearLimit().toString(), textfont));
            table.addCell(createCell(volunteer.getPlanNum().toString(), textfont));
            table.addCell(createCell(null==volunteer.getCastArchivesNum()?"":volunteer.getCastArchivesNum().toString(), textfont));
            table.addCell(createCell(null==volunteer.getLaunchNum()?"":volunteer.getLaunchNum().toString(), textfont));
licc's avatar
licc committed
74
            table.addCell(createCell(null==volunteer.getLowestMark()?"":volunteer.getLowestMark().toString(), textfont));
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
            table.addCell(createCell(null==volunteer.getLowestRank()?"":volunteer.getLowestRank(), textfont));
        }
        document.add(table);
    }


/**------------------------创建表格单元格的方法start----------------------------*/
    /**
     * 创建单元格(指定字体)
     * @param value
     * @param font
     * @return
     */
    public PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }
    /**
     * 创建单元格(指定字体、水平..)
     * @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;
    }
    /**
     * 创建单元格(指定字体、水平居..、单元格跨x列合并)
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @return
     */
    public PdfPCell createCell(String value, 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;
    }
    /**
     * 创建单元格(指定字体、水平居..、单元格跨x列合并、设置单元格内边距)
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param boderFlag
     * @return
     */
    public PdfPCell createCell(String value, 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);
        } else if (boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(0.0f);
            cell.setPaddingBottom(15.0f);
        }
        return cell;
    }
    /**
     * 创建单元格(指定字体、水平..、边框宽度:0表示无边框、内边距)
     * @param value
     * @param font
     * @param align
     * @param borderWidth
     * @param paddingSize
     * @param flag
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        cell.setBorderWidthLeft(borderWidth[0]);
        cell.setBorderWidthRight(borderWidth[1]);
        cell.setBorderWidthTop(borderWidth[2]);
        cell.setBorderWidthBottom(borderWidth[3]);
        cell.setPaddingTop(paddingSize[0]);
        cell.setPaddingBottom(paddingSize[1]);
        if (flag) {
            cell.setColspan(2);
        }
        return cell;
    }
/**------------------------创建表格单元格的方法end----------------------------*/


/**--------------------------创建表格的方法start------------------- ---------*/
    /**
     * 创建默认列宽,指定列数、水平(居中、右、左)的表格
     * @param colNumber
     * @param align
     * @return
     */
    public PdfPTable createTable(int colNumber, int align) {
        PdfPTable table = new PdfPTable(colNumber);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(align);
            table.getDefaultCell().setBorder(1);
        } 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);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }
    /**
     * 创建空白的表格
     * @return
     */
    public PdfPTable createBlankTable() {
        PdfPTable table = new PdfPTable(1);
        table.getDefaultCell().setBorder(0);
        table.addCell(createCell("", keyfont));
        table.setSpacingAfter(20.0f);
        table.setSpacingBefore(20.0f);
        return table;
    }

}