|
02-28-13, 10:54 AM | #1 |
God of Gamer
註冊日期: Sep 2002
文章: 10,915
|
C# PDF itextSharp 5.3.5.0如何加HEADER同FOOTER
Source
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; namespace PDF { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var doc1 = new Document(PageSize.A4,50,50,50,50); MemoryStream Memory = new MemoryStream(); PdfWriter PdfWriter = PdfWriter.GetInstance(doc1, Memory); BaseFont bfChinese = BaseFont.CreateFont(@"c:\windows\Fonts\kaiu.ttf",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font ChFont = new Font(bfChinese, 12); Font ChFont_blue = new Font (bfChinese,40,Font.NORMAL,new BaseColor(51,0,153)); Font ChFont_msg = new Font(bfChinese, 12, Font.ITALIC, BaseColor.RED); doc1.Open(); //PdfPTable table = new PdfPTable(5);// (new float[] { 2, 1, 1, 3 }); PdfPTable table = new PdfPTable((new float[] { 2, 1, 1, 3 })); table.TotalWidth = 566f; table.LockedWidth = true; table.AddCell(""); table.AddCell(""); table.AddCell(""); table.AddCell(""); doc1.Add(table); doc1.Close(); Response.Clear(); Response.AddHeader("Content-Disposition", "attachment;filename=pdfExample.pdf"); Response.ContentType = "application/octet-stream"; Response.OutputStream.Write(Memory.GetBuffer(), 0, Memory.GetBuffer().Length); Response.OutputStream.Flush(); Response.OutputStream.Close(); Response.Flush(); Response.End(); } } public class TwoColumnHeaderFooter : PdfPageEventHelper { PdfContentByte cb; PdfTemplate template; BaseFont bf = null; DateTime PrinteTime = DateTime.Now; public override void OnOpenDocument(PdfWriter writer, Document document) { { try { PrinteTime = DateTime.Now; bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb = writer.DirectContent; template = cb.CreateTemplate(50, 50); } catch (DocumentException de) { } catch (System.IO.IOException ioe) { } } } public override void OnEndPage(PdfWriter writer, Document document) { base.OnEndPage(writer, document); int pageN = writer.PageNumber; string text = "Page " + pageN + " of "; float len = bf.GetWidthPoint(text, 8); Rectangle pageSize = document.PageSize; cb.SetRGBColorFill(100, 100, 100); cb.BeginText(); cb.SetFontAndSize(bf, 8); cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,"NTU C&INC",pageSize.GetRight(40),pageSize.GetTop(30),0); cb.SetTextMatrix(pageSize.GetLeft(40),pageSize.GetBottom(30)); cb.ShowText(text); cb.EndText(); cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30)); cb.BeginText(); cb.SetFontAndSize(bf, 8); cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,"Printed On "+PrinteTime.ToString(),pageSize.GetRight(40),pageSize.GetBottom(30),0); cb.EndText(); } public override void OnCloseDocument(PdfWriter writer, Document document) { base.OnCloseDocument(writer, document); template.BeginText(); template.SetFontAndSize(bf, 8); template.SetTextMatrix(0,0); template.ShowText("" + (writer.PageNumber - 1)); template.EndText(); } } } 求教,雖然上面已經收貨,但想再做好尐,依家想做埋HEADER同FOOTER,但出現問題 基本上是跟這個網做 http://www.cc.ntu.edu.tw/chinese/epa...01220_1509.htm 這部份是沒有問題 接著跟這裡做Header同Footer http://www.cc.ntu.edu.tw/chinese/epa...0320_1610.html 但做不到,其中不明白這句是甚麼意思 >>"所以我們先建立Page Event class" 其實是要怎樣建立? 我的情況是在已經開了個叫pdf的project,然後在defalut.aspx.cs中輸入 http://www.cc.ntu.edu.tw/chinese/epa...01220_1509.htm 的code 跟著是要在defalut.apxs.cs中加入 http://www.cc.ntu.edu.tw/chinese/epa...0320_1610.html 的code 例如這樣 public partial class _Default : System.Web.UI.Page { } public class TwoColumnHeaderFooter : PdfPageEventHelper { } 還是要在同一個Project中開新增一個class File,然後將所有code打落去呢?? 兩種方法也做過,但都係冇嘢出…… 另外也參考過 http://www.mazsoft.com/blog/post/200...F-library.aspx 也試了將所有CODE放在一起或在同一個PROJECT裡分成不同的CLASS FILE 不過在分成不同的CLASS FILE的情況下 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SendOutPDF(new CustomReports().CreatePDF("Title: Sample 1")); } 出現紅色說找不到這個CLASS 而將所有CODE放在一起時又說已有項目宣告了 (好在唔係寫Code搵食)
__________________
幸せそうな人達をみていると、自分も幸せな雰囲気になれるんですよ 此篇文章於 02-28-13 11:07 AM 被 RPG之鬼 編輯。 |
02-28-13, 06:02 PM | #2 |
Junior Member
註冊日期: Apr 2010
文章: 89
|
我睇應該係個 PdfWriter 未加番個 PdfPageEventHelper 落去?
根據你的 code, 在 Page_Load 的 PdfWriter PdfWriter = PdfWriter.GetInstance(doc1, Memory); 後加上: TwoColumnHeaderFooter PageEventHandler = new TwoColumnHeaderFooter(); PDFWriter.PageEvent = PageEventHandler; 試試? |
02-28-13, 11:43 PM | #3 | |
God of Gamer
註冊日期: Sep 2002
文章: 10,915
|
引用:
聽日返公再試試
__________________
幸せそうな人達をみていると、自分も幸せな雰囲気になれるんですよ |
|
03-01-13, 11:48 AM | #4 | |
God of Gamer
註冊日期: Sep 2002
文章: 10,915
|
引用:
你掂喎!一加尐CODE就得!! 超THX
__________________
幸せそうな人達をみていると、自分も幸せな雰囲気になれるんですよ |
|
03-01-13, 01:48 PM | #5 |
Junior Member
註冊日期: Apr 2010
文章: 89
|
我做 cd-rom 多 幫到就得 |