Excel轉PDF轉換器(免費在線工具列表)
Full Comparison
Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing.
"可攜式文件格式",或稱PDF,是由Adobe建立的文件格式。 在展示需要保持文字和圖片格式一致的文件時,PDF很方便。 在當今世界,PDF文件是必要的,並在各領域中用於文件建立和發票開具。 由於市場上有多個PDF程式庫,建立PDF幾乎成為本能。選擇合適的PDF程式庫對於您的專案至關重要,因此在使用前務必評估每個程式庫的優勢和特性。
在本文中,我們將看到如何使用iText C#在PDF中新增頁碼。 此外,我們將比較iText和IronPDF。
如何使用iText C#在PDF中新增頁碼
- 使用任何IDE建立一個新的C#專案。
- 建立一個新的PDF物件。
- 在HTML頁腳中新增頁碼。
- 從HTML內容建立PDF。
- 將PDF文件保存到您的電腦中。
什麼是IronPDF
IronPDF是一個強大的PDF .NET Framework,開發人員可以使用它輕鬆地建立、查看和編輯PDF。 IronPDF是一個內部運行於chrome引擎上的高級工具。 它可以將HTML5、JavaScript、CSS和圖像文件轉換為PDF,新增自訂的標題和頁腳,以及生成與瀏覽器中顯示完全一致的PDF。 IronPDF支持許多網上和網上格式,包括HTML、ASPX、Razor View和MVC。
IronPDF功能
- 使用.NET C#程式碼建立、讀取和輕鬆編輯PDF文件。
- 從網站URL連結建立PDF,管理使用者代理、代理、Cookie、HTTP頭和表單變數,以便使用HTML登錄表單進行登錄。
- 從現有的PDF文件中提取圖像。
- 包含元素到PDF:表格、文字、圖像、書籤、水印、標題、頁腳等。
- 能夠輕鬆拆分和合併多個PDF文件中的頁面。
要了解更多IronPDF的文件,請參考這裡。
安裝 IronPDF
在Visual Studio工具中,選擇NuGet Package Manager,然後可以在工具中找到Visual Command-Line介面。 應在套件管理終端標籤中輸入以下命令。
Install-Package IronPdf
或者我們可以使用包管理方式。 可以使用Visual Studio的NuGet Package Manager選項直接將包安裝到解決方案中。 可用於在NuGet網站上查找包的搜尋框。我們只需在包管理器中搜尋"IronPDF",如以下截圖所示:

上圖中顯示了相關的搜尋結果列表。 請進行必要的選擇以將包安裝到您的系統上。
現在,包已經下載並安裝好,可以在當前專案中使用。
什麼是iText
iText是一個靈活的程式庫,用於在C#中製作和修改PDF文件。 它提供了多種功能,例如加密、PDF合併、提取文字和圖像等。 iText是完成許多任務的高效工具,包括在PDF中新增頁碼。
iText功能
- iText程式庫提供了一個用於生成PDF文件的API。
- 可以使用iText程式將HTML和XML字串解析為PDF文件。
- 我們可以使用iText程式庫在PDF文件中新增書籤、頁碼和標記。
- 我們還可以使用iText程式庫將PDF文件拆分為多個PDF或將多個PDF文件合併為單個PDF。
- 我們可以使用iText修改PDF表單。
安裝iText
使用NuGet包管理器搜索iText。 需要安裝iText和iText.pdfhtml,因為iText的功能分佈在多個包中。
如果您選擇了Visual Command-Line介面,則需要安裝以下包:
Install-Package iText7
Install-Package itext7.pdfhtml
Install-Package iText7
Install-Package itext7.pdfhtml
由於iText是最新版本,因此我們使用它在我們的解決方案中。
使用IronPDF新增頁碼
IronPDF的全面程式庫使向PDF文件中新增頁碼變得簡單。 如需說明,請參見以下程式碼:
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Initialize the IronPdf renderer
var renderer = new IronPdf.HtmlToPdf();
// HTML content to convert to PDF
string header = "<h1>Hello Ironpdf!</h1>";
// Render the HTML content to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(header);
// Define the footer with page number placeholders
HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
};
// Add footer to the PDF
pdf.AddHtmlFooters(htmlFooter);
// Save the output PDF file
pdf.SaveAs("output.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
// Initialize the IronPdf renderer
var renderer = new IronPdf.HtmlToPdf();
// HTML content to convert to PDF
string header = "<h1>Hello Ironpdf!</h1>";
// Render the HTML content to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(header);
// Define the footer with page number placeholders
HtmlHeaderFooter htmlFooter = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
};
// Add footer to the PDF
pdf.AddHtmlFooters(htmlFooter);
// Save the output PDF file
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Initialize the IronPdf renderer
Dim renderer = New IronPdf.HtmlToPdf()
' HTML content to convert to PDF
Dim header As String = "<h1>Hello Ironpdf!</h1>"
' Render the HTML content to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(header)
' Define the footer with page number placeholders
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 output PDF file
pdf.SaveAs("output.pdf")
End Sub
End Class
解釋
- 初始化渲染器:我們建立一個
HtmlToPdf實例,該實例提供HTML轉PDF的轉換功能。 - 定義HTML內容:指定需要轉換為PDF的HTML內容。
- 渲染HTML內容:使用
RenderHtmlAsPdf函式將HTML轉換為PDF文件。 - 定義頁腳:頁碼以佔位符形式體現在HTML頁腳文字中。
-
新增頁腳和保存PDF:將頁腳應用於文件並將其保存為PDF文件。

