产品比较 如何在C#中使用iTextSharp在PDF中添加页码 Curtis Chau 已更新:七月 28, 2025 Download IronPDF NuGet 下载 DLL 下载 Windows 安装程序 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article “便携式文档格式(PDF)”是一种由Adobe创建的文件格式。 PDF在展示需要统一格式的文本文档和图像时非常方便。 在当今世界,PDF文件非常重要,并在所有企业领域用于文档创建和开具发票。 由于市场上现有的多个PDF库,创建PDF几乎变得本能。在为您的项目使用PDF库之前,选择合适的PDF库至关重要,您需要权衡每个库的优点和特点。 本文中,我们将看到如何使用iTextSharp C#在PDF中添加页码。 Also, we are going to compare iTextSharp with IronPDF. 如何使用iTextSharp C#在PDF中添加页码 使用任何IDE创建一个新的C#项目。 创建一个新的PDF对象。 将页码添加到HTML页脚。 从HTML内容创建PDF。 将PDF文件保存到您的计算机上。 什么是IronPDF IronPDF是一个功能强大的PDF .NET框架,开发者可以用来轻松创建、查看和编辑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包管理器,您可以在“工具”下找到可视化命令行界面。 应在包管理终端选项卡中输入以下命令。 Install-Package IronPdf 或者我们可以使用包管理器方法。 可以通过Visual Studio的NuGet包管理器选项将包直接安装到解决方案中。 NuGet网站上有一个搜索框可以定位包。我们只需在包管理器中查找“IronPDF”,如下截图所示: 上图显示了相关搜索结果的列表。 为了在您的系统上安装包,请做出必要的选择。 现在包已下载并安装,可以在当前项目中使用。 什么是iTextSharp iTextSharp是一个用于在C#中生成和修改PDF文档的灵活库。 它提供了多种功能,例如加密、PDF合并、文本和图像提取等。 iTextSharp是一个高效的工具,可用于许多任务,包括向PDF中添加页码。 iTextSharp功能 iText库提供了一个用于生成PDF文档的API。 iText程序可将HTML和XML字符串解析为PDF文件。 我们可以使用iText库为PDF文档添加书签、页码和标记。 我们还可以使用iText库将PDF文档拆分为多个PDF,或合并多个PDF文档为一个PDF。 我们可以使用iText修改PDF表单。 安装iTextSharp 使用NuGet包管理器搜索iText。 由于iText功能分布在多个包中,需要安装iText7和iText.pdfhtml。 如果选择Visual命令行界面,需要安装以下包: Install-Package iText7 Install-Package itext7.pdfhtml Install-Package iText7 Install-Package itext7.pdfhtml SHELL 由于iText 7是最新版本,因此我们在解决方案中使用它。 使用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 $vbLabelText $csharpLabel 解释 初始化渲染器:我们创建一个HtmlToPdf的实例,该实例提供HTML到PDF的转换功能。 定义HTML内容:指定需要转换为PDF的HTML内容。 渲染HTML内容:使用RenderHtmlAsPdf函数将HTML转换为PDF文档。 定义页脚:页码以占位符的形式在HTML页脚文本中表示。 添加页脚并保存PDF:将页脚应用于文档并将其保存为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 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 $vbLabelText $csharpLabel 解释 创建PDF文档:初始化一个新的Document和PdfWriter以生成一个空的PDF文件。 添加内容:向文档添加段落等基本内容。 页码:我们利用iTextSharp的事件处理功能来包含页码。 我们创建了一个自定义类继承自PdfPageEventHelper。 重写OnEndPage:在自定义类中重写OnEndPage方法,以在每页底部添加页码。 关闭文档:通过关闭文档来完成文档。 结论 总之,虽然iTextSharp在C# PDF操作库领域仍然是强大的竞争对手,但IronPDF的专业化、可用性以及与.NET环境的无缝集成,使其在需要HTML到PDF转换及相关功能的情况下成为最佳选择。 使用IronPDF,您可以从HTML内容轻松创建发票、报告和动态生成的文档,以现代开发环境中成功所需的效率和灵活性。 IronPDF的Lite版本包含永久许可证、升级选项和一年的软件维护。 带水印的试用期允许用户在实际设置中评估产品。 访问许可页面以了解更多信息。 欲了解Iron Software的更多详情,请访问他们的网站。 {i:(iTextSharp 是其各自所有者的注册商标。 本网站与 iTextSharp 无关,也未得到 iTextSharp 的支持或赞助。所有产品名称、徽标和品牌均为其各自所有者的财产。 比较仅供参考,反映的是撰写时的公开信息。] 常见问题解答 如何使用iTextSharp在C#中为PDF添加页码? 要在C#中使用iTextSharp添加页码,可以创建一个继承自PdfPageEventHelper的自定义类。重写OnEndPage方法以插入页码,并将此事件附加到PdfWriter实例。 为什么PDF 在各个行业中至关重要? PDF保持文本和图像的一致格式,这对文件创建和跨企业部门开票非常重要。它们在以专业方式展示文件方面可靠。 使用IronPDF进行PDF操作的优势是什么? IronPDF为在.NET环境中创建、查看和编辑PDF提供了强大的功能。它擅长将HTML、JavaScript和图像转换为PDF,并支持自定义页眉、页脚和水印。 IronPDF能否处理支持JavaScript的HTML到PDF转换? 是的,IronPDF可以将包括JavaScript在内的HTML内容转换为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)) Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已发布十一月 13, 2025 比较 C# HTML 到 PDF 开源与 IronPDF 比较开源 HTML 到 PDF 库与 IronPDF for C#。发现哪个解决方案为您的 .NET 项目提供最佳的 PDF 生成能力。 阅读更多 已发布十月 27, 2025 哪种 ASP.NET Core PDF 库性价比最高? 发现适合ASP.NET Core应用程序的最佳PDF库。比较IronPDF的Chrome引擎与Aspose和Syncfusion的替代品。 阅读更多 已发布十月 27, 2025 如何使用 Aspose C# 与 IronPDF 创作 PDF 通过此逐步指南,学习如何使用 Aspose C# 与 IronPDF 创建 PDF,专为开发人员设计。 阅读更多 C# 报告工具(功能比较)如何在C#中使用iTextSharp读...
已发布十一月 13, 2025 比较 C# HTML 到 PDF 开源与 IronPDF 比较开源 HTML 到 PDF 库与 IronPDF for C#。发现哪个解决方案为您的 .NET 项目提供最佳的 PDF 生成能力。 阅读更多
已发布十月 27, 2025 哪种 ASP.NET Core PDF 库性价比最高? 发现适合ASP.NET Core应用程序的最佳PDF库。比较IronPDF的Chrome引擎与Aspose和Syncfusion的替代品。 阅读更多
已发布十月 27, 2025 如何使用 Aspose C# 与 IronPDF 创作 PDF 通过此逐步指南,学习如何使用 Aspose C# 与 IronPDF 创建 PDF,专为开发人员设计。 阅读更多