PDF管理控件Spire.PDF使用教程:如何向現(xiàn)有PDF文件添加頁眉頁腳
更多資源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程
Spire.PDF是一個專業(yè)的PDF組件,能夠獨立地創(chuàng)建、編寫、編輯、操作和閱讀PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API擁有豐富的功能,如安全設置(包括數(shù)字簽名)、PDF文本/附件/圖片提取、PDF文件合并/拆分、元數(shù)據(jù)更新、章節(jié)和段落優(yōu)化、圖形/圖像描繪和插入、表格創(chuàng)建和處理、數(shù)據(jù)導入等等。>>下載Spire.PDF最新試用版
C# PDF 添加頁眉頁腳
在Spire.Pdf命名空間下有一個PdfPageTemplateElement類,用于創(chuàng)建頁眉\頁腳,水印和印章模板。在該模板上創(chuàng)建好頁眉頁腳等信息后,即可應用到PDF文檔,使現(xiàn)有的頁面出現(xiàn)相應信息。
static void Main(string[] args) { //創(chuàng)建PDF文檔 PdfDocument doc = new PdfDocument(); //指定頁面大小 doc.PageSettings.Size = PdfPageSize.A4; //將頁邊距設置為0 doc.PageSettings.Margins = new PdfMargins(0); //創(chuàng)建PdfMargins對象,指定期望設置的頁邊距 PdfMargins margins = new PdfMargins(40, 60, 40, 60); //在文檔模板的頂部和底部應用頁眉頁腳模板 doc.Template.Top = CreateHeaderTemplate(doc, margins); doc.Template.Bottom = CreateFooterTemplate(doc, margins); //在文檔模板的左右部分應用空白模板 doc.Template.Left = new PdfPageTemplateElement(margins.Left, doc.PageSettings.Size.Height); doc.Template.Right = new PdfPageTemplateElement(margins.Right, doc.PageSettings.Size.Height); //在文檔中添加兩頁并寫入文字 PdfPageBase page1 = doc.Pages.Add(); PdfPageBase page2 = doc.Pages.Add(); PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("華文楷體", 15f, FontStyle.Bold), true); page1.Canvas.DrawString("正文開始的地方...", font, PdfBrushes.Black, 0, 0); page2.Canvas.DrawString("正文開始的地方...", font, PdfBrushes.Black, 0, 0); //保存文檔 doc.SaveToFile("HeaderAndFooter.pdf"); } //創(chuàng)建頁眉模板 static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins) { //獲取頁面大小 SizeF pageSize = doc.PageSettings.Size; //創(chuàng)建PdfPageTemplateElement對象headerSpace,即作頁眉模板 PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top); headerSpace.Foreground = false; //聲明x,y兩個float型變量 float x = margins.Left; float y = 0; //在headerSpace中繪制圖片 PdfImage headerImage = PdfImage.FromFile(@"C:\logo.png"); float width = headerImage.Width / 3; float height = headerImage.Height / 3; headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height); //在headerSpace中繪制線段 PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f); headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2); //返回headerSpace return headerSpace; } // 創(chuàng)建頁腳模板 static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins) { //獲取頁面大小 SizeF pageSize = doc.PageSettings.Size; //創(chuàng)建PdfPageTemplateElement對象footerSpace,即頁腳模板 PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom); footerSpace.Foreground = false; //聲明x,y兩個float型變量 float x = margins.Left; float y = 0; //在footerSpace中繪制線段 PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f); footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y); //在footerSpace中繪制文字 y = y + 5; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("黑體", 10f, FontStyle.Bold), true); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); String footerText = "成都冰藍科技有限公司\n電話:028-81705109\n網址:http://www.e-iceblue.cn"; footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Black, x, y, format); //在footerSpace中繪制當前頁碼和總頁碼 PdfPageNumberField number = new PdfPageNumberField(); PdfPageCountField count = new PdfPageCountField(); PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "第{0}頁/共{1}頁", number, count); compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top); SizeF size = font.MeasureString(compositeField.Text); compositeField.Bounds = new RectangleF(pageSize.Width - x - size.Width, y, size.Width, size.Height); compositeField.Draw(footerSpace.Graphics); //返回footerSpace return footerSpace; }
C# 在現(xiàn)成 PDF 中添加頁眉頁腳
在現(xiàn)成的PDF頁面中,坐標體系有以下設定:
- 坐標原點是頁面的左上角定點
- X軸正半軸向右延伸,Y軸正半軸向下延伸
在頁面的空白部分添加圖片、文字或橫線時,需要自己把握現(xiàn)有文檔的邊距,防止頁眉頁腳的內容覆蓋到正文。
方法一:直接在現(xiàn)有文檔繪制頁眉頁腳
static void Main(string[] args) { //加載一個測試文檔 PdfDocument existingPdf = new PdfDocument(); existingPdf.LoadFromFile(@"C:\test.pdf"); //調用DrawHeader方法在現(xiàn)有文檔添加頁眉 DrawHeader(existingPdf); //調用DrawFooter方法在現(xiàn)有文檔添加頁腳 DrawFooter(existingPdf); //保存文檔 existingPdf.SaveToFile("output.pdf"); } //在頁面上方空白部位繪制頁眉 static void DrawHeader(PdfDocument doc) { //獲取頁面大小 SizeF pageSize = doc.Pages[0].Size; //聲明x,y兩個float型變量 float x = 90; float y = 20; for (int i = 0; i < doc.Pages.Count; i++) { //在每一頁的指定位置繪制圖片 PdfImage headerImage = PdfImage.FromFile(@"C:\logo.png"); float width = headerImage.Width / 3; float height = headerImage.Height / 3; doc.Pages[i].Canvas.DrawImage(headerImage, x, y, width, height); //在每一頁的指定位置繪制橫線 PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f); doc.Pages[i].Canvas.DrawLine(pen, x, y + height + 2, pageSize.Width - x, y + height + 2); } } //在頁面下方空白部位繪制頁腳 static void DrawFooter(PdfDocument doc) { //獲取頁面大小 SizeF pageSize = doc.Pages[0].Size; //聲明x,y兩個float型變量 float x = 90; float y = pageSize.Height - 72; for (int i = 0; i < doc.Pages.Count; i++) { //在每一頁的指定位置繪制橫線 PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f); doc.Pages[i].Canvas.DrawLine(pen, x, y, pageSize.Width - x, y); //在每一頁的指定位置繪制文字 y = y + 5; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("黑體", 10f, FontStyle.Bold), true); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); String footerText = "成都冰藍科技有限公司\n電話:028-81705109\n網址:http://www.e-iceblue.cn"; doc.Pages[i].Canvas.DrawString(footerText, font, PdfBrushes.Black, x, y, format); //在每一頁的指定位置當前頁碼和總頁碼 PdfPageNumberField number = new PdfPageNumberField(); PdfPageCountField count = new PdfPageCountField(); PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "第{0}頁/共{1}頁", number, count); compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top); SizeF size = font.MeasureString(compositeField.Text); compositeField.Bounds = new RectangleF(pageSize.Width - x - size.Width, y, size.Width, size.Height); compositeField.Draw(doc.Pages[i].Canvas); } }
方法二:通過模板功能添加頁眉頁腳
static void Main(string[] args) { //加載一個測試文檔 PdfDocument existingPdf = new PdfDocument(); existingPdf.LoadFromFile(@"C:\test.pdf"); //創(chuàng)建新的PDF文檔 PdfDocument newPdf = new PdfDocument(); //設置頁面大小 newPdf.PageSettings.Size = existingPdf.Pages[0].Size; //將頁邊距設置為0 newPdf.PageSettings.Margins = new PdfMargins(0); //創(chuàng)建PdfMargins對象,指定期望設置的頁邊距 //期望邊距需小于或等于現(xiàn)有文檔的實際邊距,否則頁眉頁腳可能覆蓋主體內容 PdfMargins margins = new PdfMargins(90, 72, 90, 72); //在新建文檔的頂部和底部應用頁眉頁腳模板 newPdf.Template.Top = CreateHeaderTemplate(newPdf, margins); newPdf.Template.Bottom = CreateFooterTemplate(newPdf, margins); //在新建文檔的左右部分應用空白模板 newPdf.Template.Left = new PdfPageTemplateElement(margins.Left, newPdf.PageSettings.Size.Height); newPdf.Template.Right = new PdfPageTemplateElement(margins.Right, newPdf.PageSettings.Size.Height); for (int i = 0; i < existingPdf.Pages.Count; i++) { //添加頁面到新建文檔,并將現(xiàn)有文檔當做模板繪制到新頁面 newPdf.Pages.Add().Canvas.DrawTemplate(existingPdf.Pages[i].CreateTemplate(), new PointF(-margins.Left, -margins.Top)); } //保存文檔 newPdf.SaveToFile("output.pdf"); } //創(chuàng)建頁眉模板 static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc, PdfMargins margins) { //獲取頁面大小 SizeF pageSize = doc.PageSettings.Size; //創(chuàng)建PdfPageTemplateElement對象headerSpace,即作頁眉模板 PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top); headerSpace.Foreground = false; //聲明x,y兩個float型變量 float x = margins.Left; float y = 0; //在headerSpace中繪制圖片 PdfImage headerImage = PdfImage.FromFile(@"C:\logo.png"); float width = headerImage.Width / 3; float height = headerImage.Height / 3; headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height); //在headerSpace中繪制橫線 PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f); headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2); //返回headerSpace return headerSpace; } //創(chuàng)建頁腳模板 static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc, PdfMargins margins) { //獲取頁面大小 SizeF pageSize = doc.PageSettings.Size; //創(chuàng)建PdfPageTemplateElement對象footerSpace,即頁腳模板 PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Bottom); footerSpace.Foreground = false; //聲明x,y兩個float型變量 float x = margins.Left; float y = 0; //在footerSpace中繪制橫線 PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f); footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y); //在footerSpace中繪制文字 y = y + 5; PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("黑體", 10f, FontStyle.Bold), true); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left); String footerText = "成都冰藍科技有限公司\n電話:028-81705109\n網址:http://www.e-iceblue.cn"; footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Black, x, y, format); //在footerSpace中繪制當前頁碼和總頁碼 PdfPageNumberField number = new PdfPageNumberField(); PdfPageCountField count = new PdfPageCountField(); PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "第{0}頁/共{1}頁", number, count); compositeField.StringFormat = new PdfStringFormat(PdfTextAlignment.Right, PdfVerticalAlignment.Top); SizeF size = font.MeasureString(compositeField.Text); compositeField.Bounds = new RectangleF(pageSize.Width - x - size.Width, y, size.Width, size.Height); compositeField.Draw(footerSpace.Graphics); //返回footerSpace return footerSpace; }
如果你有任何問題或意見,可在下方評論區(qū)留言,點擊資源列表查看更多教程資源~
*想要購買正版授權的朋友可以咨詢在線客服哦~
掃描關注“慧聚IT”微信公眾號,及時獲取更多產品最新動態(tài)及最新資訊