如何為PDF文件新增註釋(初學者教程)
Full Comparison
Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against QuestPDF on pricing, HTML support, and licensing.
便攜式文件格式(PDF)是一種普遍使用的文件格式,能確保不同平台和裝置上的文件呈現一致性。 其固定的版面使其成為共享論文、合同、發票等文件的首選格式。 在企業界,PDF文件對於正式文件而言是不可或缺的。 隨著對PDF生成和操作需求的增長,出現了幾個簡化此過程的程式庫,供開發者使用。
在本文中,我們將探討如何使用C#中的QuestPDF為PDF新增頁碼,並同時對IronPDF進行比較,幫助您決定哪個程式庫更適合您的專案需求。
什麼是IronPDF?
IronPDF是一個功能豐富的程式庫,專為.NET生態系統而構建,旨在高效處理PDF建立、操作和渲染任務。 它利用基於Chromium的引擎,精確地將HTML、CSS和JavaScript轉換為PDF文件。 這使得它成為需要將HTML內容直接轉換為PDF格式的網頁開發者的一個絕佳選擇,同時保留原始的版面和樣式。
使用IronPDF,您可以輕鬆將PDF功能整合到您的.NET應用中,包括建立自定義頁眉和頁腳、為PDF新增新頁、嵌入圖片和表格,以及執行合併或拆分文件等高級PDF操作。 該程式庫支持各種格式,並提供多種自定義選項,使其成為從動態網頁內容生成專業級PDF的理想選擇。
IronPDF的主要功能:
- 允許您直接從C#程式碼生成PDF。
- 將網頁、HTML和JavaScript轉換為高質量PDF。
- 提供新增自定義元素的選項,如頁眉、頁腳和水印。
- 促進合併、拆分和編輯現有PDF。
- 與.NET應用程式無縫工作,包括ASP.NET和MVC框架。
要深入了解IronPDF的功能和更高級的範例,請參閱官方文件。
安裝IronPDF
要將IronPDF新增到您的專案中,請在Visual Studio中使用NuGet程式包管理器。 您可以使用Visual命令行介面或直接在NuGet程式包管理器中搜尋。
命令行安裝:
Install-Package IronPdf
或者,您可以在NuGet程式包管理器中搜尋"IronPDF"並安裝它。

什麼是QuestPDF?
QuestPDF是一個現代.NET程式庫,旨在生成PDF文件。 其重點是為開發者提供一個靈活高效的工具,用於從C#建立PDF。 QuestPDF允許使用聲明式風格的直觀和流暢的方法設計文件。
QuestPDF著重於簡單性、速度和性能,是生成動態報告和文件的絕佳選擇。 該程式庫還支持先進的佈局功能,自定義樣式和易於使用的範本。
QuestPDF的功能
- 易於使用的API構建複雜的PDF文件。
- 支持靈活的佈局和文件結構設置,設置預設頁面、列項目等。
- 允許使用類似CSS的屬性輕鬆設計元素。
- 支持圖片、預設文字樣式設置、表格、條碼、圖表、列、行、多種頁面型別等。
- 非常適合建立報告、發票和資料驅動的文件。
欲了解更多詳情,請參閱QuestPDF文件。
安裝QuestPDF
開始使用QuestPDF,通過NuGet指令行安裝它:
Install-Package QuestPDF
或者,通過NuGet程式包管理器進行安裝:

