itext高版本的pdf的页眉和页脚信息显示方法

本次对itext的研究主要针对的是pdf 的table,主要应用于打印单据。

框架为jfinal,表头信息中有动态数据,比如:年月日等。

下面直接贴代码:

/**
     * 前台调用打印方法
     * LMM
     *
     */
    public void printStorage(){
        try {
            Integer id=getParaToInt("pid",0);//获取前台传数据
            List stolist=Stordetail.dao.find("select s.*,st.*,p.pub_name,e.edi_name,i.iss_name from stordetail s " +
                    " left join publish p on s.pub_id=p.pub_id left join edition e on s.edi_id=e.edi_id" +
                    " left join storsummary st on s.stsu_id=st.stsu_id " +
                    " left join issue i on s.iss_id=i.iss_id where s.stsu_id="+id+" order by det_id desc");//需要显示的数据
            Storsummary stsu=Storsummary.dao.findById(id);//需要显示的数据
            //start print
            Rectangle rec = new Rectangle(240*2.5f,140*2.5f); //自定义纸张大小
            Document document = new Document(rec,10,10,50,50);//纸张大小,marginleft,marginright,margintop,marginbottom
            //设置输出的位置并把对象装入输出对象中
            PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream(PathKit.getWebRootPath() + "/temp/printStorage.pdf"));
            //设置pdf每页的页眉和页脚
            writer.setPageEvent(new HeadFootInfoPdfPageEvent(stolist,stsu));
            //打开文档
            document.open();
            //设置中文字体
            BaseFont baseFont = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);//配置中文
            //字体和字号
            Font font = new Font(baseFont, 12, Font.NORMAL);
            //title
            String[] date=stolist.get(0).getDate("stsu_data").toString().split("-");
            //table start包括table的每列的宽度
            PdfPTable table = new PdfPTable(new float[]{20f,70f,30f,50f,100f});
            table.setSpacingBefore(5);//
            table.setSpacingAfter(5);
             table.setHeaderRows(1);//设置每页的table都要显示head
            //head
            PdfPHeaderCell head=new PdfPHeaderCell();
            PdfPCell c1 = new PdfPCell(new Phrase("序号", font));
            c1.addHeader(head);//列设置为head
            table.addCell(c1).setHorizontalAlignment(1);
            PdfPCell c2 = new PdfPCell(new Phrase("入库品种版本", font));
            c2.addHeader(head);
            table.addCell(c2).setHorizontalAlignment(1);
            PdfPCell c3 = new PdfPCell(new Phrase("期数", font));
            c3.addHeader(head);
            table.addCell(c3).setHorizontalAlignment(1);
            PdfPCell c5 = new PdfPCell(new Phrase("数量", font));
            c5.addHeader(head);
            table.addCell(c5).setHorizontalAlignment(1);
            PdfPCell c6 = new PdfPCell(new Phrase("备注", font));
            c6.addHeader(head);
            table.addCell(c6).setHorizontalAlignment(1);
            //value
            for(int i=0;i                 table.addCell(new PdfPCell(new Phrase(""+(i+1)+"", font))).setHorizontalAlignment(1);
                table.addCell(new PdfPCell(new Phrase(""+stolist.get(i).getStr("pub_name")
                        +"-"+stolist.get(i).getStr("edi_name")+"", font))).setHorizontalAlignment(1);
                table.addCell(new PdfPCell(new Phrase(""+stolist.get(i).getStr("iss_name")+"期", font))).setHorizontalAlignment(1);
                table.addCell(new PdfPCell(new Phrase(""+stolist.get(i).getInt("det_num")+"", font))).setHorizontalAlignment(1);
                table.addCell(new PdfPCell(new Phrase("", font))).setHorizontalAlignment(1);
            }
            document.add(table);//table end
            //关闭文档
            document.close();
            //显示路径
            redirect(getRequest().getScheme() + "://" +getRequest().getServerName() + ":" +
            getRequest().getServerPort() + getRequest().getContextPath()+"/temp/printStorage.pdf");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

这个class是一个内部的class,如果你要设置的页眉和页脚只是静态内容的话,可以新建一个public class,由于我个人需要显示一些查询的数据作为页眉或者页脚,jfinal显示内容的时候必须继承Controller,而下面的class也需要继承一个类,因为不能继承两个class,故建为内部类,代码如下:

/**
     * 设置打印单据的头部和底部信息
     * @author LMM
     *
     */
    class HeadFootInfoPdfPageEvent extends PdfPageEventHelper{
        //自定义传参数
        protected List stolist=new ArrayList();//查出的入库list
        protected Storsummary stsu=new Storsummary();//入库汇总
          //无参构造方法
        public HeadFootInfoPdfPageEvent() {
            super();
            // TODO Auto-generated constructor stub
        }
        //有参构造方法
        public HeadFootInfoPdfPageEvent(List stolist, Storsummary stsu) {
            super();
            this.stolist = stolist;
            this.stsu = stsu;
        }
        //实现页眉和页脚的方法
        public void onEndPage(PdfWriter writer, Document document) {  
            try{
                String[] date=stolist.get(0).getDate("stsu_data").toString().split("-");
                PdfContentByte headAndFootPdfContent = writer.getDirectContent();  
                headAndFootPdfContent.saveState();  
                headAndFootPdfContent.beginText();
                //设置中文
                BaseFont bfChinese =  BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
                headAndFootPdfContent.setFontAndSize(bfChinese,12);  
                //文档页头信息设置  
                float x = document.top(-20);
                float x1 = document.top(-5);
                //页头信息中间  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER,  
                                    "******责任公司入库单",  
                                   (document.right() + document.left())/2,  
                                   x, 0);  
                //页头信息左面  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_LEFT,  
                        date[0]+"年"+date[1]+"月"+date[2]+"日",  
                                   document.left()+100, x1, 0);  
              //页头信息中间  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER,  
                        "入库单号:"+stsu.getStr("stsu_code")+"",  
                                   (document.right() + document.left())/2,  
                                   x1, 0);  
                //页头信息右面  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_RIGHT,  
                                   " 单位:册",  
                                   document.right()-100, x1, 0);  
                //文档页脚信息设置  
                float y = document.bottom(-20);  
                //页脚信息左面  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_LEFT,  
                                   "负责人:",  
                                   document.left()+100, y, 0);  
                //页脚信息中间  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_CENTER,  
                                    "   库管员:    ",  
                                   (document.right() + document.left())/2,  
                                   y, 0);  
                //页脚信息右面  
                headAndFootPdfContent.showTextAligned(PdfContentByte.ALIGN_RIGHT,  
                                   " 经手人:",  
                                   document.right()-100, y, 0);  
                headAndFootPdfContent.endText();  
                headAndFootPdfContent.restoreState();  
            }catch(DocumentException de) {  
                de.printStackTrace();  
            }catch(IOException de) {  
                de.printStackTrace();  
            }  
        }  
    } 

你可能感兴趣的:(jfinal)