在C# .NET程式庫中生成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是一個運行在chromium引擎上的先進工具。 它可以將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包管理器,您可以在工具下找到Visual命令行介面。 應在包管理終端標籤中輸入以下命令。
Install-Package IronPdf
或者我們可以使用包管理器方法。 可使用Visual Studio的NuGet包管理器選項直接將包安裝到解決方案中。 NuGet網站上有一個搜尋框用於查找包。我們只需在包管理器中搜尋"IronPDF",如下所示:
在C#中使用iText將頁碼新增到PDF:圖1 - 從包管理器安裝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和iText.pdfhtml。
如果選擇Visual命令行介面,需要安裝以下包:
Install-Package iTextSharp
由於iText是最新版本,所以我們在解決方案中使用它。
使用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頁面。 接下來,我們建立了一個RenderHtmlAsPdf。
HTML內容被作為參數傳遞給RenderHtmlAsPdf函式。 我們指定需要轉換為PDF的HTML材料。 頁碼作為佔位符表示,或在此HTML文字的頁腳部分使用{page} of {total-pages}。
該方法返回建立的PDF文件,作為PdfDocument物件。 使用SaveAs方法,我們將PDF文件保存為名為"output.pdf"的文件。 或者,我們可以使用OpenInDefaultPDFViewer函式使用系統的預設PDF閱讀器打開建立的PDF文件。 我們還可以使用上述方法向已存在的PDF文件新增頁碼。
在C#中使用iText將頁碼新增到PDF:圖2 - IronPDF:帶有頁碼的輸出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 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
首先,我們為PdfWriter建立新物件,這使我們可以建立一個空的PDF文件。您可以在PDF文件中包含文字、圖片、表格和其他形式的材料。 讓我們使用Paragraph新增一些範例文字以作為演示。 重要的一步是現在向PDF文件中新增頁碼。 我們將使用iText的頁面事件來實現此功能。 我們首先定義一個類,該類從PdfPageEventHelper類繼承,以便能夠覆寫在PDF生成過程中發生特定事件時調用的方法。
該類覆蓋了OnEndPage函式,新增了一個包含當前頁碼的單元的表格。 最後,在關閉文件之前,我們需要將PdfWriter物件。 通過這個配置,OnEndPage函式將在每次向PDF文件新增新頁面時調用,在每個頁面的底部新增頁碼。 我們也可以使用現有的PDF文件新增頁碼。
在C#中使用iText將頁碼新增到PDF:圖3 - iText:帶有頁碼的輸出PDF
總結
總之,IronPDF的專業性、可用性以及與.NET環境的無縫整合,使其成為需要HTML到PDF轉換及相關功能的場景中的最佳選擇,雖然iText在C# PDF操作程式庫中仍然是一個強大的競爭者。 使用IronPDF,您可以從HTML內容建立發票、報告和動態生成的文件,並具備現代開發環境所需的輕松、有效和可擴展性。
IronPDF的Lite版本包含永久授權、升級選項以及一年期的軟體維護服務。 帶有浮水印的試用期允許使用者在實際環境中評估產品。 存取頁面以獲取更多詳情。 存取此網站以了解有關Iron Software的更多資訊。
常見問題
如何使用C#在PDF中新增頁碼?
您可以使用IronPDF通過定義具有頁碼佔位符的HTML內容並使用AddHtmlFooters方法將其作為頁尾應用於PDF來新增頁碼。
使用IronPDF進行PDF操作的優勢是什麼?
IronPDF提供強大的PDF.NET框架功能,支持HTML5、JavaScript、CSS和圖像到PDF轉換,使PDF的操作變得容易,特別是在自訂標頭和頁尾方面。
iTextSharp與IronPDF在新增頁碼方面如何比較?
iTextSharp使用PdfPageEventHelper類來處理頁碼,而IronPDF則提供了一個更簡單的方法,允許您定義具有頁碼佔位符的HTML頁尾。
我能操作現有的PDF文件來新增頁碼嗎?
可以,使用IronPDF,您可以將頁碼新增至現有的PDF文件,方法是渲染具有頁碼佔位符的HTML頁尾並將其與原始文件合併。
進行HTML到PDF的轉換,首選的方法是什麼?
由於IronPDF能夠無縫整合.NET環境並處理HTML5、JavaScript和CSS,IronPDF是進行HTML到PDF轉換的首選解決方案。
在C#專案中使用IronPDF的安裝步驟有哪些?
您可以在Visual Studio中通過NuGet套件管理器搜尋'IronPDF',並將其新增至您的專案中來安裝IronPDF。
如何確保我的PDF使用C#時有準確的頁碼?
使用IronPDF,透過定義具有頁碼佔位符的HTML模板並將其一致地應用於所有PDF頁面來確保您的頁碼準確。
IronPDF有授權選項嗎?
IronPDF提供永久授權的'Lite'版本、升級選項和一年的軟體維護,並附有水印的試用期以供評估。
我在哪裡可以找到使用IronPDF的詳細範例和文件?
可以在他們的官方網站ironpdf.com找到使用IronPDF的完整文件和範例。
IronPDF是否完全支持.NET 10,而我可以在.NET 10專案中使用頁碼功能嗎?
可以,IronPDF支持.NET 10,並且其所有功能——包括使用佔位符在標頭或頁尾中新增頁碼——在.NET 10專案中可即裝即用,無需任何特殊配置或變通方法。

