
Excel轉PDF轉換器(免費在線工具列表)
"可攜式文件格式",或稱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介面。 應在套件管理終端標籤中輸入以下命令。
或者我們可以使用包管理方式。 可以使用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
由於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");
}
}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);
}
}
}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的資訊,請存取他們的網站。

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
相關文章


