使用C#代碼在PDF文檔添加頁碼的操作指南
向 PDF 文檔添加頁碼不僅實(shí)用,而且具有美觀效果,因?yàn)樗苁刮臋n呈現(xiàn)出類似專業(yè)出版物的精致外觀。無論您處理的是小說的電子版、報(bào)告,還是其他類型的長篇文檔,添加頁碼都能顯著提高其可讀性和使用價(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);
// 測量 "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);
// 測量 "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#在PC上查找連接藍(lán)牙設(shè)備并實(shí)現(xiàn)數(shù)據(jù)傳輸
這篇文章主要介紹了如何用C#在PC上查找連接藍(lán)牙設(shè)備并實(shí)現(xiàn)數(shù)據(jù)傳輸,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C# 文件操作函數(shù) 創(chuàng)建文件 判斷存在
本文列舉了C#中文件操作中常用的函數(shù),創(chuàng)建文件和判斷文件存不存在的基本使用,簡單實(shí)用,希望能幫到大家。2016-05-05
C#實(shí)現(xiàn)簡易計(jì)算器功能(2)(窗體應(yīng)用)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Unity實(shí)現(xiàn)輪盤方式的按鈕滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)輪盤方式的按鈕滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
C#使用base64對(duì)字符串進(jìn)行編碼和解碼的測試
今天小編就為大家分享一篇關(guān)于C#使用base64對(duì)字符串進(jìn)行編碼和解碼的測試,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-02-02
Unity實(shí)現(xiàn)文本轉(zhuǎn)貼圖
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)文本轉(zhuǎn)貼圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05