這將為您的專案新增生成PDF所需的程式庫,使用QuestPDF。
使用IronPDF新增頁碼
IronPDF提供一種簡單的方法將頁碼新增到PDF。 下面的程式碼演示了如何做到這一點:
using IronPdf;
class Program
{
static void Main(string[] args)
{
// HTML content for the PDF
var html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>";
// Set up the IronPDF renderer with header for page numbers
ChromePdfRenderer renderer = new ChromePdfRenderer
{
RenderingOptions =
{
HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
}
}
};
// Render the HTML as a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
// Save the PDF to a file
pdf.SaveAs("pageNumbers.pdf");
}
}
using IronPdf;
class Program
{
static void Main(string[] args)
{
// HTML content for the PDF
var html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>";
// Set up the IronPDF renderer with header for page numbers
ChromePdfRenderer renderer = new ChromePdfRenderer
{
RenderingOptions =
{
HtmlHeader = new HtmlHeaderFooter
{
HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"
}
}
};
// Render the HTML as a PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
// Save the PDF to a file
pdf.SaveAs("pageNumbers.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
' HTML content for the PDF
Dim html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>"
' Set up the IronPDF renderer with header for page numbers
Dim renderer As New ChromePdfRenderer With {
.RenderingOptions = {
HtmlHeader = New HtmlHeaderFooter With {.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"}
}
}
' Render the HTML as a PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)
' Save the PDF to a file
pdf.SaveAs("pageNumbers.pdf")
End Sub
End Class
輸出

在這段程式碼中,我們為PDF文件建立一個HTML頁眉,其中{total-pages}代表當前頁碼和總頁數的動態佔位符。 RenderHtmlAsPdf 方法將HTML轉換為PDF。 該功能允許根據文件的長度動態調整頁面。
如何使用QuestPDF新增頁碼
在QuestPDF中,新增頁碼可以以類似的方式完成。 下面是使用QuestPDF新增頁碼的程式碼:
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
class Program
{
static void Main(string[] args)
{
// Set the license type for QuestPDF
QuestPDF.Settings.License = LicenseType.Community;
// Create a PDF document using the QuestPDF fluent API
var document = Document.Create(container =>
{
// Define a page with content and a header with page numbers
container.Page(page =>
{
page.Content().Text("Hello, QuestPDF!");
// Add a centered header with page number and total pages
page.Header().AlignCenter().Text(text =>
{
text.Span("Page ");
text.CurrentPageNumber();
text.Span(" of ");
text.TotalPages();
});
});
});
// Generate and save the PDF document
document.GeneratePdf("QuestPdfOutput.pdf");
}
}
using QuestPDF.Fluent;
using QuestPDF.Infrastructure;
class Program
{
static void Main(string[] args)
{
// Set the license type for QuestPDF
QuestPDF.Settings.License = LicenseType.Community;
// Create a PDF document using the QuestPDF fluent API
var document = Document.Create(container =>
{
// Define a page with content and a header with page numbers
container.Page(page =>
{
page.Content().Text("Hello, QuestPDF!");
// Add a centered header with page number and total pages
page.Header().AlignCenter().Text(text =>
{
text.Span("Page ");
text.CurrentPageNumber();
text.Span(" of ");
text.TotalPages();
});
});
});
// Generate and save the PDF document
document.GeneratePdf("QuestPdfOutput.pdf");
}
}
Imports QuestPDF.Fluent
Imports QuestPDF.Infrastructure
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Set the license type for QuestPDF
QuestPDF.Settings.License = LicenseType.Community
' Create a PDF document using the QuestPDF fluent API
Dim document = Document.Create(Sub(container)
' Define a page with content and a header with page numbers
container.Page(Sub(page)
page.Content().Text("Hello, QuestPDF!")
' Add a centered header with page number and total pages
page.Header().AlignCenter().Text(Sub(text)
text.Span("Page ")
text.CurrentPageNumber()
text.Span(" of ")
text.TotalPages()
End Sub)
End Sub)
End Sub)
' Generate and save the PDF document
document.GeneratePdf("QuestPdfOutput.pdf")
End Sub
End Class

這段QuestPDF程式碼定義了一個簡單的文件,頁眉中包含頁碼。 TotalPages()方法用於動態生成相對於每頁的頁碼。
總結

總之,IronPDF和QuestPDF都提供了有效的解決方案用於在C#中將頁碼新增到PDF中。 然而,IronPDF提供了一種更精簡且使用者友好的方法。 其靈活性和易用性使其成為開發者需要最小努力來新增頁碼或操作現有PDF的理想選擇。
IronPDF可免費用於開發,允許開發者在專案中進行試驗和整合,在開發階段中沒有任何費用。 一旦準備好投入生產,商業授權提供了這些授權選項。
通過選擇IronPDF,開發者可以獲得一個可靠且功能豐富的工具,簡化PDF的建立和編輯,包括插入頁碼,並附帶持續的維護和更新。
有關IronPDF免費版本和商業許可的更多資訊,請存取IronPDF的官方網站。
常見問題
如何使用C#向PDF文件新增頁碼?
您可以使用IronPDF透過建立包含{page}和{total-pages}等佔位符的HTML標頭或頁腳來向PDF新增頁碼。使用RenderHtmlAsPdf方法時,這些佔位符會動態更新以反映當前頁面和總頁數。
IronPDF和QuestPDF在PDF操作方面有何主要區別?
IronPDF功能豐富,利用基於Chromium的引擎,非常適合需要精確佈局控制的網頁開發人員。它支援將HTML、CSS和JavaScript轉換為PDF。QuestPDF提供了一個現代的聲明式API,專注於簡單性和性能,適用於具有靈活佈局的動態報告。
如何在.NET專案中安裝一個PDF程式庫?
要在.NET專案中安裝像IronPDF這樣的PDF程式庫,請在Visual Studio中使用NuGet套件管理器。您可以使用命令列Install-Package IronPdf安裝它,或在NuGet套件管理器介面中找到它。
IronPDF為網頁開發人員提供了哪些優勢?
IronPDF對網頁開發人員有利,因為它可以將HTML、CSS和JavaScript轉換為PDF,保持精確的佈局和樣式。它還支援新增自訂標頭、頁腳和進階文件操作,如合併和拆分PDF。
可以免費使用IronPDF嗎?
可以,在開發階段可以免費使用IronPDF,允許開發人員無成本地整合和測試其功能。但在生產使用時需要商業授權。
在C#中使用PDF程式庫進行文件管理有什麼好處?
在C#中使用像IronPDF這樣的PDF程式庫可以簡化文件管理,使PDF的生成、操作和轉換變得容易。它提供了保持文件呈現一致的工具,並支援進階功能,如新增頁碼、自訂標頭和合併文件。
我可以使用IronPDF自定義PDF的外觀嗎?
可以,IronPDF允許使用HTML和CSS進行PDF的樣式自定義。您可以建立自訂的標頭、頁腳和浮水印,確保PDF符合特定的設計要求。



