如何在PDF中使用C#添加頁碼
"便攜式文件格式",或 PDF,是一種由 Adobe 創建的檔案格式。 在展示需要對文字和圖片進行格式化的論文時,PDF 文件非常方便。 在現今世界中,PDF 檔案是不可或缺的,並被所有企業部門用來建立文件和開立發票。 由於目前市場上有多種 PDF 函式庫,建立 PDF 實際上已成為一種本能。為了選擇適合您的 PDF 函式庫,在使用其中一個函式庫進行專案之前,權衡每個函式庫的優點與特性是非常重要的。
本文將介紹如何使用 iTextSharp C# 在 PDF 中加入頁碼。 此外,我們也將比較iTextSharp和IronPDF 。
如何使用 iTextSharp C# 在 PDF 中加入頁碼
1.使用任何 IDE 建立新的 C# 專案。 2.建立新的 PDF 物件。 3.在 HTML 頁尾加入頁數。
- 從 HTML 內容建立 PDF。
- 將 PDF 檔案儲存到您的電腦。
什麼是 IronPDF
IronPDF是一個強大的 PDF .NET 框架,開發人員可以使用它來輕鬆產生、檢視和編輯 PDF 文件。 IronPDF 是一個精密的工具,內部以 chromium 引擎執行。 它可以將 HTML5、JavaScript、CSS 和圖片檔案轉換為 PDF,新增自訂頁首和頁腳,並產生與瀏覽器中顯示效果完全相同的 PDF 檔案。 IronPDF for .NET 支援許多線上和網路格式,包括 HTML、ASPX、Razor View 和 MVC。
IronPDF 功能
- 使用 .NET C# 程式碼建立、讀取和簡單編輯 PDF 檔案。
- 透過網站 URL 連結建立 PDF,同時管理 User-Agent、Proxy、Cookie、HTTP 標頭和表單變量,以啟用使用 HTML 登入表單進行登入。
- 從現有 PDF 檔案中刪除影像。
- 包含 PDF 的元素:表格、文字、照片、書籤、浮水印、頁首、頁尾等。
- 能夠輕鬆地分離和合併多個 PDF 文件的頁面。
要了解有關 IronPDF 文件的更多信息,請參閱此處。
安裝 IronPDF。
在 Visual Studio 工具中,選擇 NuGet 套件管理器,即可在"工具"下找到 Visual 命令列介面。 以下指令應輸入套件管理終端標籤。
Install-Package IronPdf
或者我們可以使用套件管理員的方法。 使用 Visual Studio 的 NuGet Package Manager 選項,可以直接將套件安裝到解決方案中。 在 NuGet 網站上有一個搜尋方塊可用來尋找套件。我們只需要在套件管理員中尋找"IronPDF",如下截圖所示:

上方顯示的是相關搜尋結果清單。 若要在您的系統上安裝套件,請進行必要的選擇。
現在套件已經下載並安裝完成,可以在目前的專案中使用。
什麼是 iTextSharp
iTextSharp 是用 C# 製作和修改 PDF 文件的靈活函式庫。 它提供了多種功能,例如加密、PDF合併、文字和圖片提取等等。 iTextSharp 是一種高效的工具,可執行多種任務,包括在 PDF 中加入頁碼。
iTextSharp 功能
- 可透過 iText 函式庫使用 API 來產生 PDF 文件。
- HTML 和 XML 字串均可使用 iText 程式解析為 PDF 檔案。
- 我們可以使用 iText 庫為 PDF 文件添加書籤、頁碼和標記。
- 我們也可以使用 iText 函式庫將一個 PDF 文件拆分成多個 PDF,或將多個 PDF 文件合併成一個 PDF。
- 我們可以使用 iText 修改 PDF 表單。
安裝 iTextSharp
使用 NuGet 套件管理器尋找 iText。 iText7 和 iText.pdfhtml 是必要的安裝程式,因為 iText 功能分佈在許多套件中。
如果您選擇 Visual Command-Line 介面,則需要安裝下列套件:
Install-Package iTextSharp
由於 iText 7 是最新的版本,因此我們在解決方案中採用了 iText 7。
使用 IronPDF 添加頁碼。
透過 IronPDF 的綜合資料庫,在 PDF 檔案中加入頁碼變得非常簡單。 為了便於理解,請參閱以下程式碼。
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Create a new HtmlToPdf renderer instance
var renderer = new HtmlToPdf();
// Define the HTML content with a header
string header = "<h1>Hello IronPDF!</h1>";
// Render the HTML as a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf(header);
// Define the HTML footer with page numbers
HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
};
// Add footer to the PDF
pdf.AddHtmlFooters(htmlFooter);
// Save the PDF document to a file
pdf.SaveAs("output.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Create a new HtmlToPdf renderer instance
var renderer = new HtmlToPdf();
// Define the HTML content with a header
string header = "<h1>Hello IronPDF!</h1>";
// Render the HTML as a PDF document
PdfDocument pdf = renderer.RenderHtmlAsPdf(header);
// Define the HTML footer with page numbers
HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter()
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
};
// Add footer to the PDF
pdf.AddHtmlFooters(htmlFooter);
// Save the PDF document to a file
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create a new HtmlToPdf renderer instance
Dim renderer = New HtmlToPdf()
' Define the HTML content with a header
Dim header As String = "<h1>Hello IronPDF!</h1>"
' Render the HTML as a PDF document
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(header)
' Define the HTML footer with page numbers
Dim htmlFooter As New HtmlHeaderFooter() With {.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"}
' Add footer to the PDF
pdf.AddHtmlFooters(htmlFooter)
' Save the PDF document to a file
pdf.SaveAs("output.pdf")
End Sub
End Class
首先,我們定義要轉換為 PDF 的 HTML 文字。 此HTML內容可以包含單一HTML段落或整個HTML頁面。 接下來,我們建立一個 HtmlToPdf 類別的實例,該類別提供 HTML 到 PDF 的轉換功能 RenderHtmlAsPdf。
HTML 內容作為參數傳遞給 RenderHtmlAsPdf 函數。 我們指定需要轉換為 PDF 的 HTML 內容。 頁碼以佔位符的形式表示,即此 HTML 文字頁尾部分的 {page} of {total-pages}。
此方法將建立的 PDF 文件作為 PdfDocument 物件傳回。 使用 SaveAs 方法,我們將 PDF 文件儲存到名為"output.pdf"的檔案中。 或者,我們可以使用 OpenInDefaultPDFViewer 函數,用系統的預設 PDF 閱讀器開啟已建立的 PDF 文件。 我們也可以使用上述方法為現有的 PDF 檔案添加頁碼。
如何在 C# 中使用 iTextSharp 在 PDF 中新增頁碼:圖 2 - IronPDF:帶有頁碼的輸出 PDF
要了解有關 IronPDF 代碼的更多信息,請參閱此處。
使用 iTextSharp 新增頁碼。
首先,讓我們使用 iTextSharp 來產生一個新的 PDF 文件。 以下是建立帶有頁碼的新 PDF 文件的基本範例:
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
// Create a new PDF document
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("output.pdf", FileMode.Create));
// Open the document to add content
doc.Open();
doc.Add(new Paragraph("Hello, world!"));
// Attach page number event to PDF writer
writer.PageEvent = new PageNumberEventHandler();
// Close the document
doc.Close();
}
}
public class PageNumberEventHandler : PdfPageEventHelper
{
public override void OnOpenDocument(PdfWriter writer, Document document)
{
base.OnOpenDocument(writer, document);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
// Create a table to hold the page number
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 300f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(new Phrase($"Page {writer.PageNumber}"));
cell.Border = 0;
table.AddCell(cell);
// Write the table at the bottom of the page
table.WriteSelectedRows(0, -1, 150f, document.Bottom, writer.DirectContent);
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
}
}
}
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
// Create a new PDF document
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("output.pdf", FileMode.Create));
// Open the document to add content
doc.Open();
doc.Add(new Paragraph("Hello, world!"));
// Attach page number event to PDF writer
writer.PageEvent = new PageNumberEventHandler();
// Close the document
doc.Close();
}
}
public class PageNumberEventHandler : PdfPageEventHelper
{
public override void OnOpenDocument(PdfWriter writer, Document document)
{
base.OnOpenDocument(writer, document);
}
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
// Create a table to hold the page number
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 300f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
PdfPCell cell = new PdfPCell(new Phrase($"Page {writer.PageNumber}"));
cell.Border = 0;
table.AddCell(cell);
// Write the table at the bottom of the page
table.WriteSelectedRows(0, -1, 150f, document.Bottom, writer.DirectContent);
}
public override void OnCloseDocument(PdfWriter writer, Document document)
{
base.OnCloseDocument(writer, document);
}
}
}
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Namespace ConsoleApp1
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create a new PDF document
Dim doc As New Document()
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("output.pdf", FileMode.Create))
' Open the document to add content
doc.Open()
doc.Add(New Paragraph("Hello, world!"))
' Attach page number event to PDF writer
writer.PageEvent = New PageNumberEventHandler()
' Close the document
doc.Close()
End Sub
End Class
Public Class PageNumberEventHandler
Inherits PdfPageEventHelper
Public Overrides Sub OnOpenDocument(ByVal writer As PdfWriter, ByVal document As Document)
MyBase.OnOpenDocument(writer, document)
End Sub
Public Overrides Sub OnEndPage(ByVal writer As PdfWriter, ByVal document As Document)
MyBase.OnEndPage(writer, document)
' Create a table to hold the page number
Dim table As New PdfPTable(1)
table.TotalWidth = 300F
table.HorizontalAlignment = Element.ALIGN_CENTER
Dim cell As New PdfPCell(New Phrase($"Page {writer.PageNumber}"))
cell.Border = 0
table.AddCell(cell)
' Write the table at the bottom of the page
table.WriteSelectedRows(0, -1, 150F, document.Bottom, writer.DirectContent)
End Sub
Public Overrides Sub OnCloseDocument(ByVal writer As PdfWriter, ByVal document As Document)
MyBase.OnCloseDocument(writer, document)
End Sub
End Class
End Namespace
首先,我們為 Document 和 PdfWriter 建立一個新的對象,這樣我們就可以建立一個空白的 PDF 檔案。您可以在 PDF 文件中包含文字、照片、表格和其他類型的內容。 讓我們使用 Paragraph 添加一些範例文字以作示範。 現在最重要的步驟是為PDF文件添加頁碼。 我們將使用 iTextSharp 的頁面事件來實現這一點。 我們首先定義一個繼承自 PdfPageEventHelper 類別的類,以便能夠重寫在 PDF 生成過程中發生特定事件時調用的方法。
此類重寫了 OnEndPage 函數,以新增包含目前頁碼的單一儲存格表格。 最後,在關閉文件之前,我們需要將我們的 PageNumberEventHandler 類別的實例連接到 PdfWriter 物件。 透過此配置,每次新增頁面至 PDF 文件時,都會呼叫 PageNumberEventHandler 類別的 OnEndPage 函數,並在每頁底部新增頁碼。 我們也可以使用現有的PDF文件來新增頁碼。

