Flying Saucer生成PDF 页码

Flying Saucer很强大支持CSS样式,可以直接将html转换成PDF,而且免费跨平台。
下面我来说一下生成PDF页码
开始我以为生成页码会和Itext一样在程序中实现,但是不遂人愿,程序实现是不对的。后来我查了英文说明网站(因为英语不咋的实在懒得看)
http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features

找到了显示页码的方法是在CSS操作
@page { 
size: 4.18in 6.88in;
margin: 0.25in; 
-fs-flow-top: "header";
-fs-flow-bottom: "footer";
-fs-flow-left: "left";
-fs-flow-right: "right";
border: thin solid black;
padding: 1em;
}

#header {
font: bold serif;
position: absolute; top: 0; left: 0; 
-fs-move-to-flow: "header";
}

#footer {
font-size: 90%; font-style: italic; 
position: absolute; top: 0; left: 0;
-fs-move-to-flow: "footer";
}


#pagenumber:before {
content: counter(page); 
}

#pagecount:before {
content: counter(pages);  
}

还有一段HTML里面代码
<div id="header" style="">Alice's Adventures in Wonderland</div> 
<div id="footer" style=""> Page <span id="pagenumber"/> of <span id="pagecount"/> </div> 

这样就可以显示了,但是仅能显示一页,把“Page <span id="pagenumber"/> of <span id="pagecount"/>”放到第二页时就显示第二页,没办法不给力啊,页数不能控制。
峰回路转我有找到另一个CSS样式
@page { 
    size: 4.18in 6.88in;
    margin: 0.25in; 
    border: thin solid black;
    padding: 1em;
    @bottom-center{
            content:"@xxoo有限公司 版权所有";
            font-family: SimSun; 
        font-size: 9px;
        color:red;
        };
    @top-center { content: element(header) };
    @bottom-right{
            content:"page " counter(page) " of " counter(pages);
            font-family: SimSun; 
        font-size: 9px;
        color:red;
                };
          }
           
          div#myheader {
        display: block;
        position: running(header);
    }          


其实第一种方案是过时的写法,第二种才是现在的写法。
转自 http://topic.csdn.net/u/20111012/11/0e65faba-de91-4104-9aa1-eb7cd9c97ce2.html
我稍微改了一下
@page { 
    
    margin: 0.25in; 
    
    padding: 1em;
    @bottom-center{
            content:"@OOXX  版权所有";
            font-family: SimSun; 
        font-size: 9px;
        color:red;
        };
    @top-center { content: element(header) };
    @bottom-right{
            content:"第" counter(page) "页  共 " counter(pages) "页";
            font-family: SimSun; 
        font-size: 9px;
        color:#000;
                };
          }
           
          div#myheader {
        display: block;
        position: running(header);
    } 


#pagenumber:before {
content: counter(page); 
}

#pagecount:before {
content: counter(pages);  
}

生成的例子



猜你喜欢

转载自wuxiangxin.iteye.com/blog/1550349