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

时间:2023-03-10 01:58:49
C#生成PDF页脚第几页共几页
分类: .net 2012-06-06 21:04 2842人阅读 评论(3) 收藏 举报

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

我用的是C#生成PDF页脚第几页共几页这个组件来实现的.net生成PDF。

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

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

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

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

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

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

然后再新建一个类C#生成PDF页脚第几页共几页这个类是用来重写Itext组件的一些方法的。

该类要继承类PdfPageEventHelper和接口IPdfPageEventC#生成PDF页脚第几页共几页

然后重写里面的方法

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