結論
總而言之, IronPDF的專業性、易用性以及與 .NET 環境的無縫集成,使其成為需要 HTML 到 PDF 轉換及相關功能的場景的最佳選擇,儘管 iTextSharp 在 C# PDF 操作庫領域仍然是一個強大的競爭對手。 透過 IronPDF,您可以輕鬆、有效率且靈活地從 HTML 內容建立發票、報告和動態生成的文檔,從而在現代開發環境中取得成功。
IronPDF 的 Lite 版本包含永久授權、升級選項和一年的軟體維護。 水印試用期可讓使用者在實際設定中評估產品。 請造訪許可證頁面以了解更多詳情。 請造訪此網站以了解更多關於Iron Software的資訊。
常見問題解答
如何使用 C# 將頁碼添加到 PDF?
您可以使用 IronPDF 通過定義帶有頁碼佔位符的 HTML 內容並使用 AddHtmlFooters 方法將其應用為 PDF 中的頁腳來添加頁碼。
使用 IronPDF 進行 PDF 操作的好處是什么?
IronPDF 提供強大的 PDF.NET Framework能力,支持 HTML5、JavaScript、CSS 和圖像到 PDF 轉換,使您可以輕鬆地操控具有自定義頁眉和頁腳的 PDF。
iTextSharp 和 IronPDF 在添加頁碼方面如何比較?
iTextSharp 使用 PdfPageEventHelper 類來處理頁碼,而 IronPDF 通過允許您定義帶有頁碼佔位符的 HTML 頁腳來提供更簡單的方法。
我可以操控現有的 PDF 文件來添加頁碼嗎?
是的,使用 IronPDF,您可以通過渲染帶有頁碼佔位符的 HTML 頁腳並將其與原始文檔合併來向現有 PDF 文件添加頁碼。
轉換 HTML 為 PDF 的首選方法是什麼?
IronPDF 是首選的 HTML 到 PDF 轉換工具,因為它可以無縫集成到 .NET 環境中,並能夠處理 HTML5、JavaScript 和 CSS。
在 C# 項目中使用 IronPDF 的安裝步驟是什麼?
您可以通過在 Visual Studio 的 NuGet 包管理器中搜索 'IronPDF' 並將其添加到您的項目來安裝 IronPDF。
如何確保我的 PDF 使用 C# 擁有準確的頁碼?
使用 IronPDF,通過定義帶有頁碼佔位符的 HTML 模板並一致地將其應用到所有 PDF 頁面上,確保您的頁碼準確。
IronPDF 是否提供可用的授權選項?
IronPDF 提供一個 'Lite' 版本,配有永久許可證、升級選項和一年軟件維護,以及一個帶水印的試用期供評估。
我在哪裡可以找到使用 IronPDF 的詳盡示例和文檔?
使用 IronPDF 的綜合文檔和示例可在他們的官方網站 ironpdf.com 上找到。
IronPDF 是否完全兼容 .NET 10,我可以在 .NET 10 項目中使用頁碼功能嗎?
是的,IronPDF 支援 .NET 10 和它的所有功能——包括使用頁首或頁尾中的佔位符來添加頁碼——在 .NET 10 項目中開箱即用,無需任何特殊配置或解決方案。



