Flying Saucer生成PDF 页码

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

找到了显示页码的方法是在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);
}


其实第一种方案是过时的写法,第二种才是现在的写法。
转自[url]http://topic.csdn.net/u/20111012/11/0e65faba-de91-4104-9aa1-eb7cd9c97ce2.html[/url]
我稍微改了一下

@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);
}

生成的例子

[img]http://dl.iteye.com/upload/attachment/0069/0867/cf1ca8bf-0fb4-3977-bf71-680dc3ae4ee6.png[/img]
[img]http://dl.iteye.com/upload/attachment/0069/0871/71fe8aa6-1e9a-36b1-8a16-f74762eceba4.png[/img]
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值