C#生成PDF頁腳第幾頁共幾頁

我在網上找了很久都沒找到在封面顯示生成的PDF總頁數,而後本身摸索着作出來,分享給你們。c#


我用的是這個組件來實現的.net生成PDF。windows


首先建立一個工程,而後引用這個組件ide



而後建立一個頁面,添加一個 按鈕測試


而後開始寫後臺了。。很少說,直接貼代碼。字體

protected void Button1_Click(object sender, EventArgs e)
       {
           PDF();
       } 
?
private void PDF()
        {
            string filePath = "C:\\PDF";
            if (false == Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            string filename = filePath + "/PDF.pdf";//設置保存路徑
 
            Document doc = new Document(iTextSharp.text.PageSize.A4, 25, 25, 50, 40);//定義pdf大小,設置上下左右邊距
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));//生成pdf路徑,建立文件流
 
            doc.Open();
            writer.PageEvent = new HeaderAndFooterEvent();
 
            HeaderAndFooterEvent.PAGE_NUMBER = true;//不實現頁眉跟頁腳
            First(doc, writer);//封面頁
             
            doc.NewPage();//新建一頁
 
            PdfHeader(doc, writer);//在新建的一頁裏面加入數據
            HeaderAndFooterEvent.PAGE_NUMBER = false;//開始書寫頁眉跟頁腳
 
 
            writer.Flush();
            writer.CloseStream = true;
            doc.Close();
        }
 
        private void PdfHeader(Document doc, PdfWriter writer)
        {
            string totalStar = string.Empty;
            writer.PageEvent = new HeaderAndFooterEvent();
            string tmp = "這個是標題";
            doc.Add(HeaderAndFooterEvent.InsertTitleContent(tmp));
        }
 
        private void First(Document doc, PdfWriter writer)
        {
            string tmp = "分析報告";
            doc.Add(HeaderAndFooterEvent.InsertTitleContent(tmp));
 
            tmp = "(正文     頁,附件 0 頁)";
            doc.Add(HeaderAndFooterEvent.InsertTitleContent(tmp));
 
            //模版 顯示總共頁數
            HeaderAndFooterEvent.tpl = writer.DirectContent.CreateTemplate(100, 100); //模版的寬度和高度
            PdfContentByte cb = writer.DirectContent;
            cb.AddTemplate(HeaderAndFooterEvent.tpl, 266, 714);//調節模版顯示的位置
         
        }

而後再新建一個類這個類是用來重寫Itext組件的一些方法的。spa

 

該類要繼承類PdfPageEventHelper和接口IPdfPageEvent.net

而後重寫方法code


public static PdfTemplate tpl = null ; //模版
        public static bool PAGE_NUMBER = false ; //爲True時就生成 頁眉和頁腳
 
        iTextSharp.text.Font font = BaseFontAndSize( "黑體" , 10, Font.NORMAL, BaseColor.BLACK);
 
 
        //重寫 關閉一個頁面時
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            if (PAGE_NUMBER)
            {
               Phrase header = new Phrase( "PDF測試生成頁眉分析報告" , font);
 
               Phrase footer = new Phrase( "第" + (writer.PageNumber - 1) + "頁/共     頁" , font);
               PdfContentByte cb = writer.DirectContent;
               
                //模版 顯示總共頁數
               cb.AddTemplate(tpl, document.Right - 54 + document.LeftMargin, document.Bottom - 8); //調節模版顯示的位置
 
                //頁眉顯示的位置
                ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, header,
                       document.Right - 140 + document.LeftMargin, document.Top + 10, 0);
 
                //頁腳顯示的位置
                ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer,
                       document.Right - 60 + document.LeftMargin, document.Bottom - 10, 0);
            }
        }
 
        //重寫 打開一個新頁面時
        public override void OnStartPage(PdfWriter writer, Document document)
        {
            if (PAGE_NUMBER)
            {
                writer.PageCount = writer.PageNumber-1;
            }
        }
        //關閉PDF文檔時發生該事件
        public override void OnCloseDocument(PdfWriter writer, Document document)
        {
            BaseFont bf = BaseFont.CreateFont( @"c:\windows\fonts\SIMYOU.TTF" , BaseFont.IDENTITY_H, false ); //調用的字體
            tpl.BeginText();
            tpl.SetFontAndSize(bf, 16); //生成的模版的字體、顏色
            tpl.ShowText((writer.PageNumber - 2).ToString()); //模版顯示的內容
            tpl.EndText();
            tpl.ClosePath();
        }
        //定義字體 顏色
        public static Font BaseFontAndSize( string font_name, int size, int style, BaseColor baseColor)
        {
            BaseFont baseFont;
            BaseFont.AddToResourceSearch( "iTextAsian.dll" );
            BaseFont.AddToResourceSearch( "iTextAsianCmaps.dll" );
            Font font = null ;
            string file_name = "" ;
            int fontStyle;
            switch (font_name)
            {
                case "黑體" :
                    file_name = "SIMHEI.TTF" ;
                    break ;
                case "華文中宋" :
                    file_name = "STZHONGS.TTF" ;
                    break ;
                case "宋體" :
                    file_name = "SIMYOU.TTF" ;
                    break ;
                default :
                    file_name = "SIMYOU.TTF" ;
                    break ;
            }
            baseFont = BaseFont.CreateFont( @"c:/windows/fonts/" + file_name, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //字體:黑體
            if (style < -1)
            {
                fontStyle = Font.NORMAL;
            }
            else
            {
                fontStyle = style;
            }
            font = new Font(baseFont, size, fontStyle, baseColor);
            return font;
        }
 
        //定義輸出文本
        public static Paragraph InsertTitleContent( string text)
        {
 
            iTextSharp.text.Font font = BaseFontAndSize( "華文中宋" , 16, Font.BOLD,BaseColor.BLACK);
            
            //BaseFont bfSun = BaseFont.CreateFont(@"c:\windows\fonts\STZHONGS.TTF", BaseFont.IDENTITY_H, false); //調用的字體
            //Font font = new Font(bfSun, 15);
 
            Paragraph paragraph = new Paragraph(text, font); //新建一行
            paragraph.Alignment = Element.ALIGN_CENTER; //居中
            paragraph.SpacingBefore = 5;
            paragraph.SpacingAfter = 5;
            paragraph.SetLeading(1, 2); //每行間的間隔
            return paragraph;
        }

好了,大功告成了!!!繼承

下載地址:http://download.csdn.net/detail/aasswwe/4356788接口

相關文章
相關標籤/搜索