使用C#代碼在PDF文檔添加頁碼的操作指南
向 PDF 文檔添加頁碼不僅實(shí)用,而且具有美觀效果,因?yàn)樗苁刮臋n呈現(xiàn)出類似專業(yè)出版物的精致外觀。無論您處理的是小說的電子版、報(bào)告,還是其他類型的長(zhǎng)篇文檔,添加頁碼都能顯著提高其可讀性和使用價(jià)值。在本文中,您將學(xué)習(xí)如何在 C# 中使用 Spire.PDF for .NET 向 PDF 文檔添加頁碼。
安裝 Spire.PDF for .NET
首先,您需要將 Spire.PDF for .NET 包中包含的 DLL 文件添加為 .NET 項(xiàng)目的引用。這些 DLL 文件可以通過此鏈接下載,或者通過 NuGet 進(jìn)行安裝。
PM> Install-Package Spire.PDF
PDF 坐標(biāo)系統(tǒng)
在使用 Spire.PDF for .NET 操作現(xiàn)有 PDF 文檔時(shí),需要注意坐標(biāo)系統(tǒng)的原點(diǎn)位于頁面的左上角。x 軸向右延伸,y 軸向下延伸。
通常,頁碼會(huì)放置在文檔的頁眉或頁腳區(qū)域。因此,在確定頁碼的合適位置時(shí),必須考慮頁面的尺寸和邊距。

