使用itext创建PDF文档-导出大表格-每页显示标题

import java.io.*;
import java.util.*;
import java.util.List;
 
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
 
/** 
 *  创建Pdf文档
 */
public class HelloPdf{
 
    //先建立Document对象
    Document document = new Document(PageSize.A4, 36.0F, 36.0F, 36.0F, 36.0F);  
 
    String filenameuuid = UUID.randomUUID().toString();
 
    public  void getPdfIo() throws DocumentException, IOException{  
 
 
        //字体的定义
        BaseFont fontChinesetitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);  
 
        BaseFont fontChinesecontent = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
 
        Font FontChinese = new Font(fontChinesetitle, 12, Font.NORMAL);
 
        Font chinese = new Font(fontChinesecontent, 10, Font.NORMAL);   
 
        //保存本地指定路径  
        saveLocal();
 
        //设置页眉
        Paragraph  para  = new Paragraph("会议管理综合查询" , FontChinese);
        HeaderFooter header=new HeaderFooter(para,false);  
        header.setBorder(Rectangle.NO_BORDER);  //设置为没有边框
        header.setAlignment(1);  
        document.setHeader(header);  
 
 
        //设置页脚
        HeaderFooter footer_right = new HeaderFooter(new Paragraph("",chinese), true);      
        footer_right.setAlignment(1);  
        footer_right.setBorder(Rectangle.NO_BORDER);    
        document.setFooter(footer_right);    
 
        //打开文件
        document.open();  
 
        //生成八列表格
        PdfPTable table = new PdfPTable(8);
 
        //设置表格具体宽度  100为整张宽度
        table.setTotalWidth(100);
 
        //设置每一列所占的长度
        table.setWidths(new float[]{6,6,13,12,12,15,24,12});
 
        //设置表头
        String titleName[] = {"序号","上下午","会议时间","会议主题","地  点","主办处/召集人","参会范围","备注"};
 
        for(int i=0;i<titleName.length;i++){
            PdfPCell celltitle = new PdfPCell();
            celltitle.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
            celltitle.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
            Paragraph  paratitle  = new Paragraph( titleName[i] ,chinese);
            paratitle.setAlignment(1);
            celltitle.addElement(paratitle);
            table.addCell(celltitle);
        }
 
        List list = new ArrayList();
        Vector vector = new Vector(); 
        vector.add("1");
        vector.add("上午");
        vector.add("2011-04-11 14:30—2011-04-11 17:00");
        vector.add("会议");
        vector.add("会议室");
        vector.add("书记");
        vector.add("所有人");
        vector.add("空");
 
        for(int i=0; i<50; i++){
            list.add(vector);
        }
 
        //获取要写入的内容
        Vector info = new Vector(); 
 
        for(int i=0; i<list.size(); i++){
            info = (Vector)list.get(i);
            for(int j=0; j<8; j++){
                PdfPCell cellcon = new PdfPCell();
                cellcon.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
                cellcon.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
                Paragraph  paracon  = new Paragraph( (String)info.get( j ) ,chinese);
                paracon.setAlignment(1);
                cellcon.addElement(paracon);
                table.addCell(cellcon);
            }
        }
 
        //将表格的第一行设置为表头 让它在每一页都显示出来
        table.setHeaderRows(1);
 
        //将表格添加到文档中
        document.add(table);
 
        //关闭文档  
        document.close();  
        
        System.out.println("ok");
 
    }  
 
 
    //指定一个文件进行保存 这里吧文件保存到D盘的text.pdf  
    public void saveLocal() throws IOException, DocumentException{  
 
        //直接生成PDF 制定生成到D盘test.pdf  
        File file = new File("D:\\"+filenameuuid+".pdf");  
        file.createNewFile();  
        PdfWriter.getInstance(document, new FileOutputStream(file));  
 
    }
    
    
    //返回路径
    public String returnFileName(){
        return filenameuuid;
    }
 
 
    public static void main(String[] args) {
 
        HelloPdf exportPdf = new HelloPdf();
 
        try {
            exportPdf.getPdfIo();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
 
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值