C#生成PDF页脚第几页共几页

C#生成PDF页脚第几页共几页

分类: .net

我在网上找了好久都没找到在封面显示生成的PDF总页数,然后自己摸索着做出来,分享给大家。


我用的是这个组件来实现的.net生成PDF。


首先创建一个工程,然后引用这个组件



然后创建一个页面,添加一个 按钮


然后开始写后台了。。不多说,直接贴代码。


  1. protected void Button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             PDF();  
  4.         }  
  5.   
  6. private void PDF()  
  7.         {  
  8.             string filePath = "C:\PDF";  
  9.             if (false == Directory.Exists(filePath))  
  10.                 Directory.CreateDirectory(filePath);  
  11.             string filename = filePath + "/PDF.pdf";//设置保存路径  
  12.   
  13.             Document doc = new Document(iTextSharp.text.PageSize.A4, 25, 25, 50, 40);//定义pdf大小,设置上下左右边距  
  14.             PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));//生成pdf路径,创建文件流  
  15.   
  16.             doc.Open();  
  17.             writer.PageEvent = new HeaderAndFooterEvent();  
  18.   
  19.             HeaderAndFooterEvent.PAGE_NUMBER = true;//不实现页眉跟页脚  
  20.             First(doc, writer);//封面页  
  21.               
  22.             doc.NewPage();//新建一页  
  23.   
  24.             PdfHeader(doc, writer);//在新建的一页里面加入数据  
  25.             HeaderAndFooterEvent.PAGE_NUMBER = false;//开始书写页眉跟页脚  
  26.   
  27.   
  28.             writer.Flush();  
  29.             writer.CloseStream = true;  
  30.             doc.Close();  
  31.         }  
  32.   
  33.         private void PdfHeader(Document doc, PdfWriter writer)  
  34.         {  
  35.             string totalStar = string.Empty;  
  36.             writer.PageEvent = new HeaderAndFooterEvent();  
  37.             string tmp = "这个是标题";  
  38.             doc.Add(HeaderAndFooterEvent.InsertTitleContent(tmp));  
  39.         }  
  40.   
  41.         private void First(Document doc, PdfWriter writer)  
  42.         {  
  43.             string tmp = "分析报告";  
  44.             doc.Add(HeaderAndFooterEvent.InsertTitleContent(tmp));  
  45.   
  46.             tmp = "(正文     页,附件 0 页)";  
  47.             doc.Add(HeaderAndFooterEvent.InsertTitleContent(tmp));  
  48.   
  49.             //模版 显示总共页数  
  50.             HeaderAndFooterEvent.tpl = writer.DirectContent.CreateTemplate(100, 100); //模版的宽度和高度  
  51.             PdfContentByte cb = writer.DirectContent;  
  52.             cb.AddTemplate(HeaderAndFooterEvent.tpl, 266, 714);//调节模版显示的位置  
  53.           
  54.         }  

然后再新建一个类这个类是用来重写Itext组件的一些方法的。

该类要继承类PdfPageEventHelper和接口IPdfPageEvent

