JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Tools  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
作者 Re:iText 頁尾計算、頁小計 [Re:aurora88]
LiaoLuke





發文: 106
積分: 0
於 2009-05-20 18:14 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
用 PdfPageEventHelper 
//    在 onEndPage 裡加入頁碼的Template (用 PdfTemplate )
//    在 onCloseDocument 裡去把總頁數加入該 template 中); 
      //template.
      PdfPageEventHelper page = new PdfPageEventHelper(){
        /** The PdfTemplate that contains the total number of pages. */
        protected PdfTemplate total;
        /** The font that will be used. */
        protected BaseFont helv;
        /**
         * @see com.lowagie.text.pdf.PdfPageEvent#onOpenDocument(
         *      com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
         */
        public void onOpenDocument(PdfWriter writer, Document document) {
                total = writer.getDirectContent().createTemplate(20, 10);
                total.setBoundingBox(new Rectangle(-10, -10, 20, 10));
                try {
                    helv = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
                } catch (Exception e) {
                        throw new ExceptionConverter(e);
                }
        }
        /**
         * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document)
         */
        public void onEndPage(PdfWriter writer, Document document) {
          PdfContentByte cb = writer.getDirectContent();
          cb.saveState();
          String text = "Page " + writer.getPageNumber() + " of ";
          float textSize1 = helv.getWidthPoint(text, 6);
          float centerPoint =  (document.left() + document.right())/2;
          cb.beginText();
          cb.setFontAndSize(helv, 6);
          cb.setTextMatrix(centerPoint - (textSize1/2), document.bottom() - 10);
          //cb.setTextMatrix(document.left(), document.bottom() - 10);
          cb.showText(text);
          cb.endText();
          cb.addTemplate(total, centerPoint + (textSize1/2), document.bottom() - 10);
        }
        /**
         * @see com.lowagie.text.pdf.PdfPageEvent#onCloseDocument(com.lowagie.text.pdf.PdfWriter,
         *      com.lowagie.text.Document)
         */
        public void onCloseDocument(PdfWriter writer, Document document) {
                total.beginText();
                total.setFontAndSize(helv, 6);
                total.setTextMatrix(0, 0);
                total.showText(String.valueOf(document.getPageNumber() - 1));
                total.endText();
        }
      };


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
3650 iText 頁尾計算、頁小計 aurora88 153 2009-05-19 10:55
4207 Re:iText 頁尾計算、頁小計 LiaoLuke 2448 2009-05-20 18:14
3306 Re:iText 頁尾計算、頁小計 aurora88 9 2009-05-23 12:45
» JWorld@TW »  Java Tools

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8