概述
頁眉頁腳是一篇完整、精緻的文檔的重要組成部分。在頁眉頁腳處,可以呈現的内容很多,如公司名稱、頁碼、工作表名、日期、圖檔,如LOGO、标記等。在之前的文章中介紹了如何通過建立一頁空白PDF頁來添加頁眉到該頁面,包括文字頁面、圖檔頁眉。但是在實際應用中,該方法會有一定局限性,通過測試,下面将介紹C#給現有的PDF文檔添加頁眉頁腳的方法。該方法中,豐富了我們對于添加頁眉頁腳的内容形式,包括添加圖檔、文字、超連結、頁碼等。
使用工具
- Free Spire.PDF for .NET 4.3(社群版)
注:下載下傳該類庫後,注意在程式中添加引用Spire.Pdf.dll(dll檔案可在安裝路徑下的Bin檔案夾中擷取)

C# 代碼步驟(供參考)
步驟 1 :添加using指令
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
步驟 2 :加載測試文檔
//執行個體化PdfDocument類,加載測試文檔
PdfDocument existingPdf = new PdfDocument();
existingPdf.LoadFromFile("Test.pdf");
步驟 3 :添加頁眉頁腳
//調用DrawHeader()方法在現有文檔添加頁眉
DrawHeader(existingPdf);
//調用DrawFooter()方法在現有文檔添加頁腳
DrawFooter(existingPdf);
注:這裡需要自定義方法來分别添加頁眉、頁腳到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("logo.png");
float width = headerImage.Width / 7;
float height = headerImage.Height / 7;
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 = " Website\n https://g20.org/";
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);
}
}
步驟 4 :儲存文檔
existingPdf.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("output.pdf");
代碼完成後,調試運作程式,生成文檔。打開文檔後,效果如下:
全部代碼:
using Spire.Pdf;
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
namespace PdfHeader
{
class Program
{
static void Main(string[] args)
{
//加載一個測試文檔
PdfDocument existingPdf = new PdfDocument();
existingPdf.LoadFromFile("Test.pdf");
//調用DrawHeader()方法在現有文檔添加頁眉
DrawHeader(existingPdf);
//調用DrawFooter()方法在現有文檔添加頁腳
DrawFooter(existingPdf);
//儲存并打開文檔
existingPdf.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("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("logo.png");
float width = headerImage.Width / 7;
float height = headerImage.Height / 7;
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 = " Website\n https://g20.org/";
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);
}
}
}
}
View Code
總結
相較于上篇文章中的添加頁眉的方法,本方法在處理現有的PDF文檔中更具實用性。當然,兩種方法針對不同的程式設計需要,滿足不同的需求,我們在選擇這兩種方法時,可酌情而定。
(本文完)
如需轉載,請注明出處。