要了解更多IronPDF程式碼,請參考這裡。
使用iText新增頁碼
首先,我們使用iText生成一個新的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 document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create));
// Open the document to add content
document.Open();
document.Add(new Paragraph("Hello, world!"));
// Attach page number event to PDF writer
writer.PageEvent = new PageNumberEventHandler();
// Close the document
document.Close();
}
}
public class PageNumberEventHandler : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
// Create a table for the page number
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 300f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
// Add page number to the table cell
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, 150, document.Bottom, writer.DirectContent);
}
}
}
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 document = new Document();
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("output.pdf", FileMode.Create));
// Open the document to add content
document.Open();
document.Add(new Paragraph("Hello, world!"));
// Attach page number event to PDF writer
writer.PageEvent = new PageNumberEventHandler();
// Close the document
document.Close();
}
}
public class PageNumberEventHandler : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);
// Create a table for the page number
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 300f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
// Add page number to the table cell
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, 150, document.Bottom, writer.DirectContent);
}
}
}
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 document As New Document()
Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream("output.pdf", FileMode.Create))
' Open the document to add content
document.Open()
document.Add(New Paragraph("Hello, world!"))
' Attach page number event to PDF writer
writer.PageEvent = New PageNumberEventHandler()
' Close the document
document.Close()
End Sub
End Class
Public Class PageNumberEventHandler
Inherits PdfPageEventHelper
Public Overrides Sub OnEndPage(ByVal writer As PdfWriter, ByVal document As Document)
MyBase.OnEndPage(writer, document)
' Create a table for the page number
Dim table As New PdfPTable(1)
table.TotalWidth = 300F
table.HorizontalAlignment = Element.ALIGN_CENTER
' Add page number to the table cell
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, 150, document.Bottom, writer.DirectContent)
End Sub
End Class
End Namespace
解釋
- 建立PDF文件:初始化新的
PdfWriter以生成空PDF文件。 - 新增內容:向文件新增基礎內容,如段落。
- 頁碼編號:我們利用iText的事件處理來包括頁碼。 我們建立了一個繼承自
PdfPageEventHelper的自訂類。 - 覆寫
OnEndPage方法來在每頁底部新增頁碼。 -
關閉文件:通過關閉文件來完成它。

結論
總而言之,IronPDF的專業性、使用便利性以及與.NET環境的無縫整合使其成為需要HTML到PDF轉換及相關功能的最佳選擇,即使iText在C# PDF處理程式庫領域依然是強大的競爭者。 使用IronPDF,您可以從HTML內容建立發票、報告和動態生成的文件,具備成功現代開發環境所需的輕鬆性、有效性和適應性。
IronPDF的Lite版中包含永久授權、升級選項和一年軟體維護。 水印試用期允許使用者在實際環境中評估這款產品。 請存取授權頁面以獲取更多資訊。 欲了解更多有關Iron Software的資訊,請存取他們的網站。
常見問題
如何在C#中使用iTextSharp為PDF新增頁碼?
要在C#中使用iTextSharp新增頁碼,可以建立一個繼承自PdfPageEventHelper的自訂類別。覆寫OnEndPage方法以插入頁碼,並將此事件附加到PdfWriter實例。
為什麼PDF在各個行業中至關重要?
PDF保留文字和圖像的一致格式,這對於文件建立和在企業領域的開票至關重要。它們在以專業方式展示文件時可靠可用。
使用IronPDF進行PDF操作有什麼優勢?
IronPDF在.NET環境中提供建立、查看和編輯PDF的強大功能。它在將HTML、JavaScript和圖片轉成PDF方面表現出色,並支援自訂頁眉、頁腳和水印。
IronPDF可以處理帶JavaScript支援的HTML到PDF轉換嗎?
可以,IronPDF能將HTML內容(包括JavaScript)轉換為PDF,同時保留其在網頁瀏覽器中顯示的佈局和功能。
如何在C#專案中安裝IronPDF?
您可以使用Visual Studio中的NuGet套件管理器安裝IronPDF。只需搜尋'IronPDF'並將其新增到您的專案即可開始使用其功能。
IronPDF提供的試用期有什麼重要性?
IronPDF的試用期允許開發者在真實應用中透過帶水印的試用來探索其功能,以在購買授權之前評估其適用性。
iTextSharp和IronPDF在新增頁碼上有何比較?
iTextSharp使用事件處理來新增頁碼,而IronPDF則提供使用HTML範本來設置頁眉和頁腳的更直接方法,這使其成為尋求易於使用的.NET開發人員的理想選擇。
在為C#專案選擇PDF程式庫時應考慮哪些因素?
考慮每個程式庫提供的功能。例如,IronPDF專注於HTML到PDF轉換,並且易於與.NET框架整合,而iTextSharp則提供強大的加密和文字提取功能。
iTextSharp是否與.NET 10相容以新增頁碼,還是應該使用iText 7?
iTextSharp(版本5.x)已正式不再更新並進入維護模式;它僅接收安全修補程式,並且不再有新功能更新。它並未直接支持.NET 10,並且在現代.NET版本中使用可能會導致相容性限制。對於新專案或升級到.NET 10的專案,強烈推薦使用iText 7(或iText Core),因為它支持最新的框架並包含相較於iTextSharp的改進。([itextpdf.com](https://itextpdf.com/products/itextsharp?utm_source=openai))



