直接导出报表数据到PDF

package com.zhwg.core.tool;

import java.io.File;

import java.io.FileOutputStream;

import org.junit.Test;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

/**

  • 导出pdf模版

  • @author zhwg
    */
    public class ExportPdf {
    public static BaseFont bfChinese;
    private static PdfTemplate tpl;
    private float maxWidth = 500;

    static{
    try {
    //bfChinese = BaseFont.createFont(“STSong-Light”,“UniGB-UCS2-H”,BaseFont.NOT_EMBEDDED);
    bfChinese = getBaseFont();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public ExportPdf(){}

    public PagePdf newPagePdf(float x, float y){
    return new PagePdf(x,y);
    }

    //页码事件
    private static class PagePdf extends PdfPageEventHelper{
    private float x;//
    private float y;//

     public PagePdf(float x, float y){
     	this.x = x;
     	this.y = y;
     }
     /**
      * 重写PdfPageEventHelper中的onOpenDocument方法
      */
     public void onOpenDocument(PdfWriter writer, Document document) {
     	tpl = writer.getDirectContent().createTemplate(100, 100);
     }
     /**
      * 重写PdfPageEventHelper中的onEndPage方法
      */
     public void onEndPage(PdfWriter writer, Document document) {
         // 新建获得用户页面文本和图片内容位置的对象
         PdfContentByte cb = writer.getDirectContent();
         cb.saveState();// 保存图形状态
         String text = "第" + writer.getPageNumber() + "页,共";
         // 获取点字符串的宽度
         float textSize = bfChinese.getWidthPoint(text, 8);
         cb.beginText();
         // 设置随后的文本内容写作的字体和字号
         cb.setFontAndSize(bfChinese, 8);
         // 定位'X/'
         cb.setTextMatrix(x, y);
         cb.showText(text);
         cb.endText();
         cb.addTemplate(tpl, x + textSize, y);// 将模板加入到内容(content)中- // 定位'Y'
         cb.stroke();  
         cb.restoreState();
         cb.closePath();//sanityCheck();
     }
    
     /**
      * 重写PdfPageEventHelper中的onCloseDocument方法
      */
     public void onCloseDocument(PdfWriter writer, Document document) {
     	//关闭document的时候获取总页数,并把总页数按模版写道之前预留的位置  
         tpl.beginText();  
         tpl.setFontAndSize(bfChinese, 8);  
         tpl.showText(Integer.toString(writer.getPageNumber() - 1)+"页");  
         tpl.endText();  
         tpl.closePath();//sanityCheck();
     }
    

    }

    public static BaseFont getBaseFont(){
    BaseFont bfChinese = null;
    try {
    String prefixFont = “”;
    String os = System.getProperties().getProperty(“os.name”);
    if(os.startsWith(“win”) || os.startsWith(“Win”)){
    prefixFont = “C:\Windows\Fonts” + File.separator;
    }else {
    prefixFont = “/usr/share/fonts/chinese” + File.separator;
    }
    bfChinese = BaseFont.createFont(prefixFont + “simsun.ttc,0”, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return bfChinese;
    }

    public void setMaxWidth(float maxWidth){
    this.maxWidth = maxWidth;
    }

    public PdfPCell createCell(Object value, Font font, int align) {
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setHorizontalAlignment(align);
    cell.setPhrase(new Phrase(StringUtil.convertString(value), font));
    return cell;
    }

    public PdfPCell createCell2(Object value, Font font, int align, float height) {
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setHorizontalAlignment(align);
    cell.setPhrase(new Phrase(StringUtil.convertString(value), font));
    cell.setFixedHeight(height);
    return cell;
    }

    public PdfPCell createCell(Object value, Font font) {
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setPhrase(new Phrase(StringUtil.convertString(value), font));
    return cell;
    }

    public PdfPCell createCell(Object 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(StringUtil.convertString(value), font));
    return cell;
    }

    public PdfPCell createCell(Object 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(StringUtil.convertString(value), font));
    cell.setPadding(3.0f);
    if (!boderFlag) {
    cell.setBorder(0);
    cell.setPaddingTop(1.0f);
    cell.setPaddingBottom(8.0f);
    }
    return cell;
    }
    public PdfPCell createCellS(Object value, Font font, int align, int colspan,
    boolean boderFlag) {
    PdfPCell cell = new PdfPCell();
    cell.setHorizontalAlignment(align);
    cell.setColspan(colspan);
    cell.setPhrase(new Phrase(StringUtil.convertString(value), font));
    cell.setPadding(3.0f);
    if (!boderFlag) {
    cell.setBorder(0);
    cell.setPaddingTop(1.0f);
    cell.setPaddingBottom(8.0f);
    }
    return cell;
    }

    public PdfPCell createCell(Phrase phrase, 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(phrase);
    cell.setPadding(3.0f);
    if (!boderFlag) {
    cell.setBorder(0);
    cell.setPaddingTop(1.0f);
    cell.setPaddingBottom(8.0f);
    }
    return cell;
    }

    public PdfPCell createCell2(Object value, Font font, int align,
    int colspan, boolean boderFlag, float height) {
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setHorizontalAlignment(align);
    cell.setColspan(colspan);
    cell.setPhrase(new Phrase(StringUtil.convertString(value), font));
    cell.setPadding(3.0f);
    cell.setFixedHeight(height);
    if (!boderFlag) {
    cell.setBorder(0);
    cell.setPaddingTop(1.0f);
    cell.setPaddingBottom(8.0f);
    }
    return cell;
    }

    public PdfPCell createCellR(Object value, Font font, int align, int rowspan) {
    PdfPCell cell = new PdfPCell();
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
    cell.setHorizontalAlignment(align);//水平居中 Element.ALIGN_CENTER
    cell.setPhrase(new Phrase(StringUtil.convertString(value), font));
    cell.setRowspan(rowspan);
    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("", new Font(bfChinese, 8, Font.NORMAL)));
    table.setSpacingAfter(20.0f);
    table.setSpacingBefore(20.0f);
    return table;
    }

    public static String leftPad(String str, int i) {
    int addSpaceNo = i - str.length();
    String space = “”;
    for (int k = 0; k < addSpaceNo; k++) {
    space = " " + space;
    }
    String result = space + str;
    return result;
    }
    @Test
    public void exportPdf(String filePath) throws Exception {
    Font headfont = new Font(bfChinese, 18, Font.BOLD);// 设置字体大小
    Font textfont = new Font(bfChinese, 10, Font.NORMAL);// 设置字体大小
    Document document = new Document(new Rectangle(842F, 595F), 20, 20, 20, 50);//打印纸张 PageSize.A4-横向,左边距,右边距,上边距,下边距
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(“D:\3-4.pdf”));
    writer.setPageEvent(new PagePdf(740,565));//设置页眉 -包括显示坐标
    document.open();
    PdfPTable table = createTable(12);// 创建列数
    // 表格宽度
    int width[] = {8, 19, 8, 26, 10, 10, 13, 13, 13, 13, 12, 12 };// 设置每列宽度比例
    table.setWidths(width);
    // 表头重复
    table.setHeaderRows(5);
    table.setHeadersInEvent(true);// 每页都会显示表头

     table.addCell(createCell("表3-3", textfont, Element.ALIGN_LEFT, 12, false));
     //添加下划线
     Phrase director = new Phrase();  
     Chunk name = new Chunk(" 2018 ",headfont);  
     name.setUnderline(0.2f, -2f);  
     director.add(name);
     director.add(new Chunk("年",headfont));  
     name = new Chunk(" 1 ",headfont);  
     name.setUnderline(0.2f, -2f);  
     director.add(name);
     director.add(new Chunk("季度已完工程数量表",headfont));  
     director.setLeading(24);  
     table.addCell(createCell(director, headfont, Element.ALIGN_CENTER, 12, false));
     //table.addCell(createCell("2018 年 1 季度已完工程数量表", headfont, Element.ALIGN_CENTER, 12, false));
     
     table.addCell(createCell("工程名称:呼和浩特市城市轨道交通2号线一期工程 ", textfont, Element.ALIGN_LEFT, 4, false));
     table.addCell(createCell("标段编号:08", textfont, Element.ALIGN_CENTER, 2, false));
     table.addCell(createCell("单位工程名称:单位工程名称~单位工程名称区间", textfont, Element.ALIGN_CENTER, 4, false));
     table.addCell(createCell("截止日期:2018-03-25", textfont, Element.ALIGN_CENTER, 2, false));
     
     table.addCell(createCellR("编号", textfont, Element.ALIGN_CENTER, 2));
     table.addCell(createCellR("工程项目", textfont, Element.ALIGN_CENTER, 2));
     table.addCell(createCellR("单位", textfont, Element.ALIGN_CENTER, 2));
     table.addCell(createCellR("施工图工程总量", textfont, Element.ALIGN_CENTER, 2));
     table.addCell(createCell("本期完成", textfont, Element.ALIGN_CENTER, 2));
     table.addCell(createCell("本年完成", textfont, Element.ALIGN_CENTER, 2));
     table.addCell(createCell("开累完成", textfont, Element.ALIGN_CENTER, 2));
     table.addCell(createCell("剩余", textfont, Element.ALIGN_CENTER, 2));
     
     table.addCell(createCell("数量", textfont, Element.ALIGN_CENTER));
     table.addCell(createCell("%", textfont, Element.ALIGN_CENTER));
     table.addCell(createCell("数量", textfont, Element.ALIGN_CENTER));
     table.addCell(createCell("%", textfont, Element.ALIGN_CENTER));
     table.addCell(createCell("数量", textfont, Element.ALIGN_CENTER));
     table.addCell(createCell("%", textfont, Element.ALIGN_CENTER));
     table.addCell(createCell("数量", textfont, Element.ALIGN_CENTER));
     table.addCell(createCell("%", textfont, Element.ALIGN_CENTER));
     
     for (int i = 0; i < 100; i++) {
     	table.addCell(createCell("3.2.1.2.5", textfont, Element.ALIGN_LEFT));
     	table.addCell(createCell("项目名称" + i, textfont));
     	table.addCell(createCell("双延米", textfont));
     	table.addCell(createCell("工程总量" + i, textfont));
     	table.addCell(createCell("A" + i, textfont));
     	table.addCell(createCell("B" + i, textfont));
     	table.addCell(createCell("C" + i, textfont));
     	table.addCell(createCell("D" + i, textfont));
     	table.addCell(createCell("E" + i, textfont));
     	table.addCell(createCell("F" + i, textfont));
     	table.addCell(createCell("G" + i, textfont));
     	table.addCell(createCell("H" + i, textfont));
     }
     document.add(table);
     document.close();
    

    }

    public static void main(String[] args) {
    try {
    long t1 = System.currentTimeMillis();
    System.out.println(“start==========”+t1);

     	new ExportPdf().exportPdf("D:\\3-4.pdf");
     	
     	long t2 = System.currentTimeMillis();
     	System.out.println("end=========="+t2);
      	System.out.println("耗时=========="+(t2-t1));
     } catch (Exception e) {
     	e.printStackTrace();
     }
    

    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值