然后重写里面的方法

    1. public static PdfTemplate tpl = null;//模版  
    2.    public static bool PAGE_NUMBER = false;//为True时就生成 页眉和页脚  
    3.   
    4.    iTextSharp.text.Font font = BaseFontAndSize("黑体", 10, Font.NORMAL, BaseColor.BLACK);  
    5.   
    6.   
    7.    //重写 关闭一个页面时  
    8.    public override void OnEndPage(PdfWriter writer, Document document)  
    9.    {  
    10.        if (PAGE_NUMBER)  
    11.        {  
    12.           Phrase header = new Phrase("PDF测试生成页眉分析报告", font);  
    13.   
    14.           Phrase footer = new Phrase("第" + (writer.PageNumber - 1) + "页/共     页", font);  
    15.           PdfContentByte cb = writer.DirectContent;  
    16.             
    17.            //模版 显示总共页数  
    18.           cb.AddTemplate(tpl, document.Right - 54 + document.LeftMargin, document.Bottom - 8);//调节模版显示的位置  
    19.   
    20.            //页眉显示的位置  
    21.            ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, header,  
    22.                   document.Right - 140 + document.LeftMargin, document.Top + 10, 0);  
    23.   
    24.            //页脚显示的位置  
    25.            ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, footer,  
    26.                   document.Right - 60 + document.LeftMargin, document.Bottom - 10, 0);  
    27.        }  
    28.    }  
    29.   
    30.    //重写 打开一个新页面时  
    31.    public override void OnStartPage(PdfWriter writer, Document document)  
    32.    {  
    33.        if (PAGE_NUMBER)  
    34.        {  
    35.            writer.PageCount = writer.PageNumber-1;  
    36.        }  
    37.    }  
    38.    //关闭PDF文档时发生该事件  
    39.    public override void OnCloseDocument(PdfWriter writer, Document document)  
    40.    {  
    41.        BaseFont bf = BaseFont.CreateFont(@"c:windowsfontsSIMYOU.TTF", BaseFont.IDENTITY_H, false); //调用的字体  
    42.        tpl.BeginText();  
    43.        tpl.SetFontAndSize(bf, 16);//生成的模版的字体、颜色  
    44.        tpl.ShowText((writer.PageNumber - 2).ToString());//模版显示的内容  
    45.        tpl.EndText();  
    46.        tpl.ClosePath();  
    47.    }  
    48.    //定义字体 颜色  
    49.    public static Font BaseFontAndSize(string font_name, int size, int style, BaseColor baseColor)  
    50.    {  
    51.        BaseFont baseFont;  
    52.        BaseFont.AddToResourceSearch("iTextAsian.dll");  
    53.        BaseFont.AddToResourceSearch("iTextAsianCmaps.dll");  
    54.        Font font = null;  
    55.        string file_name = "";  
    56.        int fontStyle;  
    57.        switch (font_name)  
    58.        {  
    59.            case "黑体":  
    60.                file_name = "SIMHEI.TTF";  
    61.                break;  
    62.            case "华文中宋":  
    63.                file_name = "STZHONGS.TTF";  
    64.                break;  
    65.            case "宋体":  
    66.                file_name = "SIMYOU.TTF";  
    67.                break;  
    68.            default:  
    69.                file_name = "SIMYOU.TTF";  
    70.                break;  
    71.        }  
    72.        baseFont = BaseFont.CreateFont(@"c:/windows/fonts/" + file_name, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//字体:黑体   
    73.        if (style < -1)  
    74.        {  
    75.            fontStyle = Font.NORMAL;  
    76.        }  
    77.        else  
    78.        {  
    79.            fontStyle = style;  
    80.        }  
    81.        font = new Font(baseFont, size, fontStyle, baseColor);  
    82.        return font;  
    83.    }  
    84.   
    85.    //定义输出文本  
    86.    public static Paragraph InsertTitleContent(string text)  
    87.    {  
    88.   
    89.        iTextSharp.text.Font font = BaseFontAndSize("华文中宋", 16, Font.BOLD,BaseColor.BLACK);  
    90.          
    91.        //BaseFont bfSun = BaseFont.CreateFont(@"c:windowsfontsSTZHONGS.TTF", BaseFont.IDENTITY_H, false); //调用的字体  
    92.        //Font font = new Font(bfSun, 15);  
    93.   
    94.        Paragraph paragraph = new Paragraph(text, font);//新建一行  
    95.        paragraph.Alignment = Element.ALIGN_CENTER;//居中  
    96.        paragraph.SpacingBefore = 5;  
    97.        paragraph.SpacingAfter = 5;  
    98.        paragraph.SetLeading(1, 2);//每行间的间隔  
    99.        return paragraph;  
    100.    } 
原文地址:https://www.cnblogs.com/wangcq/p/4892473.html