分类: .net 2012-06-06 21:04 2842人阅读
我在网上找了好久都没找到在封面显示生成的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);
- PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));
-
- 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;
-
- 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;
- }
- }
-
- 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);
-
-
-
-
- Paragraph paragraph = new Paragraph(text, font);
- paragraph.Alignment = Element.ALIGN_CENTER;
- paragraph.SpacingBefore = 5;
- paragraph.SpacingAfter = 5;
- paragraph.SetLeading(1, 2);
- return paragraph;
- }
C#生成PDF页脚第几页共几页
原文:http://www.cnblogs.com/wangcq/p/4892473.html