C#生成PDF页脚第几页共几页
我在网上找了好久都没找到在封面显示生成的PDF总页数,然后自己摸索着做出来,分享给大家。
我用的是这个组件来实现的.net生成PDF。
首先创建一个工程,然后引用这个组件
然后创建一个页面,添加一个 按钮
然后开始写后台了。。不多说,直接贴代码。
- 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组件的一些方法的。
该类要继承类PdfPageEventHelper和接口IPdfPageEvent
然后重写里面的方法
- 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;
- }
版权声明:本文为wangcq原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。