在 C# 中在頁腳添加左對(duì)齊的頁碼
在 Spire.PDF for .NET 庫中,有兩個(gè)可用的類:PdfPageNumberField 和 PdfPageCountField。將它們添加到 PDF 文檔的頁面時(shí),可以獲取并顯示當(dāng)前頁碼以及總頁數(shù)。如果您希望插入類似 “第 X 頁” 或 “第 X 頁,共 Y 頁” 的文本,可以使用 PdfCompositeField 類,它允許您將所需文本與一個(gè)或多個(gè)字段組合成單一字段。
具體示例代碼如下:
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.License;
namespace AddPageNumbersToLeftCorner
{
class Program
{
static void Main(string[] args)
{
// 應(yīng)用許可證密鑰
LicenseProvider.SetLicenseKey("License Key");
// 創(chuàng)建 PdfDocument 對(duì)象
PdfDocument doc = new PdfDocument();
// 加載 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");
// 創(chuàng)建字體、畫刷和畫筆,用于設(shè)置頁碼的顯示樣式
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true);
PdfBrush brush = PdfBrushes.Black;
PdfPen pen = new PdfPen(brush, 1.0f);
// 創(chuàng)建 PdfPageNumberField 對(duì)象和 PdfPageCountField 對(duì)象
PdfPageNumberField pageNumberField = new PdfPageNumberField();
PdfPageCountField pageCountField = new PdfPageCountField();
// 創(chuàng)建 PdfCompositeField 對(duì)象,將頁碼字段和總頁數(shù)字段組合為一個(gè)字段
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);
// 獲取頁面尺寸
SizeF pageSize = doc.Pages[0].Size;
// 設(shè)置復(fù)合字段的位置
compositeField.Location = new PointF(72, pageSize.Height - 45);
// 遍歷文檔中的每一頁
for (int i = 0; i < doc.Pages.Count; i++)
{
// 獲取指定頁面
PdfPageBase page = doc.Pages[i];
// 在指定位置繪制一條直線
page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50);
// 在頁面上繪制復(fù)合字段
compositeField.Draw(page.Canvas);
}
// 保存為新的 PDF 文件
doc.SaveToFile("AddPageNumbersToLeftCorner.pdf");
// 釋放資源
doc.Dispose();
}
}
}在 C# 中在頁腳添加居中對(duì)齊的頁碼
為了將頁腳中的頁碼居中對(duì)齊,關(guān)鍵在于動(dòng)態(tài)計(jì)算文本 “第 X 頁,共 Y 頁” 的寬度。這一步計(jì)算非常重要,因?yàn)樗鼪Q定了頁碼(PdfCompositeField)的 X 坐標(biāo)。為了實(shí)現(xiàn)居中對(duì)齊,X 坐標(biāo)的計(jì)算方法為:用頁面寬度減去頁碼寬度,然后將結(jié)果除以 2,即 (PageWidth - PageNumberWidth) / 2。
具體示例代碼如下:
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.License;
namespace AddPageNumbersToCenter
{
class Program
{
static void Main(string[] args)
{
// 應(yīng)用許可證密鑰
LicenseProvider.SetLicenseKey("License Key");
// 創(chuàng)建 PdfDocument 對(duì)象
PdfDocument doc = new PdfDocument();
// 加載 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");
// 創(chuàng)建字體、畫刷和畫筆,用于設(shè)置頁碼的顯示樣式
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true);
PdfBrush brush = PdfBrushes.Black;
PdfPen pen = new PdfPen(brush, 1.0f);
// 創(chuàng)建 PdfPageNumberField 對(duì)象和 PdfPageCountField 對(duì)象
PdfPageNumberField pageNumberField = new PdfPageNumberField();
PdfPageCountField pageCountField = new PdfPageCountField();
// 創(chuàng)建 PdfCompositeField 對(duì)象,將頁碼字段和總頁數(shù)字段組合為一個(gè)字段
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);
// 遍歷文檔中的每一頁
for (int i = 0; i < doc.Pages.Count; i++)
{
// 獲取指定頁面
PdfPageBase page = doc.Pages[i];
// 獲取頁面尺寸
SizeF pageSize = doc.Pages[i].Size;
// 在指定位置繪制一條直線
page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50);
// 測(cè)量 "Page X of Y" 文本的尺寸
SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count));
// 設(shè)置復(fù)合字段的位置,實(shí)現(xiàn)居中對(duì)齊
compositeField.Location = new PointF((pageSize.Width - pageNumberSize.Width) / 2, pageSize.Height - 45);
// 在頁面上繪制復(fù)合字段
compositeField.Draw(page.Canvas);
}
// 保存為新的 PDF 文件
doc.SaveToFile("AddPageNumbersToCenter.pdf");
// 釋放資源
doc.Dispose();
}
}
}在 C# 中在頁腳添加右對(duì)齊的頁碼
為了將頁腳中的頁碼放置在右側(cè)角落,同樣需要?jiǎng)討B(tài)計(jì)算文本 “第 X 頁,共 Y 頁” 的寬度。因?yàn)轫摯a(PdfCompositeField)的 X 坐標(biāo)是通過用頁面寬度減去頁碼寬度與右側(cè)頁邊距的和來確定的,計(jì)算公式如下:
PageWidth - (PageNumberWidth + RightPageMargin)
具體示例代碼如下:
using Spire.Pdf.AutomaticFields;
using Spire.Pdf.Graphics;
using Spire.Pdf;
using System.Drawing;
using Spire.Pdf.License;
namespace AddPageNumbersToRigthCorner
{
class Program
{
static void Main(string[] args)
{
// 應(yīng)用許可證密鑰
LicenseProvider.SetLicenseKey("License Key");
// 創(chuàng)建 PdfDocument 對(duì)象
PdfDocument doc = new PdfDocument();
// 加載 PDF 文件
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf");
// 創(chuàng)建字體、畫刷和畫筆,用于設(shè)置頁碼的顯示樣式
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", 12, FontStyle.Regular), true);
PdfBrush brush = PdfBrushes.Black;
PdfPen pen = new PdfPen(brush, 1.0f);
// 創(chuàng)建 PdfPageNumberField 對(duì)象和 PdfPageCountField 對(duì)象
PdfPageNumberField pageNumberField = new PdfPageNumberField();
PdfPageCountField pageCountField = new PdfPageCountField();
// 創(chuàng)建 PdfCompositeField 對(duì)象,將頁碼字段和總頁數(shù)字段組合為一個(gè)字段
PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);
// 遍歷文檔中的每一頁
for (int i = 0; i < doc.Pages.Count; i++)
{
// 獲取指定頁面
PdfPageBase page = doc.Pages[i];
// 獲取頁面尺寸
SizeF pageSize = doc.Pages[i].Size;
// 在指定位置繪制一條直線
page.Canvas.DrawLine(pen, 72, pageSize.Height - 50, pageSize.Width - 72, pageSize.Height - 50);
// 測(cè)量 "Page X of Y" 文本的尺寸
SizeF pageNumberSize = font.MeasureString(string.Format("Page {0} of {1}", i + 1, doc.Pages.Count));
// 設(shè)置復(fù)合字段的位置,實(shí)現(xiàn)右對(duì)齊
compositeField.Location = new PointF(pageSize.Width - pageNumberSize.Width - 72, pageSize.Height - 45);
// 在頁面上繪制復(fù)合字段
compositeField.Draw(page.Canvas);
}
// 保存為新的 PDF 文件
doc.SaveToFile("AddPageNumbersToRigthCorner.pdf");
// 釋放資源
doc.Dispose();
}
}
}到此這篇關(guān)于使用C#代碼在PDF文檔添加頁碼的操作指南的文章就介紹到這了,更多相關(guān)C# PDF文檔添加頁碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 獲取硬盤號(hào),CPU信息,加密解密技術(shù)的步驟
這篇文章主要介紹了C# 獲取硬盤號(hào),CPU信息,加密解密技術(shù)的步驟,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2021-01-01
C#二叉搜索樹算法實(shí)現(xiàn)步驟和實(shí)例代碼
二叉搜索樹(Binary?Search?Tree,簡(jiǎn)稱BST)是一種節(jié)點(diǎn)有序排列的二叉樹數(shù)據(jù)結(jié)構(gòu),這篇文章主要介紹了C#二叉搜索樹算法實(shí)現(xiàn)步驟和實(shí)例代碼,需要的朋友可以參考下2024-08-08
C#調(diào)用C++ DLL bool返回值始終為true的問題
這篇文章主要介紹了C#調(diào)用C++ DLL bool返回值始終為true的問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
在C#中自動(dòng)化創(chuàng)建Excel儀表圖的操作代碼
在數(shù)據(jù)驅(qū)動(dòng)的時(shí)代,Excel 儀表圖(Gauge Chart)因其直觀、高效的特點(diǎn),成為業(yè)務(wù)監(jiān)控和績(jī)效評(píng)估的利器,本文我們將深入探討如何利用 C# Excel Chart Automation 的強(qiáng)大能力,通過編程方式在 Excel 中自動(dòng)化創(chuàng)建儀表圖,徹底告別手動(dòng)操作的煩惱,需要的朋友可以參考下2025-09-09
c#實(shí)現(xiàn)winform屏幕截圖并保存的示例
這篇文章主要介紹了c#實(shí)現(xiàn)winform屏幕截圖并保存的示例,需要的朋友可以參考下2014-02-02
C#獲取Word文檔中所有表格的實(shí)現(xiàn)代碼分享
這篇文章主要介紹了C#獲取Word文檔中所有表格的實(shí)現(xiàn)代碼分享,小編親測(cè)可用,需要的朋友可以參考下2014-